57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
package logger
|
|
|
|
import (
|
|
"go.uber.org/atomic"
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap/zapcore"
|
|
"os"
|
|
)
|
|
|
|
var Log *WailsZapLoggerAdaptor
|
|
|
|
var sugar *zap.SugaredLogger
|
|
var logger *zap.Logger
|
|
var level = &atomic.String{}
|
|
|
|
func init() {
|
|
SetLevel(zapcore.DebugLevel)
|
|
encoder := zapcore.NewConsoleEncoder(DefaultEncoderConfig())
|
|
multiWriteSyncer := zapcore.NewMultiWriteSyncer(DefaultConsoleSyncer())
|
|
|
|
core := zapcore.NewCore(encoder, multiWriteSyncer, DefaultLevelEnabler())
|
|
logger = zap.New(core, zap.AddCaller())
|
|
defer func() {
|
|
_ = logger.Sync()
|
|
}()
|
|
sugar = logger.Sugar()
|
|
|
|
Log = &WailsZapLoggerAdaptor{logger: sugar}
|
|
}
|
|
|
|
func DefaultTimeEncoder() (timeEncoder zapcore.TimeEncoder) {
|
|
timeEncoder = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05.000")
|
|
return
|
|
}
|
|
|
|
func SetLevel(l zapcore.Level) {
|
|
level.Store(l.String())
|
|
}
|
|
|
|
func DefaultLevelEnabler() zap.LevelEnablerFunc {
|
|
return func(z zapcore.Level) bool {
|
|
l, _ := zapcore.ParseLevel(level.Load())
|
|
return z >= l
|
|
}
|
|
}
|
|
|
|
func DefaultEncoderConfig() (encoderConfig zapcore.EncoderConfig) {
|
|
encoderConfig = zap.NewProductionEncoderConfig()
|
|
encoderConfig.EncodeTime = DefaultTimeEncoder()
|
|
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
|
return
|
|
}
|
|
|
|
func DefaultConsoleSyncer() zapcore.WriteSyncer {
|
|
return zapcore.AddSync(os.Stdout)
|
|
}
|