amuz.es/src/infra/goutils@v0.1.3/logger/rotater/global.go (about) 1 package rotater 2 3 import ( 4 "os" 5 "path/filepath" 6 "log" 7 "amuz.es/src/infra/goutils/logger" 8 "github.com/lestrrat-go/file-rotatelogs" 9 ) 10 11 var loggers RotateSyncerSet 12 13 func NewLogWriter(FileName string, logDir string, options ...Option) (logger.RotateSyncer, error) { 14 switch FileName { 15 case "Stdout": 16 return NewLocked(os.Stdout), nil 17 case "Stderr": 18 return NewLocked(os.Stderr), nil 19 default: 20 logpath := FileName 21 if logDir != "" { 22 logpath = filepath.Join(logDir, FileName) 23 } 24 logpath,_ = filepath.Abs(logpath) 25 log.Println(" Attention!! log writes to ", logpath) 26 options=append(options,rotatelogs.WithLinkName(logpath)) 27 if logWriter, err := NewRotater(logpath+".%Y%m%d", options...); err != nil { 28 return nil, err 29 } else { 30 loggers.Store(logWriter) 31 logWriter.SetOnClose(func() { loggers.Delete(logWriter) }) 32 return logWriter, nil 33 } 34 } 35 } 36 37 func Rotate() { 38 loggers.Range(func(rotater logger.RotateSyncer) { 39 rotater.Sync() 40 rotater.Rotate() 41 }) 42 log.Println("rotated") 43 } 44 45 func Close() { 46 loggers.Range(func(rotater logger.RotateSyncer) { 47 rotater.Sync() 48 rotater.Close() 49 }) 50 log.Println("end of log") 51 }