This repository has been archived on 2022-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
2018-06-06 15:30:23 +09:00
|
|
|
package rotater
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"amuz.es/src/infra/goutils/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
type rotateSyncer struct {
|
|
|
|
setOnceOnclose *sync.Once
|
|
|
|
onClose func()
|
|
|
|
lumberjack.Logger
|
|
|
|
}
|
|
|
|
|
2018-06-07 00:48:38 +09:00
|
|
|
func NewRotater(filename string, maxSize, maxBackup, maxDay int) logger.RotateSyncer {
|
2018-06-06 15:30:23 +09:00
|
|
|
return &rotateSyncer{
|
|
|
|
setOnceOnclose: &sync.Once{},
|
|
|
|
Logger: lumberjack.Logger{
|
|
|
|
Filename: filename,
|
|
|
|
MaxSize: maxSize, // megabytes
|
|
|
|
MaxBackups: maxBackup,
|
|
|
|
MaxAge: maxDay, //days
|
|
|
|
LocalTime: false,
|
|
|
|
Compress: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (r *rotateSyncer) SetOnClose(closeFunc func()) {
|
|
|
|
r.setOnceOnclose.Do(func() {
|
|
|
|
r.onClose = closeFunc
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rotateSyncer) Rotate() error {
|
|
|
|
return r.Logger.Rotate()
|
|
|
|
}
|
|
|
|
func (r *rotateSyncer) Close() error {
|
|
|
|
defer func() {
|
|
|
|
if r.onClose != nil {
|
|
|
|
r.onClose()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return r.Logger.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rotateSyncer) Sync() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rotateSyncer) Write(bs []byte) (int, error) {
|
|
|
|
return s.Logger.Write(bs)
|
|
|
|
}
|