Browse Source

rotation

master v0.1.6
frank 2 years 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 ( import (
"git.drinkme.beer/yinghe/log" "git.drinkme.beer/yinghe/log"
"time"
) )
func main() { 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.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" "log"
"os" "os"
"path" "path"
"time"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"go.uber.org/zap" "go.uber.org/zap"
@ -17,8 +18,6 @@ import (
var ( var (
logger Logger logger Logger
//OutputPaths = ""
//ErrOutputPaths = ""
) )
type YLogger struct { type YLogger struct {
@ -56,7 +55,7 @@ func init() {
} }
cfg = logCfg.Logs 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" cfg.ErrorOutputPaths = cfg.LogPath + cfg.APPName + ".error"
err := InitLog(cfg.LogConf, cfg) err := InitLog(cfg.LogConf, cfg)
@ -76,7 +75,7 @@ func ReloadLogger(configFile string) {
log.Fatalf("Read config file err:%s", err.Error()) log.Fatalf("Read config file err:%s", err.Error())
} }
cfg = logCfg.Logs 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" cfg.ErrorOutputPaths = cfg.LogPath + cfg.APPName + ".error"
err := InitLog(cfg.LogConf, cfg) err := InitLog(cfg.LogConf, cfg)
@ -133,8 +132,6 @@ encoderConfig:
func InitLog(logConfFile string, cfg LogCfg) error { func InitLog(logConfFile string, cfg LogCfg) error {
conf := &zap.Config{} conf := &zap.Config{}
if logConfFile != "" { if logConfFile != "" {
//InitLogger(nil)
//return fmt.Errorf("log configure file name is nil")
if path.Ext(logConfFile) != ".yml" { if path.Ext(logConfFile) != ".yml" {
InitLogger(nil) InitLogger(nil)
return fmt.Errorf("log configure file name{%s} suffix must be .yml", logConfFile) 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", NameKey: "logger",
CallerKey: "caller", CallerKey: "caller",
StacktraceKey: "stacktrace", StacktraceKey: "stacktrace",
LineEnding: "",
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.EpochMillisTimeEncoder, EncodeTime: zapcore.EpochMillisTimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder, EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder, EncodeCaller: zapcore.ShortCallerEncoder,
EncodeName: nil, EncodeName: nil,
EncodeLevel: nil,
}, },
} }
@ -181,13 +177,8 @@ func InitLog(logConfFile string, cfg LogCfg) error {
return fmt.Errorf("[Unmarshal]init logger error: %v", err) return fmt.Errorf("[Unmarshal]init logger error: %v", err)
} }
} }
log.Println("to set yuanex logger ...")
conf.OutputPaths = append(conf.OutputPaths, cfg.OutputPaths) conf.OutputPaths = append(conf.OutputPaths, cfg.OutputPaths)
conf.ErrorOutputPaths = append(conf.ErrorOutputPaths, cfg.ErrorOutputPaths) conf.ErrorOutputPaths = append(conf.ErrorOutputPaths, cfg.ErrorOutputPaths)
log.Println("OutputPahts -->", conf.OutputPaths)
log.Println("ErrorOutputPahts -->", conf.ErrorOutputPaths)
InitLogger(conf) InitLogger(conf)
return nil return nil

Loading…
Cancel
Save