update log
This commit is contained in:
42
mylog/log.go
42
mylog/log.go
@@ -3,8 +3,10 @@ package mylog
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -41,6 +43,7 @@ type (
|
||||
Config struct {
|
||||
Level string
|
||||
NeedLogFile bool
|
||||
LogFilePath string
|
||||
ConsoleWriter io.Writer
|
||||
ZapOpt []zap.Option
|
||||
}
|
||||
@@ -62,22 +65,32 @@ func NewLogger(serverName string, config *Config) *ZapLog {
|
||||
level = zapcore.DebugLevel
|
||||
}
|
||||
|
||||
cfg := zap.NewProductionEncoderConfig()
|
||||
cfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
cfg.ConsoleSeparator = " | "
|
||||
|
||||
// 指定日志时间格式
|
||||
cfg.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05.000")
|
||||
cfg.EncodeCaller = zapcore.ShortCallerEncoder
|
||||
|
||||
cores := make([]zapcore.Core, 0)
|
||||
// 使用控制台输出
|
||||
if config.ConsoleWriter != nil {
|
||||
cfg := zap.NewProductionEncoderConfig()
|
||||
cfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
cfg.ConsoleSeparator = " | "
|
||||
// 指定日志时间格式
|
||||
cfg.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05.000")
|
||||
cfg.EncodeCaller = zapcore.ShortCallerEncoder
|
||||
encoder := zapcore.NewConsoleEncoder(cfg)
|
||||
core := zapcore.NewCore(encoder, zapcore.AddSync(config.ConsoleWriter), level)
|
||||
cores = append(cores, core)
|
||||
}
|
||||
|
||||
if config.NeedLogFile {
|
||||
cfg := zap.NewProductionEncoderConfig()
|
||||
cfg.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||
cfg.ConsoleSeparator = " | "
|
||||
// 指定日志时间格式
|
||||
cfg.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05.000")
|
||||
cfg.EncodeCaller = zapcore.ShortCallerEncoder
|
||||
encoder := zapcore.NewConsoleEncoder(cfg)
|
||||
core := zapcore.NewCore(encoder, zapcore.AddSync(getRollingFileWriter(serverName, config)), level)
|
||||
cores = append(cores, core)
|
||||
}
|
||||
|
||||
opts := make([]zap.Option, 0)
|
||||
if config.ZapOpt != nil {
|
||||
opts = config.ZapOpt
|
||||
@@ -92,6 +105,19 @@ func NewLogger(serverName string, config *Config) *ZapLog {
|
||||
}
|
||||
}
|
||||
|
||||
func getRollingFileWriter(serverName string, config *Config) *lumberjack.Logger {
|
||||
//filePath := config.LogFilePath
|
||||
|
||||
return &lumberjack.Logger{
|
||||
Filename: filepath.Join(config.LogFilePath, serverName+".log"),
|
||||
MaxSize: 0,
|
||||
MaxAge: 0,
|
||||
MaxBackups: 0,
|
||||
LocalTime: true,
|
||||
Compress: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (z *ZapLog) Debug(args ...interface{}) {
|
||||
z.sugarLog.Debug(args...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user