mirror of
https://github.com/openimsdk/open-im-server.git
synced 2025-04-24 10:22:36 +08:00
zap
This commit is contained in:
parent
7818bb100d
commit
682eda06b2
@ -160,8 +160,9 @@ log:
|
|||||||
rotationTime: 24
|
rotationTime: 24
|
||||||
remainRotationCount: 2 #日志数量
|
remainRotationCount: 2 #日志数量
|
||||||
#日志级别 6表示全都打印,测试阶段建议设置为6
|
#日志级别 6表示全都打印,测试阶段建议设置为6
|
||||||
remainLogLevel: -1
|
remainLogLevel: 6
|
||||||
stderr: true
|
isStdout: true
|
||||||
|
isJson: true
|
||||||
withStack: false
|
withStack: false
|
||||||
elasticSearchSwitch: false
|
elasticSearchSwitch: false
|
||||||
elasticSearchAddr: [ 127.0.0.1:9201 ]
|
elasticSearchAddr: [ 127.0.0.1:9201 ]
|
||||||
|
@ -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)
|
return log.InitFromConfig(name, config.Config.Log.RemainLogLevel, config.Config.Log.IsStdout, config.Config.Log.IsJson)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
rootCmd.Command = c
|
rootCmd.Command = c
|
||||||
|
@ -187,13 +187,13 @@ type config struct {
|
|||||||
RotationTime int `yaml:"rotationTime"`
|
RotationTime int `yaml:"rotationTime"`
|
||||||
RemainRotationCount uint `yaml:"remainRotationCount"`
|
RemainRotationCount uint `yaml:"remainRotationCount"`
|
||||||
RemainLogLevel int `yaml:"remainLogLevel"`
|
RemainLogLevel int `yaml:"remainLogLevel"`
|
||||||
Stderr bool `yaml:"stderr"`
|
IsStdout bool `yaml:"isStdout"`
|
||||||
WithStack bool `yaml:"withStack"`
|
WithStack bool `yaml:"withStack"`
|
||||||
ElasticSearchSwitch bool `yaml:"elasticSearchSwitch"`
|
ElasticSearchSwitch bool `yaml:"elasticSearchSwitch"`
|
||||||
ElasticSearchAddr []string `yaml:"elasticSearchAddr"`
|
ElasticSearchAddr []string `yaml:"elasticSearchAddr"`
|
||||||
ElasticSearchUser string `yaml:"elasticSearchUser"`
|
ElasticSearchUser string `yaml:"elasticSearchUser"`
|
||||||
ElasticSearchPassword string `yaml:"elasticSearchPassword"`
|
ElasticSearchPassword string `yaml:"elasticSearchPassword"`
|
||||||
isJson bool `yaml:"isJson"`
|
IsJson bool `yaml:"isJson"`
|
||||||
}
|
}
|
||||||
ModuleName struct {
|
ModuleName struct {
|
||||||
LongConnSvrName string `yaml:"longConnSvrName"`
|
LongConnSvrName string `yaml:"longConnSvrName"`
|
||||||
|
@ -30,8 +30,8 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// InitFromConfig initializes a Zap-based logger
|
// InitFromConfig initializes a Zap-based logger
|
||||||
func InitFromConfig(name string, logLevel int) error {
|
func InitFromConfig(name string, logLevel int, isStdout bool, isJson bool) error {
|
||||||
l, err := NewZapLogger(logLevel)
|
l, err := NewZapLogger(logLevel, isStdout, isJson)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -59,19 +59,21 @@ type ZapLogger struct {
|
|||||||
zap *zap.SugaredLogger
|
zap *zap.SugaredLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewZapLogger(logLevel int) (*ZapLogger, error) {
|
func NewZapLogger(logLevel int, isStdout bool, isJson bool) (*ZapLogger, error) {
|
||||||
zapConfig := zap.Config{
|
zapConfig := zap.Config{
|
||||||
Level: zap.NewAtomicLevelAt(logLevelMap[logLevel]),
|
Level: zap.NewAtomicLevelAt(logLevelMap[logLevel]),
|
||||||
Encoding: "json",
|
|
||||||
EncoderConfig: zap.NewProductionEncoderConfig(),
|
EncoderConfig: zap.NewProductionEncoderConfig(),
|
||||||
InitialFields: map[string]interface{}{"PID": os.Getegid()},
|
InitialFields: map[string]interface{}{"PID": os.Getegid()},
|
||||||
DisableStacktrace: true,
|
DisableStacktrace: true,
|
||||||
}
|
}
|
||||||
if config.Config.Log.Stderr {
|
if isJson {
|
||||||
zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stderr")
|
zapConfig.Encoding = "json"
|
||||||
|
}
|
||||||
|
if isStdout {
|
||||||
|
zapConfig.OutputPaths = append(zapConfig.OutputPaths, "stdout", "stderr")
|
||||||
}
|
}
|
||||||
zl := &ZapLogger{}
|
zl := &ZapLogger{}
|
||||||
opts, err := zl.cores()
|
opts, err := zl.cores(logLevel, isStdout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -83,7 +85,7 @@ func NewZapLogger(logLevel int) (*ZapLogger, error) {
|
|||||||
return zl, nil
|
return zl, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *ZapLogger) cores() (zap.Option, error) {
|
func (l *ZapLogger) cores(logLevel int, isStdout bool) (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
|
||||||
@ -101,11 +103,11 @@ func (l *ZapLogger) cores() (zap.Option, error) {
|
|||||||
var cores []zapcore.Core
|
var cores []zapcore.Core
|
||||||
if config.Config.Log.StorageLocation != "" {
|
if config.Config.Log.StorageLocation != "" {
|
||||||
cores = []zapcore.Core{
|
cores = []zapcore.Core{
|
||||||
zapcore.NewCore(fileEncoder, writer, zap.NewAtomicLevelAt(zapcore.Level(config.Config.Log.RemainLogLevel))),
|
zapcore.NewCore(fileEncoder, writer, zap.NewAtomicLevelAt(zapcore.Level(logLevel))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if config.Config.Log.Stderr {
|
if isStdout {
|
||||||
cores = append(cores, zapcore.NewCore(fileEncoder, zapcore.Lock(os.Stdout), zap.NewAtomicLevelAt(zapcore.Level(config.Config.Log.RemainLogLevel))))
|
cores = append(cores, zapcore.NewCore(fileEncoder, zapcore.Lock(os.Stdout), zap.NewAtomicLevelAt(zapcore.Level(logLevel))))
|
||||||
}
|
}
|
||||||
return zap.WrapCore(func(c zapcore.Core) zapcore.Core {
|
return zap.WrapCore(func(c zapcore.Core) zapcore.Core {
|
||||||
return zapcore.NewTee(cores...)
|
return zapcore.NewTee(cores...)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user