From bf4884f55dcdc259f3b5f509a48e8ece839fea25 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Tue, 30 Jul 2019 17:28:08 -0400 Subject: [PATCH] Fix the log duplication issue for --log-file when we use a `log-file`, all the items in l.file array point to the same file. we are currently using a switch statement with fallthrough which ends up logging the same line multiple times. So check if we are using a `log-file` and just write the data once. Change-Id: I3396558f487daf9db2c5e21e29a7a6bee618d64a --- klog.go | 46 ++++++++++++++++++++++++++++++---------------- klog_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 16 deletions(-) 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()