Skip to content

Commit

Permalink
Fix log (openimsdk#107)
Browse files Browse the repository at this point in the history
* fix: change log package

* fix: change log package

* fix: log
  • Loading branch information
icey-yu authored Jul 4, 2024
1 parent 24cbb82 commit de5ae29
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 49 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/google/uuid v1.6.0
github.com/jinzhu/copier v0.4.0
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/magefile/mage v1.15.0
github.com/minio/minio-go/v7 v7.0.69
github.com/openimsdk/protocol v0.0.69-alpha.4
Expand All @@ -46,6 +45,7 @@ require (
golang.org/x/image v0.15.0
google.golang.org/grpc v1.62.1
google.golang.org/protobuf v1.33.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
gopkg.in/src-d/go-git.v4 v4.13.1
gopkg.in/yaml.v2 v2.4.0
gorm.io/gorm v1.25.8
Expand All @@ -66,7 +66,6 @@ require (
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/pierrec/lz4/v4 v4.1.21 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
Expand Down Expand Up @@ -102,7 +101,6 @@ require (
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lestrrat-go/strftime v1.0.6 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/minio/sha256-simd v1.0.1 // indirect
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,6 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY=
Expand All @@ -193,12 +191,6 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc h1:RKf14vYWi2ttpEmkA4aQ3j4u9dStX2t4M8UM6qqNsG8=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkLibYKgg+SwmyFU9dF2hn6MdTj4=
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA=
github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ=
github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw=
github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg=
github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
Expand Down Expand Up @@ -417,6 +409,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/src-d/go-billy.v4 v4.3.2 h1:0SQA1pRztfTFx2miS8sA97XvooFeNOmvUenF4o0EcVg=
gopkg.in/src-d/go-billy.v4 v4.3.2/go.mod h1:nDjArDMp+XMs1aFAESLRjfGSgfvoYN0hDfzEk0GjC98=
gopkg.in/src-d/go-git-fixtures.v3 v3.5.0 h1:ivZFOIltbce2Mo8IjzUHAFoq/IylO9WHhNOAJK+LsJg=
Expand Down
121 changes: 83 additions & 38 deletions log/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ import (
"context"
"fmt"
"github.com/openimsdk/tools/utils/stringutil"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"path/filepath"
"time"

rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"github.com/openimsdk/protocol/constant"
"github.com/openimsdk/tools/mcontext"
"go.uber.org/zap"
Expand Down Expand Up @@ -54,20 +54,27 @@ func InitFromConfig(
isStdout bool,
isJson bool,
logLocation string,
rotateCount uint,
rotationTime uint,
rotateTime uint,
maxBackups int,
maxSize int,
maxAge int,
compress bool,
moduleVersion string,
) error {
l, err := NewZapLogger(loggerPrefixName, moduleName, logLevel, isStdout, isJson, logLocation,
rotateCount, rotationTime, moduleVersion)
rotateTime, maxBackups, maxSize, maxAge, compress, moduleVersion)
if err != nil {
return err
}
setPkgLogger(isJson, moduleName, l)
return nil
}

func setPkgLogger(isJson bool, moduleName string, l *ZapLogger) {
pkgLogger = l.WithCallDepth(callDepth)
if isJson {
pkgLogger = pkgLogger.WithName(moduleName)
}
return nil
}

// InitConsoleLogger init osStdout and osStderr.
Expand Down Expand Up @@ -127,7 +134,12 @@ type ZapLogger struct {
moduleName string
moduleVersion string
loggerPrefixName string
logLocation string
rotationTime time.Duration
maxBackups int
maxSize int
maxAge int
compress bool
}

func NewZapLogger(
Expand All @@ -136,8 +148,11 @@ func NewZapLogger(
isStdout bool,
isJson bool,
logLocation string,
rotateCount uint,
rotationTime uint,
maxBackups int,
maxSize int,
maxAge int,
compress bool,
moduleVersion string,
) (*ZapLogger, error) {
zapConfig := zap.Config{
Expand All @@ -149,12 +164,20 @@ func NewZapLogger(
} else {
zapConfig.Encoding = "console"
}
zl := &ZapLogger{level: logLevelMap[logLevel], moduleName: moduleName, loggerPrefixName: loggerPrefixName,
rotationTime: time.Duration(rotationTime) * time.Hour, moduleVersion: moduleVersion}
opts, err := zl.cores(isStdout, isJson, logLocation, rotateCount)
if err != nil {
return nil, err
}
zl := &ZapLogger{
level: logLevelMap[logLevel],
moduleName: moduleName,
moduleVersion: moduleVersion,
loggerPrefixName: loggerPrefixName,
logLocation: logLocation,
rotationTime: time.Duration(rotationTime) * time.Hour,
maxBackups: maxBackups,
maxSize: maxSize,
maxAge: maxAge,
compress: compress,
}
opts := zl.cores(isStdout, isJson)

l, err := zapConfig.Build(opts)
if err != nil {
return nil, err
Expand All @@ -179,18 +202,17 @@ func NewConsoleZapLogger(
zapConfig.Encoding = "console"
}
zl := &ZapLogger{level: logLevelMap[logLevel], moduleName: moduleName, moduleVersion: moduleVersion}
opts, err := zl.consoleCores(outPut, isJson)
if err != nil {
return nil, err
}
opts := zl.consoleCores(outPut, isJson)

l, err := zapConfig.Build(opts)
if err != nil {
return nil, err
}
zl.zap = l.Sugar()
return zl, nil
}
func (l *ZapLogger) cores(isStdout bool, isJson bool, logLocation string, rotateCount uint) (zap.Option, error) {

func (l *ZapLogger) cores(isStdout bool, isJson bool) zap.Option {
c := zap.NewProductionEncoderConfig()
c.EncodeTime = l.timeEncoder
c.EncodeDuration = zapcore.SecondsDurationEncoder
Expand All @@ -211,12 +233,10 @@ func (l *ZapLogger) cores(isStdout bool, isJson bool, logLocation string, rotate
fileEncoder = zapcore.NewConsoleEncoder(c)
}
fileEncoder = &alignEncoder{Encoder: fileEncoder}
writer, err := l.getWriter(logLocation, rotateCount)
if err != nil {
return nil, err
}
writer := l.getWriter()

var cores []zapcore.Core
if logLocation != "" {
if l.logLocation != "" {
cores = []zapcore.Core{
zapcore.NewCore(fileEncoder, writer, zap.NewAtomicLevelAt(l.level)),
}
Expand All @@ -227,10 +247,25 @@ func (l *ZapLogger) cores(isStdout bool, isJson bool, logLocation string, rotate
}
return zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return zapcore.NewTee(cores...)
}), nil
})
}

func (l *ZapLogger) consoleCores(outPut *os.File, isJson bool) (zap.Option, error) {
// setupLogRotation set to rotate according to l.rotationTime
func (l *ZapLogger) setupLogRotation(lumLogger *lumberjack.Logger) {
ticker := time.NewTicker(l.rotationTime)
go func() {
for range ticker.C {
fileName := l.getLogFileName()
lumLogger.Filename = fileName
err := lumLogger.Rotate()
if err != nil {
ZError(context.TODO(), "rotate log field", err)
}
}
}()
}

func (l *ZapLogger) consoleCores(outPut *os.File, isJson bool) zap.Option {
c := zap.NewProductionEncoderConfig()
c.EncodeTime = l.timeEncoder
c.EncodeDuration = zapcore.SecondsDurationEncoder
Expand All @@ -255,7 +290,7 @@ func (l *ZapLogger) consoleCores(outPut *os.File, isJson bool) (zap.Option, erro

return zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return zapcore.NewTee(cores...)
}), nil
})
}

func (l *ZapLogger) customCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
Expand All @@ -278,23 +313,33 @@ func (l *ZapLogger) timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)
enc.AppendString(t.Format(layout))
}

func (l *ZapLogger) getWriter(logLocation string, rorateCount uint) (zapcore.WriteSyncer, error) {
var path string
func (l *ZapLogger) getWriter() zapcore.WriteSyncer {
fileName := l.getLogFileName()
lumberjackLogger := &lumberjack.Logger{
Filename: fileName, // log file path
MaxSize: l.maxSize, // maximum size of each log file (in MB)
MaxBackups: l.maxBackups, // maximum number of retained old log files
MaxAge: l.maxAge, // maximum number of days to retain old log files
Compress: l.compress, // whether compress old log files
}
l.setupLogRotation(lumberjackLogger) // Set to rotate according to time
return zapcore.AddSync(lumberjackLogger)
}

func (l *ZapLogger) getLogFileName() string {
var (
now = time.Now()
timeStr string
)

if l.rotationTime%(time.Hour*time.Duration(hoursPerDay)) == 0 {
path = logLocation + sp + l.loggerPrefixName + ".%Y-%m-%d"
timeStr = now.Format(".2006-01-02")
} else if l.rotationTime%time.Hour == 0 {
path = logLocation + sp + l.loggerPrefixName + ".%Y-%m-%d_%H"
timeStr = now.Format(".2006-01-02_15")
} else {
path = logLocation + sp + l.loggerPrefixName + ".%Y-%m-%d_%H_%M_%S"
}
logf, err := rotatelogs.New(path,
rotatelogs.WithRotationCount(rorateCount),
rotatelogs.WithRotationTime(l.rotationTime),
)
if err != nil {
return nil, err
timeStr = now.Format(".2006-01-02_15_04_05")
}
return zapcore.AddSync(logf), nil
return l.logLocation + sp + l.loggerPrefixName + timeStr
}

func (l *ZapLogger) capitalColorLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
Expand Down

0 comments on commit de5ae29

Please sign in to comment.