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, NeedLogFile: false,
ConsoleWriter: os.Stdout, ConsoleWriter: os.Stdout,
} }
DefaultLogFile = &LogFile{
LogFilePath: "logs",
MaxSize: 200,
MaxAge: 0,
MaxBackups: 0,
}
globalLog = NewLogger("debug", DefaultConfig) globalLog = NewLogger("debug", DefaultConfig)
) )
@ -43,9 +50,16 @@ type (
Config struct { Config struct {
Level string Level string
NeedLogFile bool NeedLogFile bool
LogFilePath string
ConsoleWriter io.Writer ConsoleWriter io.Writer
ZapOpt []zap.Option 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 { func getRollingFileWriter(serverName string, config *Config) *lumberjack.Logger {
if config.LogFilePath == "" { if config.LogFile == nil {
config.LogFilePath = "logs" config.LogFile = DefaultLogFile
} }
return &lumberjack.Logger{ return &lumberjack.Logger{
Filename: filepath.Join(config.LogFilePath, serverName+".log"), Filename: filepath.Join(config.LogFile.LogFilePath, serverName+".log"),
MaxSize: 0, MaxSize: config.LogFile.MaxSize,
MaxAge: 0, MaxAge: config.LogFile.MaxAge,
MaxBackups: 0, MaxBackups: config.LogFile.MaxBackups,
LocalTime: true, LocalTime: true,
Compress: false, Compress: false,
} }

View File

@ -22,8 +22,7 @@ func TestLog(t *testing.T) {
func TestLogFile(t *testing.T) { func TestLogFile(t *testing.T) {
config := DefaultConfig config := DefaultConfig
config.NeedLogFile = false config.NeedLogFile = true
config.LogFilePath = "logs"
Init("debug", config) Init("debug", config)
Debug("time: ", time.Now()) Debug("time: ", time.Now())