Closed rhoninzhang closed 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"}
@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.
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.
default log formt is like this:
then, how to add cutom fields into the formatter? for example, add trace_id
It seems that the zapcore.Encoder function cannot be overridden