From fd186e9e828802034ea5f16afdb0d4a85460d2e9 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Fri, 10 Mar 2023 19:53:08 +0800 Subject: [PATCH 1/3] fix cron --- pkg/common/logger/config.go | 27 ++++-------- pkg/common/logger/log.go | 82 ++++++++++++++++++------------------- 2 files changed, 48 insertions(+), 61 deletions(-) diff --git a/pkg/common/logger/config.go b/pkg/common/logger/config.go index 0b6eff1bd..58d1385d8 100644 --- a/pkg/common/logger/config.go +++ b/pkg/common/logger/config.go @@ -1,23 +1,10 @@ package log -type Config struct { - JSON bool `yaml:"json"` - Level string `yaml:"level"` - // true to enable log sampling, where the same log message and level will be throttled. - // we have two layers of sampling - // 1. global sampling - within a second, it will log the first SampleInitial, then every SampleInterval messages. - // 2. per participant/track sampling - to be used with Logger.WithItemSampler(). This would be used to throttle - // 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"` +var logMap = map[int]int{ + 6: -1, + 5: 0, + 4: 1, + 3: 2, + 2: 3, + 1: 4, } diff --git a/pkg/common/logger/log.go b/pkg/common/logger/log.go index b7ea56448..9ed02e903 100644 --- a/pkg/common/logger/log.go +++ b/pkg/common/logger/log.go @@ -1,6 +1,7 @@ package log import ( + "OpenIM/pkg/common/config" "OpenIM/pkg/common/constant" "OpenIM/pkg/common/tracelog" "context" @@ -18,8 +19,12 @@ var ( ) // InitFromConfig initializes a Zap-based logger -func InitFromConfig(conf Config, name string) { - l, err := NewZapLogger(&conf) +func InitFromConfig(name string) { + //var c zap.Config + //file, _ := os.Create(config.Config.Log.StorageLocation) + //writeSyncer := zapcore.AddSync(file) + + l, err := NewZapLogger() if err == nil { setLogger(l, name) } @@ -83,55 +88,50 @@ type ZapLogger struct { SampleInterval int } -func NewZapLogger(conf *Config) (*ZapLogger, error) { - lvl := ParseZapLevel(conf.Level) +func NewZapLogger() (*ZapLogger, error) { zapConfig := zap.Config{ - Level: zap.NewAtomicLevelAt(lvl), - Development: false, - Encoding: "console", - EncoderConfig: zap.NewDevelopmentEncoderConfig(), - OutputPaths: []string{"stderr"}, + Level: zap.NewAtomicLevelAt(zapcore.DebugLevel), + Development: true, + Encoding: "json", + EncoderConfig: zap.NewProductionEncoderConfig(), + OutputPaths: []string{config.Config.Log.StorageLocation}, ErrorOutputPaths: []string{"stderr"}, } - if conf.JSON { - zapConfig.Encoding = "json" - zapConfig.EncoderConfig = zap.NewProductionEncoderConfig() - } l, err := zapConfig.Build() if err != nil { return nil, err } zl := &ZapLogger{ - unsampled: l.Sugar(), - SampleDuration: time.Duration(conf.ItemSampleSeconds) * time.Second, - SampleInitial: conf.ItemSampleInitial, - SampleInterval: conf.ItemSampleInterval, + unsampled: l.Sugar(), + //SampleDuration: time.Duration(conf.ItemSampleSeconds) * time.Second, + //SampleInitial: conf.ItemSampleInitial, + //SampleInterval: conf.ItemSampleInterval, } - if conf.Sample { - // use a sampling logger for the main logger - samplingConf := &zap.SamplingConfig{ - Initial: conf.SampleInitial, - Thereafter: conf.SampleInterval, - } - // sane defaults - if samplingConf.Initial == 0 { - samplingConf.Initial = 20 - } - if samplingConf.Thereafter == 0 { - samplingConf.Thereafter = 100 - } - zl.zap = l.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { - return zapcore.NewSamplerWithOptions( - core, - time.Second, - samplingConf.Initial, - samplingConf.Thereafter, - ) - })).Sugar() - } else { - zl.zap = zl.unsampled - } + //if conf.Sample { + // // use a sampling logger for the main logger + // samplingConf := &zap.SamplingConfig{ + // Initial: conf.SampleInitial, + // Thereafter: conf.SampleInterval, + // } + // // sane defaults + // if samplingConf.Initial == 0 { + // samplingConf.Initial = 20 + // } + // if samplingConf.Thereafter == 0 { + // samplingConf.Thereafter = 100 + // } + // zl.zap = l.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core { + // return zapcore.NewSamplerWithOptions( + // core, + // time.Second, + // samplingConf.Initial, + // samplingConf.Thereafter, + // ) + // })).Sugar() + //} else { + // zl.zap = zl.unsampled + //} return zl, nil } From bebe89162fb470a23a638fe550600bbdadeb623d Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Fri, 10 Mar 2023 19:57:53 +0800 Subject: [PATCH 2/3] fix cron --- pkg/common/cmd/root.go | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index c53824b81..bdcd2c900 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -20,16 +20,7 @@ func NewRootCmd() (rootCmd *RootCmd) { Short: "Start the server", Long: `Start the server`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - log.InitFromConfig(log.Config{ - JSON: true, - Level: "-1", - Sample: false, - SampleInitial: 0, - SampleInterval: 0, - ItemSampleSeconds: 0, - ItemSampleInitial: 0, - ItemSampleInterval: 0, - }, "newlog") + log.InitFromConfig("newlog") return rootCmd.getConfFromCmdAndInit(cmd) }, } From c6248808630c125842497667962f6b672f2e5c56 Mon Sep 17 00:00:00 2001 From: wangchuxiao Date: Fri, 10 Mar 2023 20:02:07 +0800 Subject: [PATCH 3/3] fix cron --- pkg/common/cmd/root.go | 6 +++++- pkg/common/logger/log.go | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/common/cmd/root.go b/pkg/common/cmd/root.go index bdcd2c900..d27c51091 100644 --- a/pkg/common/cmd/root.go +++ b/pkg/common/cmd/root.go @@ -20,8 +20,12 @@ func NewRootCmd() (rootCmd *RootCmd) { Short: "Start the server", Long: `Start the server`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + err := rootCmd.getConfFromCmdAndInit(cmd) + if err != nil { + return err + } log.InitFromConfig("newlog") - return rootCmd.getConfFromCmdAndInit(cmd) + return nil }, } rootCmd.Command = c diff --git a/pkg/common/logger/log.go b/pkg/common/logger/log.go index 9ed02e903..0b3d12cad 100644 --- a/pkg/common/logger/log.go +++ b/pkg/common/logger/log.go @@ -95,7 +95,7 @@ func NewZapLogger() (*ZapLogger, error) { Encoding: "json", EncoderConfig: zap.NewProductionEncoderConfig(), OutputPaths: []string{config.Config.Log.StorageLocation}, - ErrorOutputPaths: []string{"stderr"}, + ErrorOutputPaths: []string{config.Config.Log.StorageLocation}, } l, err := zapConfig.Build() if err != nil {