mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-06 04:15:46 +08:00
log
This commit is contained in:
parent
0d3a1ba6f6
commit
c81d958b78
@ -26,7 +26,7 @@ func NewRootCmd(name string) (rootCmd *RootCmd) {
|
|||||||
if err := rootCmd.getConfFromCmdAndInit(cmd); err != nil {
|
if err := rootCmd.getConfFromCmdAndInit(cmd); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return log.InitFromConfig(name, config.Config.Log.RemainLogLevel, config.Config.Log.IsStdout, config.Config.Log.IsJson)
|
return log.InitFromConfig(name, config.Config.Log.RemainLogLevel, config.Config.Log.IsStdout, config.Config.Log.IsJson, config.Config.Log.StorageLocation, config.Config.Log.RemainRotationCount)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
rootCmd.Command = c
|
rootCmd.Command = c
|
||||||
|
@ -31,8 +31,8 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// InitFromConfig initializes a Zap-based logger
|
// InitFromConfig initializes a Zap-based logger
|
||||||
func InitFromConfig(name string, logLevel int, isStdout bool, isJson bool) error {
|
func InitFromConfig(name string, logLevel int, isStdout bool, isJson bool, logLocation string, rotateCount uint) error {
|
||||||
l, err := NewZapLogger(logLevel, isStdout, isJson)
|
l, err := NewZapLogger(logLevel, isStdout, isJson, logLocation, rotateCount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -60,7 +60,7 @@ type ZapLogger struct {
|
|||||||
zap *zap.SugaredLogger
|
zap *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewZapLogger(logLevel int, isStdout bool, isJson bool) (*ZapLogger, error) {
|
func NewZapLogger(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(),
|
||||||
@ -76,7 +76,7 @@ func NewZapLogger(logLevel int, isStdout bool, isJson bool) (*ZapLogger, error)
|
|||||||
// zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stdout", "stderr")
|
// zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stdout", "stderr")
|
||||||
// }
|
// }
|
||||||
zl := &ZapLogger{}
|
zl := &ZapLogger{}
|
||||||
opts, err := zl.cores(logLevel, isStdout, isJson)
|
opts, err := zl.cores(logLevel, isStdout, isJson, logLocation, rotateCount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -88,11 +88,10 @@ func NewZapLogger(logLevel int, isStdout bool, isJson bool) (*ZapLogger, error)
|
|||||||
return zl, nil
|
return zl, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ZapLogger) cores(logLevel int, isStdout bool, isJson bool) (zap.Option, error) {
|
func (l *ZapLogger) cores(logLevel int, isStdout bool, isJson bool, logLocation string, rotateCount uint) (zap.Option, error) {
|
||||||
c := zap.NewProductionEncoderConfig()
|
c := zap.NewProductionEncoderConfig()
|
||||||
c.EncodeTime = zapcore.ISO8601TimeEncoder
|
c.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||||
c.EncodeDuration = zapcore.SecondsDurationEncoder
|
c.EncodeDuration = zapcore.SecondsDurationEncoder
|
||||||
|
|
||||||
c.MessageKey = "msg"
|
c.MessageKey = "msg"
|
||||||
c.LevelKey = "level"
|
c.LevelKey = "level"
|
||||||
c.TimeKey = "time"
|
c.TimeKey = "time"
|
||||||
@ -106,17 +105,17 @@ func (l *ZapLogger) cores(logLevel int, isStdout bool, isJson bool) (zap.Option,
|
|||||||
fileEncoder = zapcore.NewConsoleEncoder(c)
|
fileEncoder = zapcore.NewConsoleEncoder(c)
|
||||||
}
|
}
|
||||||
fileEncoder.AddInt("PID", os.Getpid())
|
fileEncoder.AddInt("PID", os.Getpid())
|
||||||
writer, err := l.getWriter()
|
writer, err := l.getWriter(logLocation, rotateCount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var cores []zapcore.Core
|
var cores []zapcore.Core
|
||||||
if config.Config.Log.StorageLocation != "" && !isStdout {
|
if logLocation != "" && !isStdout {
|
||||||
cores = []zapcore.Core{
|
cores = []zapcore.Core{
|
||||||
zapcore.NewCore(fileEncoder, writer, zap.NewAtomicLevelAt(logLevelMap[logLevel])),
|
zapcore.NewCore(fileEncoder, writer, zap.NewAtomicLevelAt(logLevelMap[logLevel])),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if config.Config.Log.StorageLocation == "" && !isStdout {
|
if logLocation == "" && !isStdout {
|
||||||
return nil, errors.New("log storage location is empty and not stdout")
|
return nil, errors.New("log storage location is empty and not stdout")
|
||||||
}
|
}
|
||||||
if isStdout {
|
if isStdout {
|
||||||
@ -127,9 +126,9 @@ func (l *ZapLogger) cores(logLevel int, isStdout bool, isJson bool) (zap.Option,
|
|||||||
}), nil
|
}), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ZapLogger) getWriter() (zapcore.WriteSyncer, error) {
|
func (l *ZapLogger) getWriter(logLocation string, rorateCount uint) (zapcore.WriteSyncer, error) {
|
||||||
logf, err := rotatelogs.New(config.Config.Log.StorageLocation+sp+"OpenIM.log.all"+".%Y-%m-%d",
|
logf, err := rotatelogs.New(logLocation+sp+"OpenIM.log.all"+".%Y-%m-%d",
|
||||||
rotatelogs.WithRotationCount(config.Config.Log.RemainRotationCount),
|
rotatelogs.WithRotationCount(rorateCount),
|
||||||
rotatelogs.WithRotationTime(time.Duration(config.Config.Log.RotationTime)*time.Hour),
|
rotatelogs.WithRotationTime(time.Duration(config.Config.Log.RotationTime)*time.Hour),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user