Skip to content

Commit

Permalink
Merge pull request kubernetes#101158 from serathius/automated-cherry-…
Browse files Browse the repository at this point in the history
…pick-of-#100013-upstream-release-1.20

[1.20] Add duration encoder to structured logger
  • Loading branch information
k8s-ci-robot committed May 6, 2021
2 parents fe7d806 + 0d404c0 commit f757b3e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions staging/src/k8s.io/component-base/logs/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ func (l *zapLogger) WithName(name string) logr.Logger {
var encoderConfig = zapcore.EncoderConfig{
MessageKey: "msg",

TimeKey: "ts",
EncodeTime: zapcore.EpochMillisTimeEncoder,
TimeKey: "ts",
EncodeTime: zapcore.EpochMillisTimeEncoder,
EncodeDuration: zapcore.StringDurationEncoder,
}

// NewJSONLogger creates a new json logr.Logger using the given Zap Logger to log.
Expand Down
5 changes: 5 additions & 0 deletions staging/src/k8s.io/component-base/logs/json/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func TestZapLoggerInfo(t *testing.T) {
format: "{\"ts\":%f,\"msg\":\"non-string key argument passed to logging, ignoring all later arguments\",\"v\":0}\n{\"ts\":0.000123,\"msg\":\"test for non-string key argument\",\"v\":0,\"ns\":\"default\",\"podnum\":2}\n",
keysValues: []interface{}{"ns", "default", "podnum", 2, 200, "replica", "Running", 10},
},
{
msg: "test for duration value argument",
format: "{\"ts\":%f,\"msg\":\"test for duration value argument\",\"v\":0,\"duration\":\"5s\"}\n",
keysValues: []interface{}{"duration", time.Duration(5 * time.Second)},
},
}

for _, data := range testDataInfo {
Expand Down

0 comments on commit f757b3e

Please sign in to comment.