Skip to content

Commit

Permalink
Merge pull request #100 from liztio/setstderr
Browse files Browse the repository at this point in the history
Add LogToStderr, a programatic way to log exclusively to stderr or not
  • Loading branch information
k8s-ci-robot committed May 12, 2020
2 parents 32133e3 + 6f99060 commit a5796cb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions klog.go
Expand Up @@ -853,6 +853,14 @@ func SetOutputBySeverity(name string, w io.Writer) {
logging.file[sev] = rb
}

// LogToStderr sets whether to log exclusively to stderr, bypassing outputs
func LogToStderr(stderr bool) {
logging.mu.Lock()
defer logging.mu.Unlock()

logging.toStderr = stderr
}

// output writes the data to the log files and releases the buffer.
func (l *loggingT) output(s severity, log logr.InfoLogger, buf *buffer, file string, line int, alsoToStderr bool) {
l.mu.Lock()
Expand Down
15 changes: 15 additions & 0 deletions klog_test.go
Expand Up @@ -351,6 +351,21 @@ func TestSetOutputDataRace(t *testing.T) {
wg.Wait()
}

func TestLogToOutput(t *testing.T) {
logging.toStderr = true
defer logging.swap(logging.newBuffers())
buf := new(bytes.Buffer)
SetOutput(buf)
LogToStderr(false)

Info("Does logging to an output work?")

str := buf.String()
if !strings.Contains(str, "Does logging to an output work?") {
t.Fatalf("Expected %q to contain \"Does logging to an output work?\"", str)
}
}

// vGlobs are patterns that match/don't match this file at V=2.
var vGlobs = map[string]bool{
// Easy to test the numeric match here.
Expand Down

0 comments on commit a5796cb

Please sign in to comment.