update
parent
1b026b8f2e
commit
c000be429e
28
mylog/log.go
28
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,
|
||||
}
|
||||
|
|
|
@ -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())
|
||||
|
|
Loading…
Reference in New Issue