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 (
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"log"
|
|
|
|
"amuz.es/src/infra/goutils/logger"
|
|
|
|
)
|
|
|
|
|
2018-06-10 16:57:59 +09:00
|
|
|
var loggers RotateSyncerSet
|
2018-06-06 15:30:23 +09:00
|
|
|
|
|
|
|
func NewLogWriter(FileName string, MaxSizeMb, MaxBackup, MaxDay int, logDir string) logger.RotateSyncer {
|
|
|
|
switch FileName {
|
|
|
|
case "Stdout":
|
2018-06-10 16:57:59 +09:00
|
|
|
return NewLocked(os.Stdout)
|
2018-06-06 15:30:23 +09:00
|
|
|
case "Stderr":
|
2018-06-10 16:57:59 +09:00
|
|
|
return NewLocked(os.Stderr)
|
2018-06-06 15:30:23 +09:00
|
|
|
default:
|
|
|
|
logpath := FileName
|
|
|
|
if logDir != "" {
|
|
|
|
logpath = path.Join(logDir, FileName)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println(" Attention!! log writes to ", logpath)
|
|
|
|
|
2018-06-10 16:57:59 +09:00
|
|
|
logWriter := NewRotater(
|
2018-06-06 15:30:23 +09:00
|
|
|
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")
|
|
|
|
}
|