Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
08e6fdbd53 | |||
7eee167d91 | |||
c03451395f | |||
48e8f68955 | |||
7b9983504f | |||
6ac40ca1a2 |
@@ -68,12 +68,13 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
|
||||
if enc.TimeKey != "" && enc.EncodeTime != nil {
|
||||
enc.EncodeTime(ent.Time, arr)
|
||||
}
|
||||
|
||||
if enc.LevelKey != "" && enc.EncodeLevel != nil {
|
||||
enc.EncodeLevel(ent.Level, arr)
|
||||
}
|
||||
|
||||
if ent.LoggerName != "" && enc.NameKey != "" {
|
||||
nameEncoder := enc.EncodeName
|
||||
|
||||
if nameEncoder == nil {
|
||||
// Fall back to FullNameEncoder for backward compatibility.
|
||||
nameEncoder = zapcore.FullNameEncoder
|
||||
@@ -81,17 +82,23 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
|
||||
|
||||
nameEncoder(ent.LoggerName, arr)
|
||||
}
|
||||
|
||||
if ent.Caller.Defined && enc.CallerKey != "" && enc.EncodeCaller != nil {
|
||||
enc.EncodeCaller(ent.Caller, arr)
|
||||
}
|
||||
|
||||
for i := range arr.elems {
|
||||
// if i > 0 {
|
||||
// line.AppendByte('\t')
|
||||
// }
|
||||
_, _ = fmt.Fprint(line, arr.elems[i])
|
||||
}
|
||||
|
||||
putSliceEncoder(arr)
|
||||
|
||||
// Add any structured context.
|
||||
enc.writeContext(line, fields)
|
||||
|
||||
// Add the message itself.
|
||||
if enc.MessageKey != "" {
|
||||
// c.addTabIfNecessary(line)
|
||||
@@ -99,9 +106,6 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
|
||||
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
|
||||
// single-line output.
|
||||
if ent.Stack != "" && enc.StacktraceKey != "" {
|
||||
@@ -114,6 +118,7 @@ func (enc textEncoder) EncodeEntry(ent zapcore.Entry, fields []zapcore.Field) (*
|
||||
} else {
|
||||
line.AppendString(zapcore.DefaultLineEnding)
|
||||
}
|
||||
|
||||
return line, nil
|
||||
}
|
||||
|
||||
@@ -128,9 +133,10 @@ func (enc textEncoder) writeContext(line *buffer.Buffer, extra []zapcore.Field)
|
||||
}
|
||||
|
||||
// c.addTabIfNecessary(line)
|
||||
line.AppendByte('{')
|
||||
// line.AppendByte('{')
|
||||
line.AppendByte(' ')
|
||||
_, _ = line.Write(context.buf.Bytes())
|
||||
line.AppendByte('}')
|
||||
// line.AppendByte('}')
|
||||
}
|
||||
|
||||
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) {
|
||||
enc.addKey(key)
|
||||
enc.appendFloat(val, 64)
|
||||
enc.buf.AppendByte('}')
|
||||
}
|
||||
func (enc *textEncoder) AddInt64(key string, val int64) {
|
||||
enc.addKey(key)
|
||||
enc.addElementSeparator()
|
||||
enc.buf.AppendInt(val)
|
||||
enc.buf.AppendByte('}')
|
||||
}
|
||||
func (enc *textEncoder) AddReflected(key string, obj interface{}) error {
|
||||
enc.resetReflectBuf()
|
||||
@@ -241,9 +249,10 @@ func (enc *textEncoder) OpenNamespace(key string) {
|
||||
func (enc *textEncoder) AddString(key, val string) {
|
||||
enc.addKey(key)
|
||||
enc.addElementSeparator()
|
||||
enc.buf.AppendByte('"')
|
||||
// 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)
|
||||
@@ -261,6 +270,7 @@ func (enc *textEncoder) AddUint64(key string, val uint64) {
|
||||
enc.addKey(key)
|
||||
enc.addElementSeparator()
|
||||
enc.buf.AppendUint(val)
|
||||
enc.buf.AppendByte('}')
|
||||
}
|
||||
|
||||
//noinspection GoRedundantConversion
|
||||
@@ -293,15 +303,39 @@ 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(':')
|
||||
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 {
|
||||
@@ -311,7 +345,7 @@ func (enc *textEncoder) addElementSeparator() {
|
||||
case '{', '[', ':', ',', ' ':
|
||||
return
|
||||
default:
|
||||
enc.buf.AppendByte(',')
|
||||
// enc.buf.AppendByte(',')
|
||||
if enc.spaced {
|
||||
enc.buf.AppendByte(' ')
|
||||
}
|
||||
|
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...)
|
||||
}
|
||||
|
10
log_test.go
10
log_test.go
@@ -15,18 +15,18 @@ func TestLog(t *testing.T) {
|
||||
|
||||
func TestLogWithConfig(t *testing.T) {
|
||||
config := NewLogConfig()
|
||||
_ = config.Level.Set("d")
|
||||
_ = config.Level.Set("debug")
|
||||
config.Name = "main"
|
||||
//config.Fields = []zap.Field{zap.String("traceid", "12123123123")}
|
||||
// config.Fields = []zap.Field{zap.String("traceid", "12123123123")}
|
||||
|
||||
config.Init()
|
||||
|
||||
With("foo", "baz")
|
||||
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")
|
||||
// Errorf("this is %s message", "error")
|
||||
// Panicf("this is %s message", "panic")
|
||||
}
|
||||
|
||||
|
@@ -1,9 +1,5 @@
|
||||
package log
|
||||
|
||||
func With(args ...interface{}) {
|
||||
logger = logger.With(args...)
|
||||
}
|
||||
|
||||
func Debug(args ...interface{}) {
|
||||
logger.Debug(args...)
|
||||
}
|
||||
|
Reference in New Issue
Block a user