3 Commits

Author SHA1 Message Date
08e6fdbd53 update at 2021-01-04 15:51:39 by ehlxr 2021-01-04 15:51:39 +08:00
7eee167d91 update at 2021-01-01 23:12:00 by ehlxr 2021-01-01 23:12:00 +08:00
c03451395f modify text encoder format bug 2020-12-31 17:26:35 +08:00
5 changed files with 44 additions and 13 deletions

View File

@@ -55,7 +55,7 @@ func NewTextEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder {
return &textEncoder{ return &textEncoder{
EncoderConfig: &cfg, EncoderConfig: &cfg,
buf: bufferpool.Get(), buf: bufferpool.Get(),
spaced: true, spaced: false,
} }
} }
@@ -222,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()
@@ -250,7 +252,7 @@ func (enc *textEncoder) AddString(key, val string) {
// enc.buf.AppendByte('"') // enc.buf.AppendByte('"')
enc.safeAddString(val) enc.safeAddString(val)
// enc.buf.AppendByte('"') // 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)
@@ -268,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
@@ -301,15 +304,38 @@ 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('"')
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 {
@@ -319,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
log.go
View File

@@ -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
@@ -62,6 +63,10 @@ func (config *logConfig) Init() {
logger = config.newLogger().Sugar() logger = config.newLogger().Sugar()
} }
func (config *logConfig) New() *zap.SugaredLogger {
return config.newLogger().Sugar()
}
func NewLogConfig() *logConfig { func NewLogConfig() *logConfig {
return &logConfig{ return &logConfig{
Level: DebugLevel, Level: DebugLevel,
@@ -113,7 +118,7 @@ func (config *logConfig) newLogger() *zap.Logger {
var options []zap.Option var options []zap.Option
if config.EnableLineNumber { if config.EnableLineNumber {
options = append(options, zap.AddCaller(), zap.AddCallerSkip(1)) options = append(options, zap.AddCaller(), zap.AddCallerSkip(config.AddCallerSkip))
} }
if config.EnableErrorStacktrace { if config.EnableErrorStacktrace {
@@ -265,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...)
}

View File

@@ -21,10 +21,10 @@ func TestLogWithConfig(t *testing.T) {
config.Init() config.Init()
With("traceid", "21221212122") Fields("traceid", float64(21221212122))
Debugf("this is %s message", "debug") Debugf("this is %s message", "debug")
config.Init() config.Init()
With(zap.String("traceid", "12123123123")) 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")

View File

@@ -1,9 +1,5 @@
package log package log
func With(args ...interface{}) {
logger = logger.With(args...)
}
func Debug(args ...interface{}) { func Debug(args ...interface{}) {
logger.Debug(args...) logger.Debug(args...)
} }

View File

@@ -1 +1 @@
v0.0.7 v0.0.10