2018-06-06 15:30:23 +09:00
|
|
|
package rotater
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"amuz.es/src/infra/goutils/logger"
|
2018-06-12 08:45:20 +09:00
|
|
|
"github.com/lestrrat-go/file-rotatelogs"
|
2018-06-06 15:30:23 +09:00
|
|
|
)
|
|
|
|
|
2018-06-12 08:45:20 +09:00
|
|
|
type Option = rotatelogs.Option
|
|
|
|
|
2018-06-06 15:30:23 +09:00
|
|
|
type rotateSyncer struct {
|
|
|
|
setOnceOnclose *sync.Once
|
|
|
|
onClose func()
|
2018-06-12 08:45:20 +09:00
|
|
|
*rotatelogs.RotateLogs
|
2018-06-06 15:30:23 +09:00
|
|
|
}
|
|
|
|
|
2018-06-12 08:45:20 +09:00
|
|
|
func NewRotater(filename string, options ...Option) (logger.RotateSyncer, error) {
|
|
|
|
if rotateLogger, err := rotatelogs.New(filename, options...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return &rotateSyncer{
|
|
|
|
setOnceOnclose: &sync.Once{},
|
|
|
|
RotateLogs: rotateLogger,
|
|
|
|
}, nil
|
2018-06-06 15:30:23 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
func (r *rotateSyncer) SetOnClose(closeFunc func()) {
|
|
|
|
r.setOnceOnclose.Do(func() {
|
|
|
|
r.onClose = closeFunc
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rotateSyncer) Rotate() error {
|
2018-06-12 08:45:20 +09:00
|
|
|
return r.RotateLogs.Rotate()
|
2018-06-06 15:30:23 +09:00
|
|
|
}
|
|
|
|
func (r *rotateSyncer) Close() error {
|
|
|
|
defer func() {
|
|
|
|
if r.onClose != nil {
|
|
|
|
r.onClose()
|
|
|
|
}
|
|
|
|
}()
|
2018-06-12 08:45:20 +09:00
|
|
|
return r.RotateLogs.Close()
|
2018-06-06 15:30:23 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *rotateSyncer) Sync() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *rotateSyncer) Write(bs []byte) (int, error) {
|
2018-06-12 08:45:20 +09:00
|
|
|
return s.RotateLogs.Write(bs)
|
2018-06-06 15:30:23 +09:00
|
|
|
}
|