log/log.go

277 lines
6.6 KiB
Go
Raw Normal View History

2019-11-05 07:57:09 +00:00
package log
import (
"fmt"
2019-11-08 09:45:09 +00:00
"github.com/ehlxr/lumberjack"
2019-11-05 07:57:09 +00:00
"log"
"os"
"path"
"strings"
"time"
2019-11-05 08:03:03 +00:00
"github.com/ehlxr/log/bufferpool"
"github.com/ehlxr/log/crash"
"github.com/ehlxr/log/encoder"
"github.com/robfig/cron/v3"
2019-11-05 07:57:09 +00:00
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var logger *zap.SugaredLogger
const (
DebugLevel = zapcore.DebugLevel
InfoLevel = zapcore.InfoLevel
WarnLevel = zapcore.WarnLevel
ErrorLevel = zapcore.ErrorLevel
DPanicLevel = zapcore.DPanicLevel
PanicLevel = zapcore.PanicLevel
FatalLevel = zapcore.FatalLevel
)
type logConfig struct {
Level zapcore.Level
EnableColors bool
CrashLogFilename string
ErrorLogFilename string
2019-11-05 07:57:09 +00:00
EnableLineNumber bool
2021-01-01 15:12:00 +00:00
AddCallerSkip int
2019-11-05 07:57:09 +00:00
// enable the truncation of the level text to 4 characters.
EnableLevelTruncation bool
EnableErrorStacktrace bool
TimestampFormat string
EnableCapitalLevel bool
atomicLevel zap.AtomicLevel
Name string
2020-12-31 08:21:31 +00:00
Fields []zap.Field
*lumberjack.Logger
2019-11-05 07:57:09 +00:00
}
func init() {
2020-12-31 08:21:31 +00:00
config := &logConfig{
Logger: &lumberjack.Logger{
LocalTime: true,
},
2019-11-05 07:57:09 +00:00
}
2020-12-31 08:21:31 +00:00
logger = config.newLogger().Sugar()
2019-11-05 07:57:09 +00:00
}
2020-12-31 08:21:31 +00:00
func (config *logConfig) Init() {
logger = config.newLogger().Sugar()
2019-11-05 07:57:09 +00:00
}
2021-01-01 15:12:00 +00:00
func (config *logConfig) New() *zap.SugaredLogger {
return config.newLogger().Sugar()
}
2019-11-05 07:57:09 +00:00
func NewLogConfig() *logConfig {
2019-11-08 09:45:09 +00:00
return &logConfig{
2019-11-05 07:57:09 +00:00
Level: DebugLevel,
EnableColors: true,
CrashLogFilename: "./logs/crash.log",
ErrorLogFilename: "./logs/error.log",
2019-11-05 07:57:09 +00:00
EnableLineNumber: true,
EnableLevelTruncation: true,
EnableErrorStacktrace: true,
TimestampFormat: "2006-01-02 15:04:05.000",
EnableCapitalLevel: true,
Logger: &lumberjack.Logger{
2019-11-08 09:45:09 +00:00
Filename: "./logs/log.log",
MaxSize: 200,
MaxAge: 0,
MaxBackups: 30,
LocalTime: true,
Compress: false,
BackupTimeFormat: "2006-01-02",
},
2019-11-05 07:57:09 +00:00
}
}
2020-12-31 08:21:31 +00:00
func (config *logConfig) newLogger() *zap.Logger {
if config.CrashLogFilename != "" {
writeCrashLog(config.CrashLogFilename)
2019-11-05 07:57:09 +00:00
}
2020-12-31 08:21:31 +00:00
config.atomicLevel = zap.NewAtomicLevelAt(config.Level)
config.initColor()
2019-11-05 07:57:09 +00:00
cores := []zapcore.Core{
zapcore.NewCore(
2020-12-31 08:21:31 +00:00
encoder.NewTextEncoder(config.encoderConfig()),
2019-11-05 07:57:09 +00:00
zapcore.Lock(os.Stdout),
2020-12-31 08:21:31 +00:00
config.atomicLevel,
2019-11-05 07:57:09 +00:00
)}
2020-12-31 08:21:31 +00:00
if config.Filename != "" {
cores = append(cores, config.fileCore())
2019-11-05 07:57:09 +00:00
}
2020-12-31 08:21:31 +00:00
if config.ErrorLogFilename != "" {
cores = append(cores, config.errorFileCore())
2019-11-05 07:57:09 +00:00
}
core := zapcore.NewTee(cores...)
var options []zap.Option
2020-12-31 08:21:31 +00:00
if config.EnableLineNumber {
2021-01-01 15:12:00 +00:00
options = append(options, zap.AddCaller(), zap.AddCallerSkip(config.AddCallerSkip))
2019-11-05 07:57:09 +00:00
}
2020-12-31 08:21:31 +00:00
if config.EnableErrorStacktrace {
2019-11-05 07:57:09 +00:00
options = append(options, zap.AddStacktrace(zapcore.ErrorLevel))
}
zapLog := zap.New(core, options...)
2020-12-31 08:21:31 +00:00
if config.Name != "" {
zapLog = zapLog.Named(fmt.Sprintf("[%s]", config.Name))
2019-11-05 07:57:09 +00:00
}
2020-12-31 08:21:31 +00:00
return zapLog.With(config.Fields...)
2019-11-05 07:57:09 +00:00
}
2020-12-31 08:21:31 +00:00
func (config *logConfig) encoderConfig() zapcore.EncoderConfig {
el := config.encodeLevel
if config.EnableColors {
el = config.encodeColorLevel
2019-11-05 07:57:09 +00:00
}
return zapcore.EncoderConfig{
TimeKey: "T",
LevelKey: "L",
NameKey: "N",
CallerKey: "C",
MessageKey: "M",
StacktraceKey: "S",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: el,
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
tf := time.RFC3339
2020-12-31 08:21:31 +00:00
if config.TimestampFormat != "" {
tf = config.TimestampFormat
2019-11-05 07:57:09 +00:00
}
enc.AppendString(t.Format(fmt.Sprintf("[%s]", tf)))
},
EncodeDuration: zapcore.StringDurationEncoder,
EncodeCaller: func(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(trimCallerFilePath(caller))
},
}
}
2020-12-31 08:21:31 +00:00
func (config *logConfig) fileCore() zapcore.Core {
2019-11-05 07:57:09 +00:00
return zapcore.NewCore(
2020-12-31 08:21:31 +00:00
encoder.NewTextEncoder(config.encoderConfig()),
2019-11-05 07:57:09 +00:00
// zapcore.NewMultiWriteSyncer(
// zapcore.Lock(os.Stdout),
2020-12-31 08:21:31 +00:00
// config.fileWriteSyncer(),
2019-11-05 07:57:09 +00:00
// ),
2020-12-31 08:21:31 +00:00
config.fileWriteSyncer(config.Filename),
config.atomicLevel,
2019-11-05 07:57:09 +00:00
)
}
2020-12-31 08:21:31 +00:00
func (config *logConfig) errorFileCore() zapcore.Core {
2019-11-05 07:57:09 +00:00
return zapcore.NewCore(
2020-12-31 08:21:31 +00:00
encoder.NewTextEncoder(config.encoderConfig()),
2019-11-05 07:57:09 +00:00
2020-12-31 08:21:31 +00:00
config.fileWriteSyncer(config.ErrorLogFilename),
2019-11-05 07:57:09 +00:00
zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapcore.ErrorLevel
}),
)
}
2020-12-31 08:21:31 +00:00
func (config *logConfig) encodeLevel(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
2019-11-05 07:57:09 +00:00
levelString := l.CapitalString()
2020-12-31 08:21:31 +00:00
if config.EnableLevelTruncation {
2019-11-05 07:57:09 +00:00
levelString = levelString[:4]
}
enc.AppendString(fmt.Sprintf("[%s]", levelString))
}
2020-12-31 08:21:31 +00:00
func (config *logConfig) encodeColorLevel(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
2019-11-05 07:57:09 +00:00
s, ok := _levelToColorStrings[l]
if !ok {
s = _unknownLevelColor.Add(l.CapitalString())
}
enc.AppendString(fmt.Sprintf("[%s]", s))
}
func trimCallerFilePath(ec zapcore.EntryCaller) string {
if !ec.Defined {
return "undefined"
}
// Find the last separator.
idx := strings.LastIndexByte(ec.File, '/')
if idx == -1 {
return ec.FullPath()
}
buf := bufferpool.Get()
buf.AppendString(ec.File[idx+1:])
buf.AppendByte(':')
buf.AppendInt(int64(ec.Line))
caller := buf.String()
buf.Free()
return fmt.Sprintf(" %s", caller)
}
2020-12-31 08:21:31 +00:00
func (config *logConfig) fileWriteSyncer(fileName string) zapcore.WriteSyncer {
2019-11-06 02:12:49 +00:00
// go get github.com/lestrrat-go/file-rotatelogs
2019-11-06 02:11:12 +00:00
// writer, err := rotatelogs.New(
// name+".%Y%m%d",
// rotatelogs.WithLinkName(name), // 生成软链,指向最新日志文件
// rotatelogs.WithMaxAge(7*24*time.Hour), // 文件最大保存时间
// rotatelogs.WithRotationTime(24*time.Hour), // 日志切割时间间隔
// )
// if err != nil {
// log.Fatalf("config normal logger file error. %v", errors.WithStack(err))
// }
writer := &lumberjack.Logger{
2019-11-08 09:45:09 +00:00
Filename: fileName,
2020-12-31 08:21:31 +00:00
MaxSize: config.MaxSize, // 单个日志文件大小MB
MaxBackups: config.MaxBackups,
MaxAge: config.MaxAge, // 保留多少天的日志
LocalTime: config.LocalTime,
Compress: config.Compress,
BackupTimeFormat: config.BackupTimeFormat,
2019-11-06 02:11:12 +00:00
}
// Rotating log files daily
2019-11-11 01:32:36 +00:00
runner := cron.New(cron.WithSeconds(), cron.WithLocation(time.Local))
2019-11-08 09:45:09 +00:00
_, _ = runner.AddFunc("0 0 0 * * ?", func() {
_ = writer.Rotate(time.Now().AddDate(0, 0, -1))
})
go runner.Run()
2019-11-06 02:11:12 +00:00
return zapcore.AddSync(writer)
2019-11-05 07:57:09 +00:00
}
func writeCrashLog(file string) {
err := os.MkdirAll(path.Dir(file), os.ModePerm)
if err != nil {
2019-11-06 02:12:49 +00:00
log.Fatalf("make crash log dir error. %v",
2019-11-08 09:45:09 +00:00
err)
2019-11-05 07:57:09 +00:00
}
2019-11-06 02:11:12 +00:00
crash.NewCrashLog(file)
2019-11-05 07:57:09 +00:00
}
func Fields(args ...interface{}) {
logger = logger.With(args...)
}
2021-01-01 15:12:00 +00:00
func With(l *zap.SugaredLogger, args ...interface{}) *zap.SugaredLogger {
return l.With(args...)
}