diff --git a/mylog/log.go b/mylog/log.go index 16e4db1..fa5ac1b 100644 --- a/mylog/log.go +++ b/mylog/log.go @@ -32,6 +32,13 @@ var ( NeedLogFile: false, ConsoleWriter: os.Stdout, } + + DefaultLogFile = &LogFile{ + LogFilePath: "logs", + MaxSize: 200, + MaxAge: 0, + MaxBackups: 0, + } globalLog = NewLogger("debug", DefaultConfig) ) @@ -43,9 +50,16 @@ type ( Config struct { Level string NeedLogFile bool - LogFilePath string ConsoleWriter io.Writer ZapOpt []zap.Option + LogFile *LogFile + } + + LogFile struct { + LogFilePath string + MaxSize int + MaxAge int + MaxBackups int } ) @@ -106,15 +120,15 @@ func NewLogger(serverName string, config *Config) *ZapLog { } func getRollingFileWriter(serverName string, config *Config) *lumberjack.Logger { - if config.LogFilePath == "" { - config.LogFilePath = "logs" + if config.LogFile == nil { + config.LogFile = DefaultLogFile } return &lumberjack.Logger{ - Filename: filepath.Join(config.LogFilePath, serverName+".log"), - MaxSize: 0, - MaxAge: 0, - MaxBackups: 0, + Filename: filepath.Join(config.LogFile.LogFilePath, serverName+".log"), + MaxSize: config.LogFile.MaxSize, + MaxAge: config.LogFile.MaxAge, + MaxBackups: config.LogFile.MaxBackups, LocalTime: true, Compress: false, } diff --git a/mylog/log_test.go b/mylog/log_test.go index 35972d1..47c50f4 100644 --- a/mylog/log_test.go +++ b/mylog/log_test.go @@ -22,8 +22,7 @@ func TestLog(t *testing.T) { func TestLogFile(t *testing.T) { config := DefaultConfig - config.NeedLogFile = false - config.LogFilePath = "logs" + config.NeedLogFile = true Init("debug", config) Debug("time: ", time.Now())