Skip to content

Commit

Permalink
Add LogToStderr, a programatic way to log exclusively to stderr or not
Browse files Browse the repository at this point in the history
  • Loading branch information
liztio committed Oct 11, 2019
1 parent ab80cd2 commit 6f99060
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 @@ -803,6 +803,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) {
}
}

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 6f99060

Please sign in to comment.