Skip to content

Commit

Permalink
Merge pull request #106146 from pohly/json-output-default
Browse files Browse the repository at this point in the history
component-base: use stderr as default output stream for JSON
  • Loading branch information
k8s-ci-robot committed Nov 4, 2021
2 parents 2f21cff + b4988a4 commit f1b000d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 2 additions & 0 deletions staging/src/k8s.io/component-base/logs/example/cmd/logger.go
Expand Up @@ -54,6 +54,8 @@ func NewLoggerCommand() *cobra.Command {
}

func runLogger() {
fmt.Println("This is normal output via stdout.")
fmt.Fprintln(os.Stderr, "This is other output via stderr.")
klog.Infof("Log using Infof, key: %s", "value")
klog.InfoS("Log using InfoS", "key", "value")
err := errors.New("fail")
Expand Down
5 changes: 4 additions & 1 deletion staging/src/k8s.io/component-base/logs/json/json.go
Expand Up @@ -93,6 +93,7 @@ var _ registry.LogFormatFactory = Factory{}

func (f Factory) Create(options config.FormatOptions) (logr.Logger, func()) {
if options.JSON.SplitStream {
// stdout for info messages, stderr for errors.
infoStream := zapcore.Lock(os.Stdout)
size := options.JSON.InfoBufferSize.Value()
if size > 0 {
Expand All @@ -107,7 +108,9 @@ func (f Factory) Create(options config.FormatOptions) (logr.Logger, func()) {
}
return NewJSONLogger(infoStream, zapcore.Lock(os.Stderr))
}
out := zapcore.Lock(os.Stdout)
// The default is to write to stderr (same as in klog's text output,
// doesn't get mixed with normal program output).
out := zapcore.Lock(os.Stderr)
return NewJSONLogger(out, out)
}

Expand Down

0 comments on commit f1b000d

Please sign in to comment.