main
lizifeng 2023-03-20 11:56:29 +08:00
parent 1b026b8f2e
commit c000be429e
2 changed files with 22 additions and 9 deletions

View File

@ -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,
}

View File

@ -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())