Merge pull request #106146 from pohly/json-output-default

component-base: use stderr as default output stream for JSON
This commit is contained in:
Kubernetes Prow Robot 2021-11-04 04:22:04 -07:00 committed by GitHub
commit f1b000db7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 1 deletions

View File

@ -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")

View File

@ -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 {
@ -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)
}