From 25a9f43c1ab5882c089dcb3124d59ec20d2ebff4 Mon Sep 17 00:00:00 2001 From: frank Date: Wed, 11 Jan 2023 11:24:25 +0800 Subject: [PATCH] rotation --- auto_rotate.go | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/print.go | 17 ++++------------ logger.go | 17 ++++------------ 3 files changed, 61 insertions(+), 26 deletions(-) create mode 100644 auto_rotate.go diff --git a/auto_rotate.go b/auto_rotate.go new file mode 100644 index 0000000..1e4b7f3 --- /dev/null +++ b/auto_rotate.go @@ -0,0 +1,53 @@ +package log + +import ( + "fmt" + "github.com/robfig/cron" + "log" + "sync" + "time" +) + +var ( + logPath string + once sync.Once + Spec = "@daily" +) + +// RegisterRotationWithSpec set up crontab scheduler for log rotation +// +// spec, "* * * * * *"; "@daily" +func RegisterRotationWithSpec(path, spec string) { + if "" == spec { + spec = Spec + } + listening(path, spec) +} + +func RegisterRotation(path string) { + listening(path, Spec) +} + +func listening(path, spec string) { + once.Do(func() { + logPath = path + rotate() + go _listening(spec) + }) +} + +func _listening(spec string) { + _c := cron.New() + if err := _c.AddFunc(spec, rotate); nil != err { + panic(fmt.Sprintf("failed to establish crontab job: %s", err.Error())) + } else { + log.Println("log rotation started ...") + } + + _c.Start() + select {} +} + +func rotate() { + RotateLogger(New(logPath+time.Now().Format("2006-01-02"), logPath)) +} diff --git a/cmd/print.go b/cmd/print.go index b9ee1ec..c22f768 100644 --- a/cmd/print.go +++ b/cmd/print.go @@ -2,21 +2,12 @@ package main import ( "git.drinkme.beer/yinghe/log" - "time" ) func main() { - log.Infof("hahah") - - log.ReloadLogger("./conf/cfg.toml") - log.Infof(" reloaded.") - - now := time.Now() - log.RotateLogger(log.New(`./logs/cmd.`+now.Format(time.RFC3339), `./logs/cmd`)) - log.Info("ping") - log.RotateLogger(log.New(`./logs/cmd.`+now.Format(time.RFC3339), `./logs/cmd`)) + spec := "@daily" + log.RegisterRotationWithSpec(`./logs/cmd`, spec) + log.RegisterRotation("./log/cmd") + log.Infof("ping") log.Infof("pong") - - log.RotateLogger(log.New(`./logs/cmd.`+now.Add(time.Hour).Format(time.RFC3339), `./logs/cmd`)) - log.Infof("one hour later....") } diff --git a/logger.go b/logger.go index 0db06f5..48338a8 100644 --- a/logger.go +++ b/logger.go @@ -8,6 +8,7 @@ import ( "log" "os" "path" + "time" "github.com/BurntSushi/toml" "go.uber.org/zap" @@ -17,8 +18,6 @@ import ( var ( logger Logger - //OutputPaths = "" - //ErrOutputPaths = "" ) type YLogger struct { @@ -56,7 +55,7 @@ func init() { } cfg = logCfg.Logs - cfg.OutputPaths = cfg.LogPath + cfg.APPName + ".log" + cfg.OutputPaths = cfg.LogPath + cfg.APPName + time.Now().Format("2006-01-02") + ".log" cfg.ErrorOutputPaths = cfg.LogPath + cfg.APPName + ".error" err := InitLog(cfg.LogConf, cfg) @@ -76,7 +75,7 @@ func ReloadLogger(configFile string) { log.Fatalf("Read config file err:%s", err.Error()) } cfg = logCfg.Logs - cfg.OutputPaths = cfg.LogPath + cfg.APPName + ".log" + cfg.OutputPaths = cfg.LogPath + cfg.APPName + time.Now().Format("2006-01-02") + ".log" cfg.ErrorOutputPaths = cfg.LogPath + cfg.APPName + ".error" err := InitLog(cfg.LogConf, cfg) @@ -133,8 +132,6 @@ encoderConfig: func InitLog(logConfFile string, cfg LogCfg) error { conf := &zap.Config{} if logConfFile != "" { - //InitLogger(nil) - //return fmt.Errorf("log configure file name is nil") if path.Ext(logConfFile) != ".yml" { InitLogger(nil) return fmt.Errorf("log configure file name{%s} suffix must be .yml", logConfFile) @@ -166,12 +163,11 @@ func InitLog(logConfFile string, cfg LogCfg) error { NameKey: "logger", CallerKey: "caller", StacktraceKey: "stacktrace", - LineEnding: "", - EncodeLevel: zapcore.LowercaseLevelEncoder, EncodeTime: zapcore.EpochMillisTimeEncoder, EncodeDuration: zapcore.SecondsDurationEncoder, EncodeCaller: zapcore.ShortCallerEncoder, EncodeName: nil, + EncodeLevel: nil, }, } @@ -181,13 +177,8 @@ func InitLog(logConfFile string, cfg LogCfg) error { return fmt.Errorf("[Unmarshal]init logger error: %v", err) } } - - log.Println("to set yuanex logger ...") - conf.OutputPaths = append(conf.OutputPaths, cfg.OutputPaths) conf.ErrorOutputPaths = append(conf.ErrorOutputPaths, cfg.ErrorOutputPaths) - log.Println("OutputPahts -->", conf.OutputPaths) - log.Println("ErrorOutputPahts -->", conf.ErrorOutputPaths) InitLogger(conf) return nil