fix replacing existing logger
This commit is contained in:
parent
7137732a2a
commit
92f08eaa79
@ -32,6 +32,7 @@ func CtxGetFields(ctx context.Context) (fields []zap.Field) {
|
||||
|
||||
type CtxLogger struct {
|
||||
*zap.Logger
|
||||
name string
|
||||
}
|
||||
|
||||
func (cl CtxLogger) DebugCtx(ctx context.Context, msg string, fields ...zap.Field) {
|
||||
@ -51,5 +52,9 @@ func (cl CtxLogger) ErrorCtx(ctx context.Context, msg string, fields ...zap.Fiel
|
||||
}
|
||||
|
||||
func (cl CtxLogger) With(fields ...zap.Field) CtxLogger {
|
||||
return CtxLogger{cl.Logger.With(fields...)}
|
||||
return CtxLogger{cl.Logger.With(fields...), cl.name}
|
||||
}
|
||||
|
||||
func (cl CtxLogger) Sugar() *zap.SugaredLogger {
|
||||
return NewNamedSugared(cl.name)
|
||||
}
|
||||
|
||||
@ -8,12 +8,13 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
mu sync.Mutex
|
||||
logger *zap.Logger
|
||||
loggerConfig zap.Config
|
||||
namedLevels = make(map[string]zap.AtomicLevel)
|
||||
namedGlobs = make(map[string]glob.Glob)
|
||||
namedLoggers = make(map[string]CtxLogger)
|
||||
mu sync.Mutex
|
||||
logger *zap.Logger
|
||||
loggerConfig zap.Config
|
||||
namedLevels = make(map[string]zap.AtomicLevel)
|
||||
namedGlobs = make(map[string]glob.Glob)
|
||||
namedLoggers = make(map[string]CtxLogger)
|
||||
namedSugarLoggers = make(map[string]*zap.SugaredLogger)
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -59,10 +60,18 @@ func SetNamedLevels(l map[string]zap.AtomicLevel) {
|
||||
|
||||
for name, nl := range namedLoggers {
|
||||
level := getLevel(name)
|
||||
// this can be racy, but
|
||||
nl.Logger = zap.New(logger.Core()).WithOptions(
|
||||
newCore := zap.New(logger.Core()).Named(name).WithOptions(
|
||||
zap.IncreaseLevel(level),
|
||||
).Named(name)
|
||||
)
|
||||
*(nl.Logger) = *newCore
|
||||
}
|
||||
|
||||
for name, nl := range namedSugarLoggers {
|
||||
level := getLevel(name)
|
||||
newCore := zap.New(logger.Core()).Named(name).WithOptions(
|
||||
zap.IncreaseLevel(level),
|
||||
).Sugar()
|
||||
*(nl) = *newCore
|
||||
}
|
||||
}
|
||||
|
||||
@ -100,11 +109,24 @@ func NewNamed(name string, fields ...zap.Field) CtxLogger {
|
||||
}
|
||||
|
||||
level := getLevel(name)
|
||||
l := zap.New(logger.Core()).WithOptions(
|
||||
zap.IncreaseLevel(level),
|
||||
).Named(name)
|
||||
l := zap.New(logger.Core()).Named(name).WithOptions(zap.IncreaseLevel(level),
|
||||
zap.Fields(fields...))
|
||||
|
||||
ctxL := CtxLogger{l}
|
||||
ctxL := CtxLogger{Logger: l, name: name}
|
||||
namedLoggers[name] = ctxL
|
||||
return ctxL
|
||||
}
|
||||
|
||||
func NewNamedSugared(name string) *zap.SugaredLogger {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
if l, nameExists := namedSugarLoggers[name]; nameExists {
|
||||
return l
|
||||
}
|
||||
|
||||
level := getLevel(name)
|
||||
l := zap.New(logger.Core()).Named(name).Sugar().WithOptions(zap.IncreaseLevel(level))
|
||||
namedSugarLoggers[name] = l
|
||||
return l
|
||||
}
|
||||
|
||||
@ -4,8 +4,10 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
|
||||
"github.com/anytypeio/any-sync/app/logger"
|
||||
aclrecordproto "github.com/anytypeio/any-sync/commonspace/object/acl/aclrecordproto"
|
||||
"github.com/anytypeio/any-sync/commonspace/object/acl/aclrecordproto"
|
||||
"github.com/anytypeio/any-sync/commonspace/object/keychain"
|
||||
"github.com/anytypeio/any-sync/util/keys"
|
||||
"github.com/anytypeio/any-sync/util/keys/asymmetric/encryptionkey"
|
||||
@ -13,10 +15,9 @@ import (
|
||||
"github.com/anytypeio/any-sync/util/keys/symmetric"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"go.uber.org/zap"
|
||||
"hash/fnv"
|
||||
)
|
||||
|
||||
var log = logger.NewNamed("acllist").Sugar()
|
||||
var log = logger.NewNamedSugared("acllist")
|
||||
|
||||
var (
|
||||
ErrNoSuchUser = errors.New("no such user")
|
||||
|
||||
@ -4,15 +4,16 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/anytypeio/any-sync/app/logger"
|
||||
"github.com/anytypeio/any-sync/commonspace/object/tree/treestorage"
|
||||
"github.com/anytypeio/any-sync/util/slice"
|
||||
"go.uber.org/zap"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
log = logger.NewNamed("acltree").Sugar()
|
||||
log = logger.NewNamedSugared("acltree")
|
||||
ErrEmpty = errors.New("logs empty")
|
||||
)
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user