Merge remote-tracking branch 'origin/errcode' into errcode

This commit is contained in:
Gordon 2023-03-10 20:05:26 +08:00
commit 1e1fc188b4
3 changed files with 55 additions and 73 deletions

View File

@ -20,17 +20,12 @@ func NewRootCmd() (rootCmd *RootCmd) {
Short: "Start the server", Short: "Start the server",
Long: `Start the server`, Long: `Start the server`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
log.InitFromConfig(log.Config{ err := rootCmd.getConfFromCmdAndInit(cmd)
JSON: true, if err != nil {
Level: "-1", return err
Sample: false, }
SampleInitial: 0, log.InitFromConfig("newlog")
SampleInterval: 0, return nil
ItemSampleSeconds: 0,
ItemSampleInitial: 0,
ItemSampleInterval: 0,
}, "newlog")
return rootCmd.getConfFromCmdAndInit(cmd)
}, },
} }
rootCmd.Command = c rootCmd.Command = c

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{config.Config.Log.StorageLocation},
}
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
} }