You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
4.1 KiB
148 lines
4.1 KiB
2 years ago
|
package log
|
||
|
|
||
|
import (
|
||
2 years ago
|
"OpenIM/pkg/common/config"
|
||
2 years ago
|
"OpenIM/pkg/common/constant"
|
||
|
"OpenIM/pkg/common/tracelog"
|
||
|
"context"
|
||
2 years ago
|
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||
|
"path/filepath"
|
||
2 years ago
|
"time"
|
||
|
|
||
|
"go.uber.org/zap"
|
||
|
"go.uber.org/zap/zapcore"
|
||
|
)
|
||
|
|
||
|
var (
|
||
2 years ago
|
pkgLogger Logger = &ZapLogger{}
|
||
2 years ago
|
sp = string(filepath.Separator)
|
||
2 years ago
|
)
|
||
|
|
||
|
// InitFromConfig initializes a Zap-based logger
|
||
2 years ago
|
func InitFromConfig(name string) error {
|
||
2 years ago
|
l, err := NewZapLogger()
|
||
2 years ago
|
if err != nil {
|
||
|
return err
|
||
2 years ago
|
}
|
||
|
pkgLogger = l.WithCallDepth(2).WithName(name)
|
||
2 years ago
|
return nil
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
func ZDebug(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
||
2 years ago
|
pkgLogger.Debug(ctx, msg, keysAndValues...)
|
||
|
}
|
||
|
|
||
2 years ago
|
func ZInfo(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
||
2 years ago
|
pkgLogger.Info(ctx, msg, keysAndValues...)
|
||
|
}
|
||
|
|
||
2 years ago
|
func ZWarn(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
|
||
2 years ago
|
pkgLogger.Warn(ctx, msg, err, keysAndValues...)
|
||
|
}
|
||
|
|
||
2 years ago
|
func ZError(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
|
||
2 years ago
|
pkgLogger.Error(ctx, msg, err, keysAndValues...)
|
||
|
}
|
||
|
|
||
|
type ZapLogger struct {
|
||
|
zap *zap.SugaredLogger
|
||
|
// store original logger without sampling to avoid multiple samplers
|
||
|
SampleDuration time.Duration
|
||
|
SampleInitial int
|
||
|
SampleInterval int
|
||
|
}
|
||
|
|
||
2 years ago
|
func NewZapLogger() (*ZapLogger, error) {
|
||
2 years ago
|
zapConfig := zap.Config{
|
||
2 years ago
|
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
|
||
|
Development: true,
|
||
|
Encoding: "json",
|
||
|
EncoderConfig: zap.NewProductionEncoderConfig(),
|
||
|
}
|
||
|
zl := &ZapLogger{}
|
||
|
if config.Config.Log.Stderr {
|
||
|
zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stderr")
|
||
2 years ago
|
}
|
||
2 years ago
|
l, err := zapConfig.Build(zl.cores())
|
||
2 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
2 years ago
|
zl.zap = l.Sugar()
|
||
|
return zl, nil
|
||
|
}
|
||
|
|
||
|
func timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
||
|
enc.AppendString(t.Format("2006-01-02 15:04:05"))
|
||
|
}
|
||
|
|
||
|
func (l *ZapLogger) cores() zap.Option {
|
||
|
fileEncoder := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
|
||
|
writer := l.getWriter()
|
||
|
var cores []zapcore.Core
|
||
|
if config.Config.Log.StorageLocation != "" {
|
||
|
cores = []zapcore.Core{
|
||
|
zapcore.NewCore(fileEncoder, writer, zapcore.DebugLevel),
|
||
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
return zap.WrapCore(func(c zapcore.Core) zapcore.Core {
|
||
|
return zapcore.NewTee(cores...)
|
||
|
})
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (l *ZapLogger) getWriter() zapcore.WriteSyncer {
|
||
|
logf, _ := rotatelogs.New(config.Config.Log.StorageLocation+sp+"openIM"+"-"+".%Y_%m%d_%H",
|
||
|
rotatelogs.WithLinkName(config.Config.Log.StorageLocation+sp+"openIM"+"-"),
|
||
|
rotatelogs.WithMaxAge(2*24*time.Hour),
|
||
|
rotatelogs.WithRotationTime(time.Minute),
|
||
|
)
|
||
|
return zapcore.AddSync(logf)
|
||
2 years ago
|
}
|
||
|
|
||
|
func (l *ZapLogger) ToZap() *zap.SugaredLogger {
|
||
|
return l.zap
|
||
|
}
|
||
|
|
||
|
func (l *ZapLogger) Debug(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
||
|
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||
|
l.zap.Debugw(msg, keysAndValues...)
|
||
|
}
|
||
|
|
||
|
func (l *ZapLogger) Info(ctx context.Context, msg string, keysAndValues ...interface{}) {
|
||
|
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||
|
l.zap.Infow(msg, keysAndValues...)
|
||
|
}
|
||
|
|
||
|
func (l *ZapLogger) Warn(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
|
||
|
if err != nil {
|
||
|
keysAndValues = append(keysAndValues, "error", err)
|
||
|
}
|
||
|
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||
|
l.zap.Warnw(msg, keysAndValues...)
|
||
|
}
|
||
|
|
||
|
func (l *ZapLogger) Error(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
|
||
|
if err != nil {
|
||
|
keysAndValues = append(keysAndValues, "error", err)
|
||
|
}
|
||
|
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
|
||
|
l.zap.Errorw(msg, keysAndValues...)
|
||
|
}
|
||
|
|
||
|
func (l *ZapLogger) WithValues(keysAndValues ...interface{}) Logger {
|
||
|
dup := *l
|
||
|
dup.zap = l.zap.With(keysAndValues...)
|
||
|
return &dup
|
||
|
}
|
||
|
|
||
|
func (l *ZapLogger) WithName(name string) Logger {
|
||
|
dup := *l
|
||
|
dup.zap = l.zap.Named(name)
|
||
|
return &dup
|
||
|
}
|
||
|
|
||
|
func (l *ZapLogger) WithCallDepth(depth int) Logger {
|
||
|
dup := *l
|
||
|
dup.zap = l.zap.WithOptions(zap.AddCallerSkip(depth))
|
||
|
return &dup
|
||
|
}
|