fix(zaputil)_: register encoder only once
This commit is contained in:
parent
5dca5c1354
commit
ebac71140f
|
@ -3,7 +3,6 @@ package logutils
|
|||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
@ -123,15 +122,11 @@ func NewZapAdapter(logger log.Logger, enab zapcore.LevelEnabler) zapcore.Core {
|
|||
}
|
||||
}
|
||||
|
||||
var registerOnce sync.Once
|
||||
|
||||
// NewZapLoggerWithAdapter returns a logger forwarding all logs with level info and above.
|
||||
func NewZapLoggerWithAdapter(logger log.Logger) (*zap.Logger, error) {
|
||||
registerOnce.Do(func() {
|
||||
if err := zaputil.RegisterJSONHexEncoder(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
if err := zaputil.RegisterJSONHexEncoder(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
cfg := zap.Config{
|
||||
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
|
||||
|
|
|
@ -1,26 +1,20 @@
|
|||
package tt
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/status-im/status-go/protocol/zaputil"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var registerOnce sync.Once
|
||||
|
||||
// MustCreateTestLogger returns a logger based on the passed flags.
|
||||
func MustCreateTestLogger() *zap.Logger {
|
||||
return MustCreateTestLoggerWithConfig(loggerConfig())
|
||||
}
|
||||
|
||||
func MustCreateTestLoggerWithConfig(cfg zap.Config) *zap.Logger {
|
||||
registerOnce.Do(func() {
|
||||
if err := zaputil.RegisterConsoleHexEncoder(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
if err := zaputil.RegisterConsoleHexEncoder(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
cfg.Encoding = "console-hex"
|
||||
l, err := cfg.Build()
|
||||
if err != nil {
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type jsonHexEncoder struct {
|
||||
|
@ -31,13 +32,22 @@ func (enc *jsonHexEncoder) Clone() zapcore.Encoder {
|
|||
return &jsonHexEncoder{Encoder: encoderClone}
|
||||
}
|
||||
|
||||
var (
|
||||
registerJSONHexEncoderOnce sync.Once
|
||||
registerConsoleHexEncodeOnce sync.Once
|
||||
)
|
||||
|
||||
// RegisterJSONHexEncoder registers a jsonHexEncoder under "json-hex" name.
|
||||
// Later, this name can be used as a value for zap.Config.Encoding to enable
|
||||
// jsonHexEncoder.
|
||||
func RegisterJSONHexEncoder() error {
|
||||
return zap.RegisterEncoder("json-hex", func(cfg zapcore.EncoderConfig) (zapcore.Encoder, error) {
|
||||
return NewJSONHexEncoder(cfg), nil
|
||||
var err error
|
||||
registerJSONHexEncoderOnce.Do(func() {
|
||||
err = zap.RegisterEncoder("json-hex", func(cfg zapcore.EncoderConfig) (zapcore.Encoder, error) {
|
||||
return NewJSONHexEncoder(cfg), nil
|
||||
})
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
type consoleHexEncoder struct {
|
||||
|
@ -61,7 +71,11 @@ func (enc *consoleHexEncoder) Clone() zapcore.Encoder {
|
|||
}
|
||||
|
||||
func RegisterConsoleHexEncoder() error {
|
||||
return zap.RegisterEncoder("console-hex", func(cfg zapcore.EncoderConfig) (zapcore.Encoder, error) {
|
||||
return NewConsoleHexEncoder(cfg), nil
|
||||
var err error
|
||||
registerConsoleHexEncodeOnce.Do(func() {
|
||||
err = zap.RegisterEncoder("console-hex", func(cfg zapcore.EncoderConfig) (zapcore.Encoder, error) {
|
||||
return NewConsoleHexEncoder(cfg), nil
|
||||
})
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue