Browse Source

update: more zap logger configure

dev
Dnomd343 1 year ago
parent
commit
c23ba6310e
  1. 4
      go.mod
  2. 6
      go.sum
  3. 107
      next/logger/logger.go

4
go.mod

@ -14,6 +14,8 @@ require (
)
require (
github.com/gookit/color v1.5.4 // indirect
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/sys v0.10.0 // indirect
)

6
go.sum

@ -3,18 +3,24 @@ github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHG
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-http-utils/headers v0.0.0-20181008091004-fed159eddc2a/go.mod h1:I79BieaU4fxrw4LMXby6q5OS9XnoR9UIKLOzDFjUmuw=
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w=
github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c=
go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

107
next/logger/logger.go

@ -1,17 +1,112 @@
package logger
import "go.uber.org/zap"
import (
"fmt"
"go.uber.org/zap"
"os"
"time"
)
import "go.uber.org/zap/zapcore"
var Logger *zap.SugaredLogger
const (
logTmFmt = "2006-01-02 15:04:05"
)
func init() {
log, err := zap.NewDevelopment()
if err != nil {
panic("log utils init failed")
}
//coreConfig := zapcore.EncoderConfig{
// TimeKey: "ts",
// LevelKey: "level",
// NameKey: "logger",
// CallerKey: "caller",
// FunctionKey: zapcore.OmitKey,
// MessageKey: "msg",
// StacktraceKey: "stacktrace",
// LineEnding: zapcore.DefaultLineEnding, // 默认换行符"\n"
// EncodeLevel: zapcore.CapitalColorLevelEncoder,
// EncodeTime: zapcore.RFC3339TimeEncoder, // 日志时间格式显示
// EncodeDuration: zapcore.MillisDurationEncoder, // 时间序列化,Duration为经过的浮点秒数
// EncodeCaller: zapcore.ShortCallerEncoder, // 日志行号显示
//}
//
//encoder := zapcore.NewConsoleEncoder(coreConfig)
//
//newCore := zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), zapcore.DebugLevel)
//
//logger := zap.New(newCore)
//
//Logger = logger.Sugar()
GetLogger()
//log, err := zap.NewDevelopment()
//if err != nil {
// panic("log utils init failed")
//}
// TODO: more zap logger configure
// TODO: reserve raw logger handle
Logger = log.Sugar()
//Logger = log.Sugar()
}
func GetLogger() {
config := zapcore.EncoderConfig{
MessageKey: "M",
LevelKey: "L",
TimeKey: "T",
NameKey: "N",
CallerKey: "C",
FunctionKey: "F",
StacktraceKey: "S",
SkipLineEnding: false,
LineEnding: "!!!\n",
EncodeLevel: logEncodeLevel,
EncodeTime: logEncodeTime,
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: logEncodeCaller,
EncodeName: logEncodeName,
ConsoleSeparator: "$$",
}
//var zc = zap.Config{
// Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
// Development: false,
// DisableCaller: false,
// DisableStacktrace: false,
// Sampling: nil,
// Encoding: "json",
// EncoderConfig: config,
// OutputPaths: []string{"stdout"},
// ErrorOutputPaths: []string{"stderr"},
// InitialFields: map[string]interface{}{"app": "zapdex"},
//}
newCore := zapcore.NewCore(zapcore.NewConsoleEncoder(config), zapcore.Lock(os.Stderr), zapcore.DebugLevel)
//newCore := zapcore.NewCore(zapcore.NewJSONEncoder(config), zapcore.Lock(os.Stderr), zapcore.DebugLevel)
logger := zap.New(newCore, zap.AddCaller())
//logger, _ := zc.Build()
zap.ReplaceGlobals(logger)
Logger = logger.Sugar()
}
func logEncodeLevel(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString("[" + level.CapitalString() + "]")
}
func logEncodeTime(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString("[[" + t.Format(logTmFmt) + "]]")
}
func logEncodeCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString("{" + caller.TrimmedPath() + "}")
}
func logEncodeName(loggerName string, enc zapcore.PrimitiveArrayEncoder) {
fmt.Println(loggerName)
enc.AppendString("{{" + loggerName + "}}")
}

Loading…
Cancel
Save