Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trace message output #79

Merged
merged 8 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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: res{infoLogRE, warningLogRE, errorLogRE, fatalLogRE},
scaat marked this conversation as resolved.
Show resolved Hide resolved
},
"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: res{infoLogRE, warningLogRE, errorLogRE, fatalLogRE},
scaat marked this conversation as resolved.
Show resolved Hide resolved
},
"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: res{infoLogRE, warningLogRE, errorLogRE, fatalLogRE},
scaat marked this conversation as resolved.
Show resolved Hide resolved
},
"with log file and log dir": {
// Everything, including the trace on fatal, goes to the single log file.
Expand Down
4 changes: 3 additions & 1 deletion klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,11 +830,13 @@ func (l *loggingT) output(s severity, buf *buffer, file string, line int, alsoTo
// 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.
trace := stacks(true)
if !l.toStderr {
scaat marked this conversation as resolved.
Show resolved Hide resolved
os.Stderr.Write(stacks(false))
} else {
os.Stderr.Write(trace)
scaat marked this conversation as resolved.
Show resolved Hide resolved
}
// 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