-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
numeric verbosity and configurable logger behavior
This is needed to make zapr usable for Kubernetes. Currently Kubernetes uses a forked zapr with "v" for the numeric verbosity and "err" for Logger.Error. Handling of invalid calls remains compatible with zapr v1.0.0 (= panic log messages are emitted, strongly-type Zap fields are not allowed), but new options allow changing those defaults. Those panic messages now log the source code of the caller, not the zapr.go file where the panic message is logged. The unit tests were derived from https://github.com/kubernetes/kubernetes/blob/cff40a7bcc170c473c223081fd0103fd042dc44b/staging/src/k8s.io/component-base/logs/json/json_test.go and both modified (better error messages, t.Run for each test case) and extended to enhance coverage. Several new edge cases are now covered (for example, error logging at non-zero verbosity levels). New tests for WithName, WithCallDepth and WithValues were added.
- Loading branch information
Showing
7 changed files
with
672 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
Copyright 2021 The logr Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
"github.com/go-logr/zapr" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
var errSome = errors.New("some error") | ||
|
||
func encodeTime(_ time.Time, enc zapcore.PrimitiveArrayEncoder) { | ||
// Suppress actual time to keep output constant. | ||
enc.AppendString("TIMESTAMP") | ||
} | ||
|
||
func newZapLogger() *zap.Logger { | ||
// zap gets configured to not panic on invalid log calls | ||
// and to produce simple, deterministic output on stdout. | ||
zc := zap.NewProductionConfig() | ||
zc.OutputPaths = []string{"stdout"} | ||
zc.ErrorOutputPaths = zc.OutputPaths | ||
zc.DisableStacktrace = true | ||
zc.DisableCaller = true | ||
zc.EncoderConfig.EncodeTime = encodeTime | ||
z, _ := zc.Build() | ||
return z | ||
} | ||
|
||
func ExampleNewLogger() { | ||
log := zapr.NewLogger(newZapLogger()) | ||
log.Info("info message with default options") | ||
log.Error(errSome, "error message with default options") | ||
log.Info("support for zap fields as key/value replacement is disabled", zap.Int("answer", 42)) | ||
log.Info("invalid key", 42, "answer") | ||
log.Info("missing value", "answer") | ||
// Output: | ||
// {"level":"info","ts":"TIMESTAMP","msg":"info message with default options"} | ||
// {"level":"error","ts":"TIMESTAMP","msg":"error message with default options","error":"some error"} | ||
// {"level":"dpanic","ts":"TIMESTAMP","msg":"strongly-typed Zap Field passed to logr","zap field":{"Key":"answer","Type":11,"Integer":42,"String":"","Interface":null}} | ||
// {"level":"info","ts":"TIMESTAMP","msg":"support for zap fields as key/value replacement is disabled"} | ||
// {"level":"dpanic","ts":"TIMESTAMP","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":42} | ||
// {"level":"info","ts":"TIMESTAMP","msg":"invalid key"} | ||
// {"level":"dpanic","ts":"TIMESTAMP","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"answer"} | ||
// {"level":"info","ts":"TIMESTAMP","msg":"missing value"} | ||
} | ||
|
||
func ExampleLogInfoLevel() { | ||
log := zapr.NewLoggerWithOptions(newZapLogger(), zapr.LogInfoLevel("v")) | ||
log.Info("info message with numeric verbosity level") | ||
log.Error(errSome, "error messages have no numeric verbosity level") | ||
// Output: | ||
// {"level":"info","ts":"TIMESTAMP","msg":"info message with numeric verbosity level","v":0} | ||
// {"level":"error","ts":"TIMESTAMP","msg":"error messages have no numeric verbosity level","error":"some error"} | ||
} | ||
|
||
func ExampleErrorKey() { | ||
log := zapr.NewLoggerWithOptions(newZapLogger(), zapr.ErrorKey("err")) | ||
log.Error(errSome, "error message with non-default error key") | ||
// Output: | ||
// {"level":"error","ts":"TIMESTAMP","msg":"error message with non-default error key","err":"some error"} | ||
} | ||
|
||
func ExampleAllowZapFields() { | ||
log := zapr.NewLoggerWithOptions(newZapLogger(), zapr.AllowZapFields(true)) | ||
log.Info("log zap field", zap.Int("answer", 42)) | ||
// Output: | ||
// {"level":"info","ts":"TIMESTAMP","msg":"log zap field","answer":42} | ||
} | ||
|
||
func ExampleDPanicOnBugs() { | ||
log := zapr.NewLoggerWithOptions(newZapLogger(), zapr.DPanicOnBugs(false)) | ||
log.Info("warnings suppressed", zap.Int("answer", 42)) | ||
// Output: | ||
// {"level":"info","ts":"TIMESTAMP","msg":"warnings suppressed"} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.