frank
2 years ago
3 changed files with 61 additions and 26 deletions
-
53auto_rotate.go
-
17cmd/print.go
-
17logger.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)) |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue