52 lines
966 B
Go
52 lines
966 B
Go
|
package rotater
|
||
|
|
||
|
import (
|
||
|
"sync"
|
||
|
"amuz.es/src/infra/goutils/logger"
|
||
|
)
|
||
|
|
||
|
type rotateSyncer struct {
|
||
|
setOnceOnclose *sync.Once
|
||
|
onClose func()
|
||
|
lumberjack.Logger
|
||
|
}
|
||
|
|
||
|
func newRotater(filename string, maxSize, maxBackup, maxDay int) logger.RotateSyncer {
|
||
|
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)
|
||
|
}
|