mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
feat: log add prefixName
This commit is contained in:
parent
67612967f5
commit
c7e684211d
@ -26,7 +26,7 @@ func NewRootCmd(name string) (rootCmd *RootCmd) {
|
|||||||
if err := rootCmd.getConfFromCmdAndInit(cmd); err != nil {
|
if err := rootCmd.getConfFromCmdAndInit(cmd); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if err := log.InitFromConfig(name, config.Config.Log.RemainLogLevel, config.Config.Log.IsStdout, config.Config.Log.IsJson, config.Config.Log.StorageLocation, config.Config.Log.RemainRotationCount); err != nil {
|
if err := log.InitFromConfig("OpenIM.log.all", name, config.Config.Log.RemainLogLevel, config.Config.Log.IsStdout, config.Config.Log.IsJson, config.Config.Log.StorageLocation, config.Config.Log.RemainRotationCount); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -32,14 +32,14 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// InitFromConfig initializes a Zap-based logger
|
// InitFromConfig initializes a Zap-based logger
|
||||||
func InitFromConfig(name string, logLevel int, isStdout bool, isJson bool, logLocation string, rotateCount uint) error {
|
func InitFromConfig(loggerPrefixName, moduleName string, logLevel int, isStdout bool, isJson bool, logLocation string, rotateCount uint) error {
|
||||||
l, err := NewZapLogger(name, logLevel, isStdout, isJson, logLocation, rotateCount)
|
l, err := NewZapLogger(loggerPrefixName, moduleName, logLevel, isStdout, isJson, logLocation, rotateCount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
pkgLogger = l.WithCallDepth(2)
|
pkgLogger = l.WithCallDepth(2)
|
||||||
if isJson {
|
if isJson {
|
||||||
pkgLogger = pkgLogger.WithName(name)
|
pkgLogger = pkgLogger.WithName(moduleName)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -64,9 +64,10 @@ type ZapLogger struct {
|
|||||||
zap *zap.SugaredLogger
|
zap *zap.SugaredLogger
|
||||||
level zapcore.Level
|
level zapcore.Level
|
||||||
loggerName string
|
loggerName string
|
||||||
|
loggerPrefixName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewZapLogger(loggerName string, logLevel int, isStdout bool, isJson bool, logLocation string, rotateCount uint) (*ZapLogger, error) {
|
func NewZapLogger(loggerPrefixName, loggerName string, logLevel int, isStdout bool, isJson bool, logLocation string, rotateCount uint) (*ZapLogger, error) {
|
||||||
zapConfig := zap.Config{
|
zapConfig := zap.Config{
|
||||||
Level: zap.NewAtomicLevelAt(logLevelMap[logLevel]),
|
Level: zap.NewAtomicLevelAt(logLevelMap[logLevel]),
|
||||||
// EncoderConfig: zap.NewProductionEncoderConfig(),
|
// EncoderConfig: zap.NewProductionEncoderConfig(),
|
||||||
@ -81,7 +82,7 @@ func NewZapLogger(loggerName string, logLevel int, isStdout bool, isJson bool, l
|
|||||||
// if isStdout {
|
// if isStdout {
|
||||||
// zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stdout", "stderr")
|
// zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stdout", "stderr")
|
||||||
// }
|
// }
|
||||||
zl := &ZapLogger{level: logLevelMap[logLevel], loggerName: loggerName}
|
zl := &ZapLogger{level: logLevelMap[logLevel], loggerName: loggerName, loggerPrefixName: loggerPrefixName}
|
||||||
opts, err := zl.cores(isStdout, isJson, logLocation, rotateCount)
|
opts, err := zl.cores(isStdout, isJson, logLocation, rotateCount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -158,7 +159,7 @@ func (l *ZapLogger) timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *ZapLogger) getWriter(logLocation string, rorateCount uint) (zapcore.WriteSyncer, error) {
|
func (l *ZapLogger) getWriter(logLocation string, rorateCount uint) (zapcore.WriteSyncer, error) {
|
||||||
logf, err := rotatelogs.New(logLocation+sp+"OpenIM.log.all"+".%Y-%m-%d",
|
logf, err := rotatelogs.New(logLocation+sp+l.loggerPrefixName+".%Y-%m-%d",
|
||||||
rotatelogs.WithRotationCount(rorateCount),
|
rotatelogs.WithRotationCount(rorateCount),
|
||||||
rotatelogs.WithRotationTime(time.Duration(config.Config.Log.RotationTime)*time.Hour),
|
rotatelogs.WithRotationTime(time.Duration(config.Config.Log.RotationTime)*time.Hour),
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user