This commit is contained in:
wangchuxiao 2023-03-10 19:53:08 +08:00
parent 7a248377f0
commit fd186e9e82
2 changed files with 48 additions and 61 deletions

View File

@ -1,23 +1,10 @@
package log package log
type Config struct { var logMap = map[int]int{
JSON bool `yaml:"json"` 6: -1,
Level string `yaml:"level"` 5: 0,
// true to enable log sampling, where the same log message and level will be throttled. 4: 1,
// we have two layers of sampling 3: 2,
// 1. global sampling - within a second, it will log the first SampleInitial, then every SampleInterval messages. 2: 3,
// 2. per participant/track sampling - to be used with Logger.WithItemSampler(). This would be used to throttle 1: 4,
// the logs for a particular participant/track.
Sample bool `yaml:"sample,omitempty"`
// global sampling per server
// when sampling, the first N logs will be logged
SampleInitial int `yaml:"sample_initial,omitempty"`
// when sampling, every Mth log will be logged
SampleInterval int `yaml:"sample_interval,omitempty"`
// participant/track level sampling
ItemSampleSeconds int `yaml:"item_sample_seconds,omitempty"`
ItemSampleInitial int `yaml:"item_sample_initial,omitempty"`
ItemSampleInterval int `yaml:"item_sample_interval,omitempty"`
} }

View File

@ -1,6 +1,7 @@
package log package log
import ( import (
"OpenIM/pkg/common/config"
"OpenIM/pkg/common/constant" "OpenIM/pkg/common/constant"
"OpenIM/pkg/common/tracelog" "OpenIM/pkg/common/tracelog"
"context" "context"
@ -18,8 +19,12 @@ var (
) )
// InitFromConfig initializes a Zap-based logger // InitFromConfig initializes a Zap-based logger
func InitFromConfig(conf Config, name string) { func InitFromConfig(name string) {
l, err := NewZapLogger(&conf) //var c zap.Config
//file, _ := os.Create(config.Config.Log.StorageLocation)
//writeSyncer := zapcore.AddSync(file)
l, err := NewZapLogger()
if err == nil { if err == nil {
setLogger(l, name) setLogger(l, name)
} }
@ -83,55 +88,50 @@ type ZapLogger struct {
SampleInterval int SampleInterval int
} }
func NewZapLogger(conf *Config) (*ZapLogger, error) { func NewZapLogger() (*ZapLogger, error) {
lvl := ParseZapLevel(conf.Level)
zapConfig := zap.Config{ zapConfig := zap.Config{
Level: zap.NewAtomicLevelAt(lvl), Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
Development: false, Development: true,
Encoding: "console", Encoding: "json",
EncoderConfig: zap.NewDevelopmentEncoderConfig(), EncoderConfig: zap.NewProductionEncoderConfig(),
OutputPaths: []string{"stderr"}, OutputPaths: []string{config.Config.Log.StorageLocation},
ErrorOutputPaths: []string{"stderr"}, ErrorOutputPaths: []string{"stderr"},
} }
if conf.JSON {
zapConfig.Encoding = "json"
zapConfig.EncoderConfig = zap.NewProductionEncoderConfig()
}
l, err := zapConfig.Build() l, err := zapConfig.Build()
if err != nil { if err != nil {
return nil, err return nil, err
} }
zl := &ZapLogger{ zl := &ZapLogger{
unsampled: l.Sugar(), unsampled: l.Sugar(),
SampleDuration: time.Duration(conf.ItemSampleSeconds) * time.Second, //SampleDuration: time.Duration(conf.ItemSampleSeconds) * time.Second,
SampleInitial: conf.ItemSampleInitial, //SampleInitial: conf.ItemSampleInitial,
SampleInterval: conf.ItemSampleInterval, //SampleInterval: conf.ItemSampleInterval,
} }
if conf.Sample { //if conf.Sample {
// use a sampling logger for the main logger // // use a sampling logger for the main logger
samplingConf := &zap.SamplingConfig{ // samplingConf := &zap.SamplingConfig{
Initial: conf.SampleInitial, // Initial: conf.SampleInitial,
Thereafter: conf.SampleInterval, // Thereafter: conf.SampleInterval,
} // }
// sane defaults // // sane defaults
if samplingConf.Initial == 0 { // if samplingConf.Initial == 0 {
samplingConf.Initial = 20 // samplingConf.Initial = 20
} // }
if samplingConf.Thereafter == 0 { // if samplingConf.Thereafter == 0 {
samplingConf.Thereafter = 100 // samplingConf.Thereafter = 100
} // }
zl.zap = l.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { // zl.zap = l.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewSamplerWithOptions( // return zapcore.NewSamplerWithOptions(
core, // core,
time.Second, // time.Second,
samplingConf.Initial, // samplingConf.Initial,
samplingConf.Thereafter, // samplingConf.Thereafter,
) // )
})).Sugar() // })).Sugar()
} else { //} else {
zl.zap = zl.unsampled // zl.zap = zl.unsampled
} //}
return zl, nil return zl, nil
} }