trpc-group / trpc-go

A pluggable, high-performance RPC framework written in golang
Other
742 stars 85 forks source link

How to customize the log output format in "console" mode? #145

Closed rhoninzhang closed 6 months ago

rhoninzhang commented 6 months ago

default log formt is like this:

2023-12-13 11:02:02.381 DEBUG   demo/test.go:47 start test...

then, how to add cutom fields into the formatter? for example, add trace_id

2023-12-13 11:02:02.381 [a5d7e7d8-dec7-437c-8a48-6f31891fe028]  DEBUG   demo/test.go:47 start test...

It seems that the zapcore.Encoder function cannot be overridden

func newEncoder(c *OutputConfig) zapcore.Encoder { 
    encoderCfg := zapcore.EncoderConfig{
        TimeKey:        GetLogEncoderKey("T", c.FormatConfig.TimeKey),
        LevelKey:       GetLogEncoderKey("L", c.FormatConfig.LevelKey),
        NameKey:        GetLogEncoderKey("N", c.FormatConfig.NameKey),
        CallerKey:      GetLogEncoderKey("C", c.FormatConfig.CallerKey),
        FunctionKey:    GetLogEncoderKey(zapcore.OmitKey, c.FormatConfig.FunctionKey),
        MessageKey:     GetLogEncoderKey("M", c.FormatConfig.MessageKey),
        StacktraceKey:  GetLogEncoderKey("S", c.FormatConfig.StacktraceKey),
        LineEnding:     zapcore.DefaultLineEnding,
        EncodeLevel:    zapcore.CapitalLevelEncoder,
        EncodeTime:     NewTimeEncoder(c.FormatConfig.TimeFmt),
        EncodeDuration: zapcore.StringDurationEncoder,
        EncodeCaller:   zapcore.ShortCallerEncoder,
    }
    if c.EnableColor {
        encoderCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
    }
    switch c.Formatter {
    case "console":
        return zapcore.NewConsoleEncoder(encoderCfg)
    case "json":
        return zapcore.NewJSONEncoder(encoderCfg)
    default:
        return zapcore.NewConsoleEncoder(encoderCfg)
    }
}
WineChord commented 6 months ago

Try log.With.

rhoninzhang commented 6 months ago

log.With add a field in json mode, can't format, any other way? code:

log.With(log.Field{Key: "test_field", Value: "123456789"}).Debug("try log with")

output:

2023-12-13 16:45:54.457 DEBUG   demo/test.go:48 try log with    {"test_field": "123456789"}
WineChord commented 6 months ago

@rhoninzhang, please check out the changes made in https://github.com/trpc-group/trpc-go/pull/146.

I have provided an example in zaplogger_test.go. The idea is to implement a custom encoder. However, I found it challenging to get it right, and the implementation is not straightforward. For instance, the fields argument in EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) is always an empty array, regardless of whether the logger is decorated with additional fields. This can be quite confusing. I suggest you switch to this branch and explore it yourself. Feel free to provide more feedback.

WineChord commented 6 months ago

image

I noticed that the InlineMarshalerType might be what you're looking for when dealing with unstructured JSON values (where only the value itself is shown, rather than {key: value}). However, I found that this utility seems to be inaccessible from the trpc-go logging package. It provides its own log.Field type and uses zapcore.Any to extract possible value types, but unfortunately, InlineMarshalerType is not included.