@@ -0,0 +1,113 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"91porn-server/middleware/requestid"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
var (
|
||||
defaultZap, _ = zap.NewProductionConfig().Build()
|
||||
|
||||
zapLogger *zap.Logger
|
||||
ZapLog *zap.Logger
|
||||
Debug = defaultZap.Debug
|
||||
Info = defaultZap.Info
|
||||
Warn = defaultZap.Warn
|
||||
Error = defaultZap.Error
|
||||
Fatal = defaultZap.Fatal
|
||||
)
|
||||
|
||||
// Options
|
||||
type Options struct {
|
||||
Level string // debug, warn, info, error fatal //default info
|
||||
DisableStack bool `json:"disableStack"`
|
||||
}
|
||||
|
||||
func Init(opts Options) {
|
||||
cfg := zap.NewProductionConfig()
|
||||
cfg.EncoderConfig.EncodeTime = timeEncoder
|
||||
cfg.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||
cfg.Level = zap.NewAtomicLevelAt(zapLevel(opts.Level))
|
||||
cfg.DisableStacktrace = opts.DisableStack
|
||||
//cfg.EncoderConfig.EncodeCaller = zapcore.FullCallerEncoder
|
||||
zapLogger, _ = cfg.Build()
|
||||
ZapLog = zapLogger
|
||||
Debug = zapLogger.Debug
|
||||
Info = zapLogger.Info
|
||||
Warn = zapLogger.Warn
|
||||
Error = zapLogger.Error
|
||||
Fatal = zapLogger.Fatal
|
||||
}
|
||||
|
||||
func DebugX(ctx context.Context, msg string, fs ...Field) {
|
||||
Debug(msg, appendContext(ctx, fs...)...)
|
||||
}
|
||||
|
||||
func InfoX(ctx context.Context, msg string, fs ...Field) {
|
||||
Info(msg, appendContext(ctx, fs...)...)
|
||||
}
|
||||
|
||||
func WarnX(ctx context.Context, msg string, fs ...Field) {
|
||||
Warn(msg, appendContext(ctx, fs...)...)
|
||||
}
|
||||
|
||||
func ErrorX(ctx context.Context, msg string, fs ...Field) {
|
||||
Error(msg, appendContext(ctx, fs...)...)
|
||||
}
|
||||
|
||||
func FatalX(ctx context.Context, msg string, fs ...Field) {
|
||||
Fatal(msg, appendContext(ctx, fs...)...)
|
||||
}
|
||||
|
||||
func timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
||||
enc.AppendString(t.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
|
||||
func zapLevel(level string) zapcore.Level {
|
||||
level = strings.ToLower(level)
|
||||
l := zapcore.InfoLevel
|
||||
switch level {
|
||||
case "debug":
|
||||
l = zapcore.DebugLevel
|
||||
case "info":
|
||||
l = zapcore.InfoLevel
|
||||
case "warn":
|
||||
l = zapcore.WarnLevel
|
||||
case "error":
|
||||
l = zapcore.ErrorLevel
|
||||
case "fatal":
|
||||
l = zapcore.FatalLevel
|
||||
default:
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
type Field = zap.Field
|
||||
|
||||
// Any
|
||||
func Any(key string, value interface{}) Field {
|
||||
return zap.Any(key, value)
|
||||
}
|
||||
|
||||
// E shortcut for Any("err", err)
|
||||
func E(err error) Field {
|
||||
return Any("err", err)
|
||||
}
|
||||
|
||||
// R is a conveinient way to log request id.
|
||||
func R(ctx context.Context) Field {
|
||||
rid := ctx.Value(requestid.ContextKey)
|
||||
return Any("request-id", rid)
|
||||
}
|
||||
|
||||
func appendContext(ctx context.Context, fs ...Field) (fields []Field) {
|
||||
fields = append(fields, R(ctx))
|
||||
fields = append(fields, fs...)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user