2 Commits
Author | SHA1 | Message | Date |
---|---|---|---|
frank | 25a9f43c1a |
rotation
|
2 years ago |
frank | c7cc9fe243 |
rotation
|
2 years ago |
5 changed files with 172 additions and 41 deletions
-
53auto_rotate.go
-
0cmd/logs/cmd.error
-
14cmd/print.go
-
17config.go
-
129logger.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)) |
||||
|
} |
@ -1,11 +1,13 @@ |
|||||
package main |
package main |
||||
|
|
||||
import "git.drinkme.beer/yinghe/log" |
|
||||
|
import ( |
||||
|
"git.drinkme.beer/yinghe/log" |
||||
|
) |
||||
|
|
||||
func main() { |
func main() { |
||||
log.Infof("hahah") |
|
||||
|
|
||||
log.ReloadLogger("./conf/cfg.toml") |
|
||||
log.Infof(" reloaded.") |
|
||||
return |
|
||||
|
spec := "@daily" |
||||
|
log.RegisterRotationWithSpec(`./logs/cmd`, spec) |
||||
|
log.RegisterRotation("./log/cmd") |
||||
|
log.Infof("ping") |
||||
|
log.Infof("pong") |
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue