2023-01-15 00:21:08 +08:00
|
|
|
package mylog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2023-03-14 13:53:07 +08:00
|
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
2023-01-15 00:21:08 +08:00
|
|
|
"io"
|
|
|
|
"os"
|
2023-03-14 13:53:07 +08:00
|
|
|
"path/filepath"
|
2023-01-15 00:21:08 +08:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DebugLevel = "DEBUG"
|
|
|
|
InfoLevel = "INFO"
|
|
|
|
WarnLevel = "WARN"
|
|
|
|
ErrorLevel = "ERROR"
|
|
|
|
FatalLevel = "FATAL"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
levelMap = map[string]zapcore.Level{
|
|
|
|
"DEBUG": zapcore.DebugLevel,
|
|
|
|
"INFO": zapcore.InfoLevel,
|
|
|
|
"WARN": zapcore.WarnLevel,
|
|
|
|
"ERROR": zapcore.ErrorLevel,
|
|
|
|
"FATAL": zapcore.FatalLevel,
|
|
|
|
}
|
2024-09-30 15:41:29 +08:00
|
|
|
|
|
|
|
defaultLogLevel = DebugLevel
|
|
|
|
|
2023-01-15 00:21:08 +08:00
|
|
|
// DefaultConfig 默认配置
|
|
|
|
DefaultConfig = &Config{
|
|
|
|
Level: DebugLevel,
|
|
|
|
NeedLogFile: false,
|
|
|
|
ConsoleWriter: os.Stdout,
|
|
|
|
}
|
2023-03-20 11:56:29 +08:00
|
|
|
|
|
|
|
DefaultLogFile = &LogFile{
|
|
|
|
LogFilePath: "logs",
|
|
|
|
MaxSize: 200,
|
|
|
|
MaxAge: 0,
|
|
|
|
MaxBackups: 0,
|
|
|
|
}
|
2023-01-15 00:21:08 +08:00
|
|
|
globalLog = NewLogger("debug", DefaultConfig)
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
ZapLog struct {
|
|
|
|
sugarLog *zap.SugaredLogger
|
|
|
|
}
|
|
|
|
|
|
|
|
Config struct {
|
|
|
|
Level string
|
|
|
|
NeedLogFile bool
|
|
|
|
ConsoleWriter io.Writer
|
|
|
|
ZapOpt []zap.Option
|
2023-03-20 11:56:29 +08:00
|
|
|
LogFile *LogFile
|
|
|
|
}
|
|
|
|
|
|
|
|
LogFile struct {
|
|
|
|
LogFilePath string
|
|
|
|
MaxSize int
|
|
|
|
MaxAge int
|
|
|
|
MaxBackups int
|
2023-01-15 00:21:08 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-09-30 15:41:29 +08:00
|
|
|
func SetLogLevel(level string) {
|
|
|
|
defaultLogLevel = level
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init() {
|
2024-09-30 15:55:21 +08:00
|
|
|
globalLog = NewLogger("app", &Config{
|
2024-09-30 15:41:29 +08:00
|
|
|
Level: defaultLogLevel,
|
|
|
|
NeedLogFile: false,
|
|
|
|
ConsoleWriter: os.Stdout,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitWithConfig 覆盖默认日志
|
|
|
|
func InitWithConfig(serverName string, config *Config) {
|
2023-01-15 00:21:08 +08:00
|
|
|
globalLog = NewLogger(serverName, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLogger(serverName string, config *Config) *ZapLog {
|
|
|
|
if config == nil {
|
|
|
|
config = DefaultConfig
|
|
|
|
}
|
|
|
|
var level zapcore.Level
|
|
|
|
if v, ok := levelMap[strings.ToUpper(config.Level)]; ok {
|
|
|
|
level = v
|
|
|
|
} else {
|
|
|
|
level = zapcore.DebugLevel
|
|
|
|
}
|
|
|
|
|
|
|
|
cores := make([]zapcore.Core, 0)
|
|
|
|
// 使用控制台输出
|
|
|
|
if config.ConsoleWriter != nil {
|
2023-03-14 13:53:07 +08:00
|
|
|
cfg := zap.NewProductionEncoderConfig()
|
|
|
|
cfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
|
|
|
cfg.ConsoleSeparator = " | "
|
|
|
|
// 指定日志时间格式
|
|
|
|
cfg.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05.000")
|
|
|
|
cfg.EncodeCaller = zapcore.ShortCallerEncoder
|
2023-01-15 00:21:08 +08:00
|
|
|
encoder := zapcore.NewConsoleEncoder(cfg)
|
|
|
|
core := zapcore.NewCore(encoder, zapcore.AddSync(config.ConsoleWriter), level)
|
|
|
|
cores = append(cores, core)
|
|
|
|
}
|
|
|
|
|
2023-03-14 13:53:07 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-01-15 00:21:08 +08:00
|
|
|
opts := make([]zap.Option, 0)
|
|
|
|
if config.ZapOpt != nil {
|
|
|
|
opts = config.ZapOpt
|
|
|
|
} else {
|
2023-01-19 11:32:26 +08:00
|
|
|
opts = append(opts, zap.AddCaller(), zap.AddCallerSkip(2))
|
2023-01-15 00:21:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
zl := zap.New(zapcore.NewTee(cores...), opts...)
|
|
|
|
|
|
|
|
return &ZapLog{
|
|
|
|
sugarLog: zl.Sugar(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 13:53:07 +08:00
|
|
|
func getRollingFileWriter(serverName string, config *Config) *lumberjack.Logger {
|
2023-03-20 11:56:29 +08:00
|
|
|
if config.LogFile == nil {
|
|
|
|
config.LogFile = DefaultLogFile
|
2023-03-20 11:47:46 +08:00
|
|
|
}
|
2023-03-14 13:53:07 +08:00
|
|
|
|
|
|
|
return &lumberjack.Logger{
|
2023-03-20 11:56:29 +08:00
|
|
|
Filename: filepath.Join(config.LogFile.LogFilePath, serverName+".log"),
|
|
|
|
MaxSize: config.LogFile.MaxSize,
|
|
|
|
MaxAge: config.LogFile.MaxAge,
|
|
|
|
MaxBackups: config.LogFile.MaxBackups,
|
2023-03-14 13:53:07 +08:00
|
|
|
LocalTime: true,
|
|
|
|
Compress: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 00:21:08 +08:00
|
|
|
func (z *ZapLog) Debug(args ...interface{}) {
|
|
|
|
z.sugarLog.Debug(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ZapLog) Info(args ...interface{}) {
|
|
|
|
z.sugarLog.Info(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ZapLog) Warn(args ...interface{}) {
|
|
|
|
z.sugarLog.Warn(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ZapLog) Error(args ...interface{}) {
|
|
|
|
z.sugarLog.Error(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ZapLog) Fatal(args ...interface{}) {
|
|
|
|
z.sugarLog.Fatal(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ZapLog) Debugf(format string, args ...interface{}) {
|
|
|
|
z.sugarLog.Debugf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ZapLog) Infof(format string, args ...interface{}) {
|
|
|
|
z.sugarLog.Infof(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ZapLog) Warnf(format string, args ...interface{}) {
|
|
|
|
z.sugarLog.Warnf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ZapLog) Errorf(format string, args ...interface{}) {
|
|
|
|
z.sugarLog.Errorf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ZapLog) Fatalf(format string, args ...interface{}) {
|
|
|
|
z.sugarLog.Fatalf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ZapLog) Println(args ...interface{}) {
|
|
|
|
z.sugarLog.Info(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ZapLog) Printf(format string, args ...interface{}) {
|
|
|
|
z.sugarLog.Infof(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (z *ZapLog) Sync() {
|
|
|
|
z.sugarLog.Sync()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Debug(args ...interface{}) {
|
|
|
|
globalLog.Debug(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Info(args ...interface{}) {
|
|
|
|
globalLog.Info(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Warn(args ...interface{}) {
|
|
|
|
globalLog.Warn(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Error(args ...interface{}) {
|
|
|
|
globalLog.Error(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fatal(args ...interface{}) {
|
|
|
|
globalLog.Fatal(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Debugf(format string, args ...interface{}) {
|
|
|
|
globalLog.Debugf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Infof(format string, args ...interface{}) {
|
|
|
|
globalLog.Infof(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Warnf(format string, args ...interface{}) {
|
|
|
|
globalLog.Warnf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Errorf(format string, args ...interface{}) {
|
|
|
|
globalLog.Errorf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fatalf(format string, args ...interface{}) {
|
|
|
|
globalLog.Fatalf(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Println(args ...interface{}) {
|
|
|
|
globalLog.Info(args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Printf(format string, args ...interface{}) {
|
|
|
|
globalLog.Infof(format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetLogger() *ZapLog {
|
|
|
|
return globalLog
|
|
|
|
}
|
|
|
|
|
|
|
|
func Flush() {
|
|
|
|
globalLog.Sync()
|
|
|
|
}
|