Skip to content

Commit

Permalink
Merge pull request #79 from scaat/master
Browse files Browse the repository at this point in the history
Add trace message output
  • Loading branch information
k8s-ci-robot committed Jan 8, 2020
2 parents 8422fac + fdd1c48 commit c4f7487
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
15 changes: 6 additions & 9 deletions integration_tests/klog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ func TestDestinationsWithDifferentFlags(t *testing.T) {
notExpectedInDir map[int]res
}{
"default flags": {
// Everything, EXCEPT the trace on fatal, goes to stderr
// Everything, including the trace on fatal, goes to stderr

expectedOnStderr: res{infoLogRE, warningLogRE, errorLogRE, fatalLogRE},
notExpectedOnStderr: res{stackTraceRE},
expectedOnStderr: allLogREs,
},
"everything disabled": {
// Nothing, including the trace on fatal, is showing anywhere
Expand All @@ -104,12 +103,11 @@ func TestDestinationsWithDifferentFlags(t *testing.T) {
notExpectedOnStderr: res{infoLogRE},
},
"with logtostderr only": {
// Everything, EXCEPT the trace on fatal, goes to stderr
// Everything, including the trace on fatal, goes to stderr

flags: []string{"-logtostderr=true", "-alsologtostderr=false", "-stderrthreshold=1000"},

expectedOnStderr: res{infoLogRE, warningLogRE, errorLogRE, fatalLogRE},
notExpectedOnStderr: res{stackTraceRE},
expectedOnStderr: allLogREs,
},
"with log file only": {
// Everything, including the trace on fatal, goes to the single log file
Expand All @@ -135,14 +133,13 @@ func TestDestinationsWithDifferentFlags(t *testing.T) {
notExpectedInDir: defaultNotExpectedInDirREs,
},
"with log dir and logtostderr": {
// Everything, EXCEPT the trace on fatal, goes to stderr. The -log_dir is
// Everything, including the trace on fatal, goes to stderr. The -log_dir is
// ignored, nothing goes to the log files in the log dir.

logdir: true,
flags: []string{"-logtostderr=true", "-alsologtostderr=false", "-stderrthreshold=1000"},

expectedOnStderr: res{infoLogRE, warningLogRE, errorLogRE, fatalLogRE},
notExpectedOnStderr: res{stackTraceRE},
expectedOnStderr: allLogREs,
},
"with log file and log dir": {
// Everything, including the trace on fatal, goes to the single log file.
Expand Down
10 changes: 4 additions & 6 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -868,14 +868,12 @@ func (l *loggingT) output(s severity, log logr.InfoLogger, buf *buffer, file str
os.Exit(1)
}
// Dump all goroutine stacks before exiting.
// First, make sure we see the trace for the current goroutine on standard error.
// If -logtostderr has been specified, the loop below will do that anyway
// as the first stack in the full dump.
if !l.toStderr {
os.Stderr.Write(stacks(false))
trace := stacks(true)
// Write the stack trace for all goroutines to the stderr.
if l.toStderr || l.alsoToStderr || s >= l.stderrThreshold.get() || alsoToStderr {
os.Stderr.Write(trace)
}
// Write the stack trace for all goroutines to the files.
trace := stacks(true)
logExitFunc = func(error) {} // If we get a write error, we'll still exit below.
for log := fatalLog; log >= infoLog; log-- {
if f := l.file[log]; f != nil { // Can be nil if -logtostderr is set.
Expand Down
4 changes: 2 additions & 2 deletions klog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ func TestOpenAppendOnStart(t *testing.T) {
// Logging agagin should open the file again with O_APPEND instead of O_TRUNC
Info(y)
// ensure we wrote what we expected
logging.flushAll()
logging.lockAndFlushAll()
b, err = ioutil.ReadFile(logging.logFile)
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand All @@ -491,7 +491,7 @@ func TestOpenAppendOnStart(t *testing.T) {
t.Fatalf("got %s, missing expected Info log: %s", string(b), y)
}
// The initial log message should be preserved across create calls.
logging.flushAll()
logging.lockAndFlushAll()
b, err = ioutil.ReadFile(logging.logFile)
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down

0 comments on commit c4f7487

Please sign in to comment.