diff --git a/klog.go b/klog.go index 17d29752d..e8488511b 100644 --- a/klog.go +++ b/klog.go @@ -781,24 +781,38 @@ func (l *loggingT) output(s severity, buf *buffer, file string, line int, alsoTo if alsoToStderr || l.alsoToStderr || s >= l.stderrThreshold.get() { os.Stderr.Write(data) } - if l.file[s] == nil { - if err := l.createFiles(s); err != nil { - os.Stderr.Write(data) // Make sure the message appears somewhere. - l.exit(err) + + if logging.logFile != "" { + // Since we are using a single log file, all of the items in l.file array + // will point to the same file, so just use one of them to write data. + if l.file[infoLog] == nil { + if err := l.createFiles(infoLog); err != nil { + os.Stderr.Write(data) // Make sure the message appears somewhere. + l.exit(err) + } } - } - switch s { - case fatalLog: - l.file[fatalLog].Write(data) - fallthrough - case errorLog: - l.file[errorLog].Write(data) - fallthrough - case warningLog: - l.file[warningLog].Write(data) - fallthrough - case infoLog: l.file[infoLog].Write(data) + } else { + if l.file[s] == nil { + if err := l.createFiles(s); err != nil { + os.Stderr.Write(data) // Make sure the message appears somewhere. + l.exit(err) + } + } + + switch s { + case fatalLog: + l.file[fatalLog].Write(data) + fallthrough + case errorLog: + l.file[errorLog].Write(data) + fallthrough + case warningLog: + l.file[warningLog].Write(data) + fallthrough + case infoLog: + l.file[infoLog].Write(data) + } } } if s == fatalLog { diff --git a/klog_test.go b/klog_test.go index d8455aedb..17bd268b9 100644 --- a/klog_test.go +++ b/klog_test.go @@ -22,6 +22,7 @@ import ( "fmt" "io/ioutil" stdLog "log" + "os" "path/filepath" "runtime" "strconv" @@ -521,6 +522,31 @@ func BenchmarkHeaderWithDir(b *testing.B) { } } +func BenchmarkLogs(b *testing.B) { + setFlags() + defer logging.swap(logging.newBuffers()) + + testFile, err := ioutil.TempFile("", "test.log") + if err != nil { + b.Error("unable to create temporary file") + } + defer os.Remove(testFile.Name()) + + logging.verbosity.Set("0") + logging.toStderr = false + logging.alsoToStderr = false + logging.stderrThreshold = fatalLog + logging.logFile = testFile.Name() + logging.swap([numSeverity]flushSyncWriter{nil, nil, nil, nil}) + + for i := 0; i < b.N; i++ { + Error("error") + Warning("warning") + Info("info") + } + logging.flushAll() +} + // Test the logic on checking log size limitation. func TestFileSizeCheck(t *testing.T) { setFlags()