2019-07-17 22:25:42 +00:00
|
|
|
package logutils
|
|
|
|
|
|
|
|
import (
|
2024-11-04 08:09:28 +00:00
|
|
|
"io"
|
2020-02-18 11:21:01 +00:00
|
|
|
"sync"
|
2024-11-04 08:09:28 +00:00
|
|
|
"time"
|
2020-02-18 11:21:01 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2024-11-04 08:09:28 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
2020-02-18 11:21:01 +00:00
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2024-11-04 08:09:28 +00:00
|
|
|
"github.com/status-im/status-go/protocol/zaputil"
|
2019-07-17 22:25:42 +00:00
|
|
|
)
|
|
|
|
|
2020-02-18 11:21:01 +00:00
|
|
|
var (
|
|
|
|
_zapLogger *zap.Logger
|
|
|
|
_initZapLogger sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
func ZapLogger() *zap.Logger {
|
|
|
|
_initZapLogger.Do(func() {
|
2024-11-04 08:09:28 +00:00
|
|
|
_zapLogger = defaultLogger()
|
|
|
|
|
|
|
|
// forward geth logs to zap logger
|
|
|
|
_gethLogger := _zapLogger.Named("geth")
|
|
|
|
log.Root().SetHandler(gethAdapter(_gethLogger))
|
2020-02-18 11:21:01 +00:00
|
|
|
})
|
|
|
|
return _zapLogger
|
|
|
|
}
|
2024-11-04 08:09:28 +00:00
|
|
|
|
|
|
|
func defaultLogger() *zap.Logger {
|
|
|
|
core := NewCore(
|
|
|
|
defaultEncoder(),
|
|
|
|
zapcore.AddSync(io.Discard),
|
|
|
|
zap.NewAtomicLevelAt(zap.InfoLevel),
|
|
|
|
)
|
|
|
|
return zap.New(core, zap.AddCaller())
|
|
|
|
}
|
|
|
|
|
|
|
|
func defaultEncoder() zapcore.Encoder {
|
|
|
|
encoderConfig := zap.NewDevelopmentEncoderConfig()
|
|
|
|
encoderConfig.EncodeTime = utcTimeEncoder(encoderConfig.EncodeTime)
|
|
|
|
|
|
|
|
return zaputil.NewConsoleHexEncoder(encoderConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
func utcTimeEncoder(encoder zapcore.TimeEncoder) zapcore.TimeEncoder {
|
|
|
|
return func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
|
|
|
encoder(t.UTC(), enc)
|
|
|
|
}
|
|
|
|
}
|