json: enable WithName logging

The name added to a logger via WithName only gets printed when a key is
chosen. "logger" is used as in the zap examples.

This becomes relevant once we support contextual logging. When logging through
klog the name is always empty.
This commit is contained in:
Patrick Ohly 2021-10-20 13:42:45 +02:00
parent 3fe5b3e09d
commit ab318cf89f
2 changed files with 4 additions and 4 deletions

View File

@ -43,6 +43,7 @@ func NewJSONLogger(infoStream, errorStream zapcore.WriteSyncer, encoderConfig *z
encoderConfig = &zapcore.EncoderConfig{
MessageKey: "msg",
CallerKey: "caller",
NameKey: "logger",
TimeKey: "ts",
EncodeTime: epochMillisTimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,

View File

@ -61,10 +61,9 @@ func TestZapLoggerInfo(t *testing.T) {
keysValues: []interface{}{"duration", time.Duration(5 * time.Second)},
},
{
msg: "test for WithName",
names: []string{"hello", "world"},
// TODO: log names
format: "{\"ts\":%f,\"caller\":\"json/json_test.go:%d\",\"msg\":\"test for WithName\",\"v\":0}\n",
msg: "test for WithName",
names: []string{"hello", "world"},
format: "{\"ts\":%f,\"logger\":\"hello.world\",\"caller\":\"json/json_test.go:%d\",\"msg\":\"test for WithName\",\"v\":0}\n",
},
}