You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
active/alogs/logger_scope.go

189 lines
4.1 KiB

// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name scope.go
// Created by DEV
// Modified 24/04/2024
//
// Copyright 2024 activething.com
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
package alogs
import (
"fmt"
"strings"
)
type (
LoggerScope struct {
loggerObject
path string
nodes [][]byte
mode ScopeMode
parent Logger
scopes loggerScopes
}
LoggerScopeFnc func (s *LoggerScope)
)
func newLoggerScope (name string, level Level, parent Logger, path string, nodes [][]byte, options []LoggerScopeFnc) *LoggerScope {
if path != "" {
path = fmt.Sprintf("%s%c%s",path,G.ScopeSep,name)
} else {
path = name
}
sc := &LoggerScope {
parent : parent,
nodes : append(nodes,[]byte(name)),
path : path,
}
sc.loggerObject = *newLoggerObject(name,level,sc.log)
for _,o := range options {
o(sc)
}
return sc
}
func (l *LoggerScope) Nodes() [][]byte {
return l.nodes }
func (l *LoggerScope) Path () string {
return l.path }
func (l *LoggerScope) Mode () (m ScopeMode) {
l.RLock()
m = l.mode
l.RUnlock()
return
}
func (l *LoggerScope) SetMode (mode ScopeMode) {
if !mode.isValid() {
panic(G.ErrInvalidScopeMode)
}
l.Lock()
l.mode = mode
l.Unlock()
}
func (l *LoggerScope) GetScope (name string) *LoggerScope {
return l.getScope(strings.Split(strings.ToLower(name),g.scopeSepStr)) }
func (l *LoggerScope) getScope (nodes []string)( sc *LoggerScope) {
l.RLock()
if sc = l.scopes.getScope(nodes[0]); sc != nil {
if len(nodes) > 1 {
sc = sc.getScope(nodes[:1])
}
}
l.RUnlock()
return
}
func (l *LoggerScope) NewScope (path string, options ...LoggerScopeFnc) (*LoggerScope,error) {
return l.newScope(strings.Split(strings.ToLower(path),g.scopeSepStr),options) }
func (l *LoggerScope) newScope (path []string, options []LoggerScopeFnc) (sc *LoggerScope, er error) {
l.Lock()
if sc = l.scopes.getScope(path[0]); sc == nil {
sc = newLoggerScope(path[0], l.level, l, l.path, l.nodes,options)
l.scopes = append(l.scopes, sc)
if len(path) == 1 {
l.Unlock()
return
}
} else if len(path) == 1 {
l.Unlock()
return nil,G.ErrDuplicatedScope.WrapWithFieldString(errFieldScope,path[0])
}
sc,er = sc.newScope(path[1:],options)
l.Unlock()
return
}
func (l *LoggerScope) Log(ent *Entry) error {
l.Lock()
if l.mode.IsFilter() && !l.level.Has(ent.Level) {
l.Unlock()
return nil
}
if l.mode.IsCascade() {
l.extenders.ExtendEntry(ent)
}
l.Unlock()
return l.parent.Log(ent)
}
func (l *LoggerScope) log(lvl Level, msg string, err error,ops []EntryFnc){
l.Lock()
if !l.level.Has(lvl) {
l.Unlock()
return
}
en := l.extenders.ExtendEntry(AcquireEntry(nil,lvl,msg,err,ops...))
en.Scope = l.path
en.Scopes= l.nodes
l.Unlock()
_ = l.parent.Log(en)
ReleaseEntry(en)
}
func LoggerScopeWithMode(mode ScopeMode) LoggerScopeFnc {
return func(s *LoggerScope) {
if !mode.isValid() {
panic(G.ErrInvalidScopeMode.WrapWithFieldString(entryFieldLogger,s.name))
}
if mode == ScopeDirect {
s.mode = ScopeDirect
return
}
s.mode |= mode
}
}
/*
func LoggerScopeWithLevel(level Level) LoggerScopeFnc {
return func(s *LoggerScope) {
if !level.isValid() {
panic(G.ErrInvalidLevel.WrapWithFieldString(entryFieldLogger,s.name))
}
s.level = level
}
}
*/
func LoggerScopeWithExtender(extender ...EntryExtender) LoggerScopeFnc {
return func(s *LoggerScope) {
s.extenders = append(s.extenders,extender ...) } }

Powered by TurnKey Linux.