fix replacing existing logger

This commit is contained in:
Roman Khafizianov 2023-03-10 19:18:15 +01:00 committed by Mikhail Iudin
parent 7137732a2a
commit 92f08eaa79
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
4 changed files with 48 additions and 19 deletions

View File

@ -32,6 +32,7 @@ func CtxGetFields(ctx context.Context) (fields []zap.Field) {
type CtxLogger struct { type CtxLogger struct {
*zap.Logger *zap.Logger
name string
} }
func (cl CtxLogger) DebugCtx(ctx context.Context, msg string, fields ...zap.Field) { 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 { 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)
} }

View File

@ -8,12 +8,13 @@ import (
) )
var ( var (
mu sync.Mutex mu sync.Mutex
logger *zap.Logger logger *zap.Logger
loggerConfig zap.Config loggerConfig zap.Config
namedLevels = make(map[string]zap.AtomicLevel) namedLevels = make(map[string]zap.AtomicLevel)
namedGlobs = make(map[string]glob.Glob) namedGlobs = make(map[string]glob.Glob)
namedLoggers = make(map[string]CtxLogger) namedLoggers = make(map[string]CtxLogger)
namedSugarLoggers = make(map[string]*zap.SugaredLogger)
) )
func init() { func init() {
@ -59,10 +60,18 @@ func SetNamedLevels(l map[string]zap.AtomicLevel) {
for name, nl := range namedLoggers { for name, nl := range namedLoggers {
level := getLevel(name) level := getLevel(name)
// this can be racy, but newCore := zap.New(logger.Core()).Named(name).WithOptions(
nl.Logger = zap.New(logger.Core()).WithOptions(
zap.IncreaseLevel(level), 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) level := getLevel(name)
l := zap.New(logger.Core()).WithOptions( l := zap.New(logger.Core()).Named(name).WithOptions(zap.IncreaseLevel(level),
zap.IncreaseLevel(level), zap.Fields(fields...))
).Named(name)
ctxL := CtxLogger{l} ctxL := CtxLogger{Logger: l, name: name}
namedLoggers[name] = ctxL namedLoggers[name] = ctxL
return 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
}

View File

@ -4,8 +4,10 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"hash/fnv"
"github.com/anytypeio/any-sync/app/logger" "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/commonspace/object/keychain"
"github.com/anytypeio/any-sync/util/keys" "github.com/anytypeio/any-sync/util/keys"
"github.com/anytypeio/any-sync/util/keys/asymmetric/encryptionkey" "github.com/anytypeio/any-sync/util/keys/asymmetric/encryptionkey"
@ -13,10 +15,9 @@ import (
"github.com/anytypeio/any-sync/util/keys/symmetric" "github.com/anytypeio/any-sync/util/keys/symmetric"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"go.uber.org/zap" "go.uber.org/zap"
"hash/fnv"
) )
var log = logger.NewNamed("acllist").Sugar() var log = logger.NewNamedSugared("acllist")
var ( var (
ErrNoSuchUser = errors.New("no such user") ErrNoSuchUser = errors.New("no such user")

View File

@ -4,15 +4,16 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"time"
"github.com/anytypeio/any-sync/app/logger" "github.com/anytypeio/any-sync/app/logger"
"github.com/anytypeio/any-sync/commonspace/object/tree/treestorage" "github.com/anytypeio/any-sync/commonspace/object/tree/treestorage"
"github.com/anytypeio/any-sync/util/slice" "github.com/anytypeio/any-sync/util/slice"
"go.uber.org/zap" "go.uber.org/zap"
"time"
) )
var ( var (
log = logger.NewNamed("acltree").Sugar() log = logger.NewNamedSugared("acltree")
ErrEmpty = errors.New("logs empty") ErrEmpty = errors.New("logs empty")
) )