Compare commits
14 Commits
Author | SHA1 | Date |
---|---|---|
ehlxr | a15a40a425 | |
ehlxr | 08e6fdbd53 | |
ehlxr | 7eee167d91 | |
ehlxr | c03451395f | |
ehlxr | 48e8f68955 | |
ehlxr | 7b9983504f | |
ehlxr | 6ac40ca1a2 | |
ehlxr | ac33bdc69f | |
ehlxr | 63ba3dd7ab | |
ehlxr | aad0d71d1e | |
ehlxr | 4c129271a5 | |
ehlxr | 5820e0ded2 | |
ehlxr | 119fee49af | |
ehlxr | a2a63aebee |
|
@ -1 +1,6 @@
|
||||||
# logger
|
# logger
|
||||||
|
|
||||||
|
[![license](https://badgen.net/badge/license/MIT/blue)](./LICENSE)
|
||||||
|
[![](https://badgen.net/github/commits/ehlxr/log)](https://github.com/ehlxr/log/commits/)
|
||||||
|
[![](https://badgen.net/github/last-commit/ehlxr/log)]((https://github.com/ehlxr/log/commits/))
|
||||||
|
[![](https://badgen.net/github/releases/ehlxr/log)](https://github.com/ehlxr/log/releases)
|
||||||
|
|
|
@ -68,12 +68,13 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
|
||||||
if enc.TimeKey != "" && enc.EncodeTime != nil {
|
if enc.TimeKey != "" && enc.EncodeTime != nil {
|
||||||
enc.EncodeTime(ent.Time, arr)
|
enc.EncodeTime(ent.Time, arr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if enc.LevelKey != "" && enc.EncodeLevel != nil {
|
if enc.LevelKey != "" && enc.EncodeLevel != nil {
|
||||||
enc.EncodeLevel(ent.Level, arr)
|
enc.EncodeLevel(ent.Level, arr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ent.LoggerName != "" && enc.NameKey != "" {
|
if ent.LoggerName != "" && enc.NameKey != "" {
|
||||||
nameEncoder := enc.EncodeName
|
nameEncoder := enc.EncodeName
|
||||||
|
|
||||||
if nameEncoder == nil {
|
if nameEncoder == nil {
|
||||||
// Fall back to FullNameEncoder for backward compatibility.
|
// Fall back to FullNameEncoder for backward compatibility.
|
||||||
nameEncoder = zapcore.FullNameEncoder
|
nameEncoder = zapcore.FullNameEncoder
|
||||||
|
@ -81,17 +82,23 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
|
||||||
|
|
||||||
nameEncoder(ent.LoggerName, arr)
|
nameEncoder(ent.LoggerName, arr)
|
||||||
}
|
}
|
||||||
|
|
||||||
if ent.Caller.Defined && enc.CallerKey != "" && enc.EncodeCaller != nil {
|
if ent.Caller.Defined && enc.CallerKey != "" && enc.EncodeCaller != nil {
|
||||||
enc.EncodeCaller(ent.Caller, arr)
|
enc.EncodeCaller(ent.Caller, arr)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range arr.elems {
|
for i := range arr.elems {
|
||||||
// if i > 0 {
|
// if i > 0 {
|
||||||
// line.AppendByte('\t')
|
// line.AppendByte('\t')
|
||||||
// }
|
// }
|
||||||
_, _ = fmt.Fprint(line, arr.elems[i])
|
_, _ = fmt.Fprint(line, arr.elems[i])
|
||||||
}
|
}
|
||||||
|
|
||||||
putSliceEncoder(arr)
|
putSliceEncoder(arr)
|
||||||
|
|
||||||
|
// Add any structured context.
|
||||||
|
enc.writeContext(line, fields)
|
||||||
|
|
||||||
// Add the message itself.
|
// Add the message itself.
|
||||||
if enc.MessageKey != "" {
|
if enc.MessageKey != "" {
|
||||||
// c.addTabIfNecessary(line)
|
// c.addTabIfNecessary(line)
|
||||||
|
@ -99,9 +106,6 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
|
||||||
line.AppendString(ent.Message)
|
line.AppendString(ent.Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add any structured context.
|
|
||||||
enc.writeContext(line, fields)
|
|
||||||
|
|
||||||
// If there's no stacktrace key, honor that; this allows users to force
|
// If there's no stacktrace key, honor that; this allows users to force
|
||||||
// single-line output.
|
// single-line output.
|
||||||
if ent.Stack != "" && enc.StacktraceKey != "" {
|
if ent.Stack != "" && enc.StacktraceKey != "" {
|
||||||
|
@ -114,6 +118,7 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
|
||||||
} else {
|
} else {
|
||||||
line.AppendString(zapcore.DefaultLineEnding)
|
line.AppendString(zapcore.DefaultLineEnding)
|
||||||
}
|
}
|
||||||
|
|
||||||
return line, nil
|
return line, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,9 +133,10 @@ func (enc textEncoder) writeContext(line *buffer.Buffer, extra []zapcore.Field)
|
||||||
}
|
}
|
||||||
|
|
||||||
// c.addTabIfNecessary(line)
|
// c.addTabIfNecessary(line)
|
||||||
line.AppendByte('{')
|
// line.AppendByte('{')
|
||||||
|
line.AppendByte(' ')
|
||||||
_, _ = line.Write(context.buf.Bytes())
|
_, _ = line.Write(context.buf.Bytes())
|
||||||
line.AppendByte('}')
|
// line.AppendByte('}')
|
||||||
}
|
}
|
||||||
|
|
||||||
func (enc textEncoder) addTabIfNecessary(line *buffer.Buffer) {
|
func (enc textEncoder) addTabIfNecessary(line *buffer.Buffer) {
|
||||||
|
@ -216,11 +222,13 @@ func (enc *textEncoder) AddDuration(key string, val time.Duration) {
|
||||||
func (enc *textEncoder) AddFloat64(key string, val float64) {
|
func (enc *textEncoder) AddFloat64(key string, val float64) {
|
||||||
enc.addKey(key)
|
enc.addKey(key)
|
||||||
enc.appendFloat(val, 64)
|
enc.appendFloat(val, 64)
|
||||||
|
enc.buf.AppendByte('}')
|
||||||
}
|
}
|
||||||
func (enc *textEncoder) AddInt64(key string, val int64) {
|
func (enc *textEncoder) AddInt64(key string, val int64) {
|
||||||
enc.addKey(key)
|
enc.addKey(key)
|
||||||
enc.addElementSeparator()
|
enc.addElementSeparator()
|
||||||
enc.buf.AppendInt(val)
|
enc.buf.AppendInt(val)
|
||||||
|
enc.buf.AppendByte('}')
|
||||||
}
|
}
|
||||||
func (enc *textEncoder) AddReflected(key string, obj interface{}) error {
|
func (enc *textEncoder) AddReflected(key string, obj interface{}) error {
|
||||||
enc.resetReflectBuf()
|
enc.resetReflectBuf()
|
||||||
|
@ -241,9 +249,10 @@ func (enc *textEncoder) OpenNamespace(key string) {
|
||||||
func (enc *textEncoder) AddString(key, val string) {
|
func (enc *textEncoder) AddString(key, val string) {
|
||||||
enc.addKey(key)
|
enc.addKey(key)
|
||||||
enc.addElementSeparator()
|
enc.addElementSeparator()
|
||||||
enc.buf.AppendByte('"')
|
// enc.buf.AppendByte('"')
|
||||||
enc.safeAddString(val)
|
enc.safeAddString(val)
|
||||||
enc.buf.AppendByte('"')
|
// enc.buf.AppendByte('"')
|
||||||
|
enc.buf.AppendByte('}')
|
||||||
}
|
}
|
||||||
func (enc *textEncoder) AddTime(key string, val time.Time) {
|
func (enc *textEncoder) AddTime(key string, val time.Time) {
|
||||||
enc.addKey(key)
|
enc.addKey(key)
|
||||||
|
@ -261,6 +270,7 @@ func (enc *textEncoder) AddUint64(key string, val uint64) {
|
||||||
enc.addKey(key)
|
enc.addKey(key)
|
||||||
enc.addElementSeparator()
|
enc.addElementSeparator()
|
||||||
enc.buf.AppendUint(val)
|
enc.buf.AppendUint(val)
|
||||||
|
enc.buf.AppendByte('}')
|
||||||
}
|
}
|
||||||
|
|
||||||
//noinspection GoRedundantConversion
|
//noinspection GoRedundantConversion
|
||||||
|
@ -293,15 +303,39 @@ func (enc *textEncoder) clone() *textEncoder {
|
||||||
|
|
||||||
func (enc *textEncoder) addKey(key string) {
|
func (enc *textEncoder) addKey(key string) {
|
||||||
enc.addElementSeparator()
|
enc.addElementSeparator()
|
||||||
enc.buf.AppendByte('"')
|
// enc.buf.AppendByte('"')
|
||||||
|
|
||||||
|
if enc.replaceSeparator('}') {
|
||||||
|
enc.buf.AppendByte(',')
|
||||||
|
enc.buf.AppendByte(' ')
|
||||||
|
} else {
|
||||||
|
enc.buf.AppendByte('{')
|
||||||
|
}
|
||||||
enc.safeAddString(key)
|
enc.safeAddString(key)
|
||||||
enc.buf.AppendByte('"')
|
// enc.buf.AppendByte('"')
|
||||||
enc.buf.AppendByte(':')
|
// enc.buf.AppendByte(':')
|
||||||
|
enc.buf.AppendByte('=')
|
||||||
if enc.spaced {
|
if enc.spaced {
|
||||||
enc.buf.AppendByte(' ')
|
enc.buf.AppendByte(' ')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (enc *textEncoder) replaceSeparator(v byte) bool {
|
||||||
|
last := enc.buf.Len() - 1
|
||||||
|
if last < 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if enc.buf.Bytes()[last] == v {
|
||||||
|
t := enc.buf.Bytes()[:last]
|
||||||
|
|
||||||
|
enc.buf.Reset()
|
||||||
|
_, _ = enc.buf.Write(t)
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (enc *textEncoder) addElementSeparator() {
|
func (enc *textEncoder) addElementSeparator() {
|
||||||
last := enc.buf.Len() - 1
|
last := enc.buf.Len() - 1
|
||||||
if last < 0 {
|
if last < 0 {
|
||||||
|
@ -311,7 +345,7 @@ func (enc *textEncoder) addElementSeparator() {
|
||||||
case '{', '[', ':', ',', ' ':
|
case '{', '[', ':', ',', ' ':
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
enc.buf.AppendByte(',')
|
// enc.buf.AppendByte(',')
|
||||||
if enc.spaced {
|
if enc.spaced {
|
||||||
enc.buf.AppendByte(' ')
|
enc.buf.AppendByte(' ')
|
||||||
}
|
}
|
||||||
|
|
11
go.mod
11
go.mod
|
@ -3,8 +3,11 @@ module github.com/ehlxr/log
|
||||||
go 1.13
|
go 1.13
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/ehlxr/lumberjack v2.0.0+incompatible
|
github.com/ehlxr/lumberjack v0.0.2-0.20200107093220-2a579f1b2e4d
|
||||||
github.com/robfig/cron/v3 v3.0.0
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
go.uber.org/zap v1.12.0
|
go.uber.org/multierr v1.6.0 // indirect
|
||||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
|
go.uber.org/zap v1.16.0
|
||||||
|
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect
|
||||||
|
golang.org/x/tools v0.0.0-20210104081019-d8d6ddbec6ee // indirect
|
||||||
|
honnef.co/go/tools v0.1.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
|
@ -35,15 +35,15 @@ const (
|
||||||
White
|
White
|
||||||
)
|
)
|
||||||
|
|
||||||
func (lc *logConfig) initColor() {
|
func (config *logConfig) initColor() {
|
||||||
for level, color := range _levelToColor {
|
for level, color := range _levelToColor {
|
||||||
lcs := level.String()
|
lcs := level.String()
|
||||||
|
|
||||||
if lc.EnableCapitalLevel {
|
if config.EnableCapitalLevel {
|
||||||
lcs = level.CapitalString()
|
lcs = level.CapitalString()
|
||||||
}
|
}
|
||||||
|
|
||||||
if lc.EnableLevelTruncation {
|
if config.EnableLevelTruncation {
|
||||||
lcs = lcs[:4]
|
lcs = lcs[:4]
|
||||||
}
|
}
|
||||||
_levelToColorStrings[level] = color.Add(lcs)
|
_levelToColorStrings[level] = color.Add(lcs)
|
||||||
|
|
102
log.go
102
log.go
|
@ -35,6 +35,7 @@ type logConfig struct {
|
||||||
CrashLogFilename string
|
CrashLogFilename string
|
||||||
ErrorLogFilename string
|
ErrorLogFilename string
|
||||||
EnableLineNumber bool
|
EnableLineNumber bool
|
||||||
|
AddCallerSkip int
|
||||||
|
|
||||||
// enable the truncation of the level text to 4 characters.
|
// enable the truncation of the level text to 4 characters.
|
||||||
EnableLevelTruncation bool
|
EnableLevelTruncation bool
|
||||||
|
@ -43,22 +44,27 @@ type logConfig struct {
|
||||||
EnableCapitalLevel bool
|
EnableCapitalLevel bool
|
||||||
atomicLevel zap.AtomicLevel
|
atomicLevel zap.AtomicLevel
|
||||||
Name string
|
Name string
|
||||||
|
Fields []zap.Field
|
||||||
|
|
||||||
*lumberjack.Logger
|
*lumberjack.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
lc := &logConfig{
|
config := &logConfig{
|
||||||
Logger: &lumberjack.Logger{
|
Logger: &lumberjack.Logger{
|
||||||
LocalTime: true,
|
LocalTime: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
logger = lc.newLogger().Sugar()
|
logger = config.newLogger().Sugar()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lc *logConfig) Init() {
|
func (config *logConfig) Init() {
|
||||||
logger = lc.newLogger().Sugar()
|
logger = config.newLogger().Sugar()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *logConfig) New() *zap.SugaredLogger {
|
||||||
|
return config.newLogger().Sugar()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLogConfig() *logConfig {
|
func NewLogConfig() *logConfig {
|
||||||
|
@ -84,54 +90,54 @@ func NewLogConfig() *logConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lc *logConfig) newLogger() *zap.Logger {
|
func (config *logConfig) newLogger() *zap.Logger {
|
||||||
if lc.CrashLogFilename != "" {
|
if config.CrashLogFilename != "" {
|
||||||
writeCrashLog(lc.CrashLogFilename)
|
writeCrashLog(config.CrashLogFilename)
|
||||||
}
|
}
|
||||||
|
|
||||||
lc.atomicLevel = zap.NewAtomicLevelAt(lc.Level)
|
config.atomicLevel = zap.NewAtomicLevelAt(config.Level)
|
||||||
lc.initColor()
|
config.initColor()
|
||||||
|
|
||||||
cores := []zapcore.Core{
|
cores := []zapcore.Core{
|
||||||
zapcore.NewCore(
|
zapcore.NewCore(
|
||||||
encoder.NewTextEncoder(lc.encoderConfig()),
|
encoder.NewTextEncoder(config.encoderConfig()),
|
||||||
zapcore.Lock(os.Stdout),
|
zapcore.Lock(os.Stdout),
|
||||||
lc.atomicLevel,
|
config.atomicLevel,
|
||||||
)}
|
)}
|
||||||
|
|
||||||
if lc.Filename != "" {
|
if config.Filename != "" {
|
||||||
cores = append(cores, lc.fileCore())
|
cores = append(cores, config.fileCore())
|
||||||
}
|
}
|
||||||
|
|
||||||
if lc.ErrorLogFilename != "" {
|
if config.ErrorLogFilename != "" {
|
||||||
cores = append(cores, lc.errorFileCore())
|
cores = append(cores, config.errorFileCore())
|
||||||
}
|
}
|
||||||
|
|
||||||
core := zapcore.NewTee(cores...)
|
core := zapcore.NewTee(cores...)
|
||||||
|
|
||||||
var options []zap.Option
|
var options []zap.Option
|
||||||
|
|
||||||
if lc.EnableLineNumber {
|
if config.EnableLineNumber {
|
||||||
options = append(options, zap.AddCaller(), zap.AddCallerSkip(1))
|
options = append(options, zap.AddCaller(), zap.AddCallerSkip(config.AddCallerSkip))
|
||||||
}
|
}
|
||||||
|
|
||||||
if lc.EnableErrorStacktrace {
|
if config.EnableErrorStacktrace {
|
||||||
options = append(options, zap.AddStacktrace(zapcore.ErrorLevel))
|
options = append(options, zap.AddStacktrace(zapcore.ErrorLevel))
|
||||||
}
|
}
|
||||||
|
|
||||||
zapLog := zap.New(core, options...)
|
zapLog := zap.New(core, options...)
|
||||||
|
|
||||||
if lc.Name != "" {
|
if config.Name != "" {
|
||||||
zapLog = zapLog.Named(fmt.Sprintf("[%s]", lc.Name))
|
zapLog = zapLog.Named(fmt.Sprintf("[%s]", config.Name))
|
||||||
}
|
}
|
||||||
|
|
||||||
return zapLog
|
return zapLog.With(config.Fields...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lc *logConfig) encoderConfig() zapcore.EncoderConfig {
|
func (config *logConfig) encoderConfig() zapcore.EncoderConfig {
|
||||||
el := lc.encodeLevel
|
el := config.encodeLevel
|
||||||
if lc.EnableColors {
|
if config.EnableColors {
|
||||||
el = lc.encodeColorLevel
|
el = config.encodeColorLevel
|
||||||
}
|
}
|
||||||
|
|
||||||
return zapcore.EncoderConfig{
|
return zapcore.EncoderConfig{
|
||||||
|
@ -145,8 +151,8 @@ func (lc *logConfig) encoderConfig() zapcore.EncoderConfig {
|
||||||
EncodeLevel: el,
|
EncodeLevel: el,
|
||||||
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
|
||||||
tf := time.RFC3339
|
tf := time.RFC3339
|
||||||
if lc.TimestampFormat != "" {
|
if config.TimestampFormat != "" {
|
||||||
tf = lc.TimestampFormat
|
tf = config.TimestampFormat
|
||||||
}
|
}
|
||||||
enc.AppendString(t.Format(fmt.Sprintf("[%s]", tf)))
|
enc.AppendString(t.Format(fmt.Sprintf("[%s]", tf)))
|
||||||
},
|
},
|
||||||
|
@ -157,40 +163,40 @@ func (lc *logConfig) encoderConfig() zapcore.EncoderConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lc *logConfig) fileCore() zapcore.Core {
|
func (config *logConfig) fileCore() zapcore.Core {
|
||||||
return zapcore.NewCore(
|
return zapcore.NewCore(
|
||||||
encoder.NewTextEncoder(lc.encoderConfig()),
|
encoder.NewTextEncoder(config.encoderConfig()),
|
||||||
// zapcore.NewMultiWriteSyncer(
|
// zapcore.NewMultiWriteSyncer(
|
||||||
// zapcore.Lock(os.Stdout),
|
// zapcore.Lock(os.Stdout),
|
||||||
// lc.fileWriteSyncer(),
|
// config.fileWriteSyncer(),
|
||||||
// ),
|
// ),
|
||||||
lc.fileWriteSyncer(lc.Filename),
|
config.fileWriteSyncer(config.Filename),
|
||||||
lc.atomicLevel,
|
config.atomicLevel,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lc *logConfig) errorFileCore() zapcore.Core {
|
func (config *logConfig) errorFileCore() zapcore.Core {
|
||||||
return zapcore.NewCore(
|
return zapcore.NewCore(
|
||||||
encoder.NewTextEncoder(lc.encoderConfig()),
|
encoder.NewTextEncoder(config.encoderConfig()),
|
||||||
|
|
||||||
lc.fileWriteSyncer(lc.ErrorLogFilename),
|
config.fileWriteSyncer(config.ErrorLogFilename),
|
||||||
zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
|
zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
|
||||||
return lvl >= zapcore.ErrorLevel
|
return lvl >= zapcore.ErrorLevel
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lc *logConfig) encodeLevel(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
|
func (config *logConfig) encodeLevel(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
|
||||||
levelString := l.CapitalString()
|
levelString := l.CapitalString()
|
||||||
|
|
||||||
if lc.EnableLevelTruncation {
|
if config.EnableLevelTruncation {
|
||||||
levelString = levelString[:4]
|
levelString = levelString[:4]
|
||||||
}
|
}
|
||||||
|
|
||||||
enc.AppendString(fmt.Sprintf("[%s]", levelString))
|
enc.AppendString(fmt.Sprintf("[%s]", levelString))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lc *logConfig) encodeColorLevel(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
|
func (config *logConfig) encodeColorLevel(l zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
|
||||||
s, ok := _levelToColorStrings[l]
|
s, ok := _levelToColorStrings[l]
|
||||||
if !ok {
|
if !ok {
|
||||||
s = _unknownLevelColor.Add(l.CapitalString())
|
s = _unknownLevelColor.Add(l.CapitalString())
|
||||||
|
@ -219,7 +225,7 @@ func trimCallerFilePath(ec zapcore.EntryCaller) string {
|
||||||
return fmt.Sprintf(" %s", caller)
|
return fmt.Sprintf(" %s", caller)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lc *logConfig) fileWriteSyncer(fileName string) zapcore.WriteSyncer {
|
func (config *logConfig) fileWriteSyncer(fileName string) zapcore.WriteSyncer {
|
||||||
// go get github.com/lestrrat-go/file-rotatelogs
|
// go get github.com/lestrrat-go/file-rotatelogs
|
||||||
// writer, err := rotatelogs.New(
|
// writer, err := rotatelogs.New(
|
||||||
// name+".%Y%m%d",
|
// name+".%Y%m%d",
|
||||||
|
@ -233,16 +239,16 @@ func (lc *logConfig) fileWriteSyncer(fileName string) zapcore.WriteSyncer {
|
||||||
|
|
||||||
writer := &lumberjack.Logger{
|
writer := &lumberjack.Logger{
|
||||||
Filename: fileName,
|
Filename: fileName,
|
||||||
MaxSize: lc.MaxSize, // 单个日志文件大小(MB)
|
MaxSize: config.MaxSize, // 单个日志文件大小(MB)
|
||||||
MaxBackups: lc.MaxBackups,
|
MaxBackups: config.MaxBackups,
|
||||||
MaxAge: lc.MaxAge, // 保留多少天的日志
|
MaxAge: config.MaxAge, // 保留多少天的日志
|
||||||
LocalTime: lc.LocalTime,
|
LocalTime: config.LocalTime,
|
||||||
Compress: lc.Compress,
|
Compress: config.Compress,
|
||||||
BackupTimeFormat: lc.BackupTimeFormat,
|
BackupTimeFormat: config.BackupTimeFormat,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rotating log files daily
|
// Rotating log files daily
|
||||||
runner := cron.New(cron.WithSeconds(), cron.WithLocation(time.UTC))
|
runner := cron.New(cron.WithSeconds(), cron.WithLocation(time.Local))
|
||||||
_, _ = runner.AddFunc("0 0 0 * * ?", func() {
|
_, _ = runner.AddFunc("0 0 0 * * ?", func() {
|
||||||
_ = writer.Rotate(time.Now().AddDate(0, 0, -1))
|
_ = writer.Rotate(time.Now().AddDate(0, 0, -1))
|
||||||
})
|
})
|
||||||
|
@ -264,3 +270,7 @@ func writeCrashLog(file string) {
|
||||||
func Fields(args ...interface{}) {
|
func Fields(args ...interface{}) {
|
||||||
logger = logger.With(args...)
|
logger = logger.With(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func With(l *zap.SugaredLogger, args ...interface{}) *zap.SugaredLogger {
|
||||||
|
return l.With(args...)
|
||||||
|
}
|
||||||
|
|
29
log_test.go
29
log_test.go
|
@ -3,6 +3,8 @@ package log
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLog(t *testing.T) {
|
func TestLog(t *testing.T) {
|
||||||
|
@ -13,17 +15,34 @@ func TestLog(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLogWithConfig(t *testing.T) {
|
func TestLogWithConfig(t *testing.T) {
|
||||||
lc := NewLogConfig()
|
config := NewLogConfig()
|
||||||
_ = lc.Level.Set("info")
|
_ = config.Level.Set("debug")
|
||||||
lc.Name = "main"
|
config.Name = "main"
|
||||||
lc.Init()
|
// config.Fields = []zap.Field{zap.String("traceid", "12123123123")}
|
||||||
|
|
||||||
|
config.Init()
|
||||||
|
|
||||||
|
Fields("traceid", float64(21221212122))
|
||||||
Debugf("this is %s message", "debug")
|
Debugf("this is %s message", "debug")
|
||||||
|
config.Init()
|
||||||
|
Fields(zap.String("traceid", "12123123123"))
|
||||||
Infof("this is %s message", "info")
|
Infof("this is %s message", "info")
|
||||||
Errorf("this is %s message", "error")
|
// Errorf("this is %s message", "error")
|
||||||
// Panicf("this is %s message", "panic")
|
// Panicf("this is %s message", "panic")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLogWithNew(t *testing.T) {
|
||||||
|
config := NewLogConfig()
|
||||||
|
_ = config.Level.Set("debug")
|
||||||
|
config.Name = "main"
|
||||||
|
|
||||||
|
logger := config.New()
|
||||||
|
|
||||||
|
log := With(logger, "traceid", float64(21221212122), "request", "[POST]/hello/v2")
|
||||||
|
log.Debugf("this is %s message", "debug")
|
||||||
|
log.Infof("this is %s message", "info")
|
||||||
|
}
|
||||||
|
|
||||||
func TestLogRote(t *testing.T) {
|
func TestLogRote(t *testing.T) {
|
||||||
lc := NewLogConfig()
|
lc := NewLogConfig()
|
||||||
lc.MaxSize = 1
|
lc.MaxSize = 1
|
||||||
|
|
Loading…
Reference in New Issue