Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
ehlxr | a15a40a425 | |
ehlxr | 08e6fdbd53 | |
ehlxr | 7eee167d91 |
|
@ -55,7 +55,7 @@ func NewTextEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder {
|
|||
return &textEncoder{
|
||||
EncoderConfig: &cfg,
|
||||
buf: bufferpool.Get(),
|
||||
spaced: true,
|
||||
spaced: false,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -222,13 +222,13 @@ func (enc *textEncoder) AddDuration(key string, val time.Duration) {
|
|||
func (enc *textEncoder) AddFloat64(key string, val float64) {
|
||||
enc.addKey(key)
|
||||
enc.appendFloat(val, 64)
|
||||
enc.buf.AppendByte(']')
|
||||
enc.buf.AppendByte('}')
|
||||
}
|
||||
func (enc *textEncoder) AddInt64(key string, val int64) {
|
||||
enc.addKey(key)
|
||||
enc.addElementSeparator()
|
||||
enc.buf.AppendInt(val)
|
||||
enc.buf.AppendByte(']')
|
||||
enc.buf.AppendByte('}')
|
||||
}
|
||||
func (enc *textEncoder) AddReflected(key string, obj interface{}) error {
|
||||
enc.resetReflectBuf()
|
||||
|
@ -252,7 +252,7 @@ func (enc *textEncoder) AddString(key, val string) {
|
|||
// enc.buf.AppendByte('"')
|
||||
enc.safeAddString(val)
|
||||
// enc.buf.AppendByte('"')
|
||||
enc.buf.AppendByte(']')
|
||||
enc.buf.AppendByte('}')
|
||||
}
|
||||
func (enc *textEncoder) AddTime(key string, val time.Time) {
|
||||
enc.addKey(key)
|
||||
|
@ -270,7 +270,7 @@ func (enc *textEncoder) AddUint64(key string, val uint64) {
|
|||
enc.addKey(key)
|
||||
enc.addElementSeparator()
|
||||
enc.buf.AppendUint(val)
|
||||
enc.buf.AppendByte(']')
|
||||
enc.buf.AppendByte('}')
|
||||
}
|
||||
|
||||
//noinspection GoRedundantConversion
|
||||
|
@ -304,15 +304,38 @@ func (enc *textEncoder) clone() *textEncoder {
|
|||
func (enc *textEncoder) addKey(key string) {
|
||||
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.buf.AppendByte('"')
|
||||
enc.buf.AppendByte(':')
|
||||
// enc.buf.AppendByte(':')
|
||||
enc.buf.AppendByte('=')
|
||||
if enc.spaced {
|
||||
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() {
|
||||
last := enc.buf.Len() - 1
|
||||
if last < 0 {
|
||||
|
@ -322,7 +345,7 @@ func (enc *textEncoder) addElementSeparator() {
|
|||
case '{', '[', ':', ',', ' ':
|
||||
return
|
||||
default:
|
||||
enc.buf.AppendByte(',')
|
||||
// enc.buf.AppendByte(',')
|
||||
if enc.spaced {
|
||||
enc.buf.AppendByte(' ')
|
||||
}
|
||||
|
|
8
go.mod
8
go.mod
|
@ -4,6 +4,10 @@ go 1.13
|
|||
|
||||
require (
|
||||
github.com/ehlxr/lumberjack v0.0.2-0.20200107093220-2a579f1b2e4d
|
||||
github.com/robfig/cron/v3 v3.0.0
|
||||
go.uber.org/zap v1.12.0
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
go.uber.org/multierr v1.6.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
|
||||
)
|
||||
|
|
11
log.go
11
log.go
|
@ -35,6 +35,7 @@ type logConfig struct {
|
|||
CrashLogFilename string
|
||||
ErrorLogFilename string
|
||||
EnableLineNumber bool
|
||||
AddCallerSkip int
|
||||
|
||||
// enable the truncation of the level text to 4 characters.
|
||||
EnableLevelTruncation bool
|
||||
|
@ -62,6 +63,10 @@ func (config *logConfig) Init() {
|
|||
logger = config.newLogger().Sugar()
|
||||
}
|
||||
|
||||
func (config *logConfig) New() *zap.SugaredLogger {
|
||||
return config.newLogger().Sugar()
|
||||
}
|
||||
|
||||
func NewLogConfig() *logConfig {
|
||||
return &logConfig{
|
||||
Level: DebugLevel,
|
||||
|
@ -113,7 +118,7 @@ func (config *logConfig) newLogger() *zap.Logger {
|
|||
var options []zap.Option
|
||||
|
||||
if config.EnableLineNumber {
|
||||
options = append(options, zap.AddCaller(), zap.AddCallerSkip(1))
|
||||
options = append(options, zap.AddCaller(), zap.AddCallerSkip(config.AddCallerSkip))
|
||||
}
|
||||
|
||||
if config.EnableErrorStacktrace {
|
||||
|
@ -265,3 +270,7 @@ func writeCrashLog(file string) {
|
|||
func Fields(args ...interface{}) {
|
||||
logger = logger.With(args...)
|
||||
}
|
||||
|
||||
func With(l *zap.SugaredLogger, args ...interface{}) *zap.SugaredLogger {
|
||||
return l.With(args...)
|
||||
}
|
||||
|
|
19
log_test.go
19
log_test.go
|
@ -1,9 +1,10 @@
|
|||
package log
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func TestLog(t *testing.T) {
|
||||
|
@ -21,15 +22,27 @@ func TestLogWithConfig(t *testing.T) {
|
|||
|
||||
config.Init()
|
||||
|
||||
With("traceid", float64(21221212122))
|
||||
Fields("traceid", float64(21221212122))
|
||||
Debugf("this is %s message", "debug")
|
||||
config.Init()
|
||||
With(zap.String("traceid", "12123123123"))
|
||||
Fields(zap.String("traceid", "12123123123"))
|
||||
Infof("this is %s message", "info")
|
||||
// Errorf("this is %s message", "error")
|
||||
// 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) {
|
||||
lc := NewLogConfig()
|
||||
lc.MaxSize = 1
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
package log
|
||||
|
||||
func With(args ...interface{}) {
|
||||
logger = logger.With(args...)
|
||||
}
|
||||
|
||||
func Debug(args ...interface{}) {
|
||||
logger.Debug(args...)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue