Skip to content

Commit

Permalink
Fix the log duplication issue for --log-file
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dims committed Aug 5, 2019
1 parent ab4d90c commit bf4884f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
46 changes: 30 additions & 16 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions klog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io/ioutil"
stdLog "log"
"os"
"path/filepath"
"runtime"
"strconv"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit bf4884f

Please sign in to comment.