Files
open-im-server/pkg/common/log/zap.go
T

179 lines
5.1 KiB
Go
Raw Normal View History

2023-03-10 18:37:36 +08:00
package log
import (
"context"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tracelog"
2023-03-13 12:34:56 +08:00
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
2023-03-14 11:09:55 +08:00
"os"
2023-03-13 12:34:56 +08:00
"path/filepath"
2023-03-10 18:37:36 +08:00
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
2023-03-10 20:46:06 +08:00
pkgLogger Logger = &ZapLogger{}
2023-03-13 12:34:56 +08:00
sp = string(filepath.Separator)
2023-03-10 18:37:36 +08:00
)
// InitFromConfig initializes a Zap-based logger
2023-03-10 20:46:06 +08:00
func InitFromConfig(name string) error {
2023-03-10 19:53:08 +08:00
l, err := NewZapLogger()
2023-03-10 20:46:06 +08:00
if err != nil {
return err
2023-03-10 18:37:36 +08:00
}
pkgLogger = l.WithCallDepth(2).WithName(name)
2023-03-10 20:46:06 +08:00
return nil
2023-03-10 18:37:36 +08:00
}
2023-03-13 12:34:56 +08:00
func ZDebug(ctx context.Context, msg string, keysAndValues ...interface{}) {
2023-03-10 18:37:36 +08:00
pkgLogger.Debug(ctx, msg, keysAndValues...)
}
2023-03-13 12:34:56 +08:00
func ZInfo(ctx context.Context, msg string, keysAndValues ...interface{}) {
2023-03-10 18:37:36 +08:00
pkgLogger.Info(ctx, msg, keysAndValues...)
}
2023-03-13 12:34:56 +08:00
func ZWarn(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
2023-03-10 18:37:36 +08:00
pkgLogger.Warn(ctx, msg, err, keysAndValues...)
}
2023-03-13 12:34:56 +08:00
func ZError(ctx context.Context, msg string, err error, keysAndValues ...interface{}) {
2023-03-10 18:37:36 +08:00
pkgLogger.Error(ctx, msg, err, keysAndValues...)
}
type ZapLogger struct {
zap *zap.SugaredLogger
}
2023-03-10 19:53:08 +08:00
func NewZapLogger() (*ZapLogger, error) {
2023-03-10 18:37:36 +08:00
zapConfig := zap.Config{
Level: zap.NewAtomicLevelAt(zapcore.Level(config.Config.Log.RemainLogLevel)),
2023-03-14 19:41:26 +08:00
Encoding: "json",
EncoderConfig: zap.NewProductionEncoderConfig(),
InitialFields: map[string]interface{}{"PID": os.Getegid()},
DisableStacktrace: true,
2023-03-13 12:34:56 +08:00
}
if config.Config.Log.Stderr {
zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stderr")
2023-03-10 18:37:36 +08:00
}
zl := &ZapLogger{}
2023-03-13 17:57:52 +08:00
opts, err := zl.cores()
if err != nil {
return nil, err
}
l, err := zapConfig.Build(opts)
2023-03-10 18:37:36 +08:00
if err != nil {
return nil, err
}
2023-03-13 12:34:56 +08:00
zl.zap = l.Sugar()
return zl, nil
}
2023-03-13 17:57:52 +08:00
func (l *ZapLogger) cores() (zap.Option, error) {
2023-03-14 12:33:26 +08:00
c := zap.NewProductionEncoderConfig()
2023-03-14 13:32:35 +08:00
c.EncodeTime = zapcore.ISO8601TimeEncoder
2023-03-14 13:26:49 +08:00
c.EncodeDuration = zapcore.SecondsDurationEncoder
2023-03-16 15:10:23 +08:00
c.EncodeLevel = zapcore.CapitalLevelEncoder
2023-03-14 18:07:32 +08:00
c.MessageKey = "msg"
c.LevelKey = "level"
c.TimeKey = "time"
c.CallerKey = "caller"
2023-03-14 17:51:11 +08:00
fileEncoder := zapcore.NewJSONEncoder(c)
2023-03-14 12:33:26 +08:00
fileEncoder.AddInt("PID", os.Getpid())
2023-03-13 17:57:52 +08:00
writer, err := l.getWriter()
if err != nil {
return nil, err
}
2023-03-13 12:34:56 +08:00
var cores []zapcore.Core
if config.Config.Log.StorageLocation != "" {
cores = []zapcore.Core{
zapcore.NewCore(fileEncoder, writer, zap.NewAtomicLevelAt(zapcore.Level(config.Config.Log.RemainLogLevel))),
2023-03-13 12:34:56 +08:00
}
2023-03-10 18:37:36 +08:00
}
2023-03-14 14:35:31 +08:00
if config.Config.Log.Stderr {
cores = append(cores, zapcore.NewCore(fileEncoder, zapcore.Lock(os.Stdout), zap.NewAtomicLevelAt(zapcore.Level(config.Config.Log.RemainLogLevel))))
2023-03-14 14:35:31 +08:00
}
2023-03-13 12:34:56 +08:00
return zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return zapcore.NewTee(cores...)
2023-03-13 17:57:52 +08:00
}), nil
2023-03-13 12:34:56 +08:00
}
2023-03-10 18:37:36 +08:00
2023-03-13 17:57:52 +08:00
func (l *ZapLogger) getWriter() (zapcore.WriteSyncer, error) {
logf, err := rotatelogs.New(config.Config.Log.StorageLocation+sp+"OpenIM.log.all"+".%Y-%m-%d",
rotatelogs.WithRotationCount(config.Config.Log.RemainRotationCount),
rotatelogs.WithRotationTime(time.Duration(config.Config.Log.RotationTime)*time.Hour),
2023-03-13 12:34:56 +08:00
)
2023-03-13 17:57:52 +08:00
if err != nil {
return nil, err
}
return zapcore.AddSync(logf), nil
2023-03-10 18:37:36 +08:00
}
func (l *ZapLogger) ToZap() *zap.SugaredLogger {
return l.zap
}
func (l *ZapLogger) Debug(ctx context.Context, msg string, keysAndValues ...interface{}) {
2023-03-14 11:09:55 +08:00
keysAndValues = l.kvAppend(ctx, keysAndValues)
2023-03-10 18:37:36 +08:00
l.zap.Debugw(msg, keysAndValues...)
}
func (l *ZapLogger) Info(ctx context.Context, msg string, keysAndValues ...interface{}) {
2023-03-14 11:09:55 +08:00
keysAndValues = l.kvAppend(ctx, keysAndValues)
2023-03-10 18:37:36 +08:00
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.Error())
2023-03-10 18:37:36 +08:00
}
2023-03-14 11:09:55 +08:00
keysAndValues = l.kvAppend(ctx, keysAndValues)
2023-03-10 18:37:36 +08:00
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.Error())
2023-03-10 18:37:36 +08:00
}
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
l.zap.Errorw(msg, keysAndValues...)
}
2023-03-14 11:09:55 +08:00
func (l *ZapLogger) kvAppend(ctx context.Context, keysAndValues []interface{}) []interface{} {
2023-03-14 12:07:25 +08:00
operationID := tracelog.GetOperationID(ctx)
opUserID := tracelog.GetOpUserID(ctx)
2023-03-14 15:35:58 +08:00
connID := tracelog.GetConnID(ctx)
2023-03-14 12:07:25 +08:00
if opUserID != "" {
keysAndValues = append([]interface{}{constant.OpUserID, tracelog.GetOpUserID(ctx)}, keysAndValues...)
}
if operationID != "" {
keysAndValues = append([]interface{}{constant.OperationID, tracelog.GetOperationID(ctx)}, keysAndValues...)
}
2023-03-14 15:35:58 +08:00
if connID != "" {
keysAndValues = append([]interface{}{constant.ConnID, tracelog.GetConnID(ctx)}, keysAndValues...)
}
2023-03-14 11:09:55 +08:00
return keysAndValues
}
2023-03-10 18:37:36 +08:00
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
}