53 lines
975 B
Go
53 lines
975 B
Go
|
package rotater
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"path"
|
||
|
"log"
|
||
|
"amuz.es/src/infra/goutils/logger"
|
||
|
)
|
||
|
|
||
|
var loggers logger.RotateSyncerSet
|
||
|
|
||
|
func NewLogWriter(FileName string, MaxSizeMb, MaxBackup, MaxDay int, logDir string) logger.RotateSyncer {
|
||
|
switch FileName {
|
||
|
case "Stdout":
|
||
|
return newLocked(os.Stdout)
|
||
|
case "Stderr":
|
||
|
return newLocked(os.Stderr)
|
||
|
default:
|
||
|
logpath := FileName
|
||
|
if logDir != "" {
|
||
|
logpath = path.Join(logDir, FileName)
|
||
|
}
|
||
|
|
||
|
log.Println(" Attention!! log writes to ", logpath)
|
||
|
|
||
|
logWriter := newRotater(
|
||
|
logpath,
|
||
|
MaxSizeMb, // megabytes
|
||
|
MaxBackup,
|
||
|
MaxDay, //days
|
||
|
)
|
||
|
loggers.Store(logWriter)
|
||
|
logWriter.SetOnClose(func() { loggers.Delete(logWriter) })
|
||
|
return logWriter
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func Rotate() {
|
||
|
loggers.Range(func(rotater logger.RotateSyncer) {
|
||
|
rotater.Sync()
|
||
|
rotater.Rotate()
|
||
|
})
|
||
|
log.Println("rotated")
|
||
|
}
|
||
|
|
||
|
func Close() {
|
||
|
loggers.Range(func(rotater logger.RotateSyncer) {
|
||
|
rotater.Sync()
|
||
|
rotater.Close()
|
||
|
})
|
||
|
log.Println("end of log")
|
||
|
}
|