Use a discarding writer for klog output

Configure klog to write messages to a discarding writer instead of
writing them to /dev/null.

The change does not affect klog behavior for the stderrthreshold flag.

It fixes crashes when runing on a platform without /dev/null device (for
example on Windows). The klog will exit the program if /dev/null file
cannot be open.

Fixes derailed/k9#703
mine
Petr Svoboda 2020-05-07 23:17:30 +02:00
parent b293395249
commit 072217b317
1 changed files with 2 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package cmd
import ( import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"runtime/debug" "runtime/debug"
"github.com/derailed/k9s/internal/client" "github.com/derailed/k9s/internal/client"
@ -46,9 +47,7 @@ func init() {
// Klogs (of course) want to print stuff to the screen ;( // Klogs (of course) want to print stuff to the screen ;(
klog.InitFlags(nil) klog.InitFlags(nil)
if err := flag.Set("log_file", "/dev/null"); err != nil { klog.SetOutput(ioutil.Discard)
log.Error().Err(err)
}
if err := flag.Set("stderrthreshold", "fatal"); err != nil { if err := flag.Set("stderrthreshold", "fatal"); err != nil {
log.Error().Err(err) log.Error().Err(err)
} }