Browse Source

rotation

master v0.1.6
frank 1 year ago
parent
commit
25a9f43c1a
  1. 53
      auto_rotate.go
  2. 17
      cmd/print.go
  3. 17
      logger.go

53
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))
}

17
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....")
}

17
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

Loading…
Cancel
Save