Skip to content

Commit

Permalink
Merge pull request #78 from dims/dedup-log-attempt-2
Browse files Browse the repository at this point in the history
Fix the log duplication issue for --log-file
  • Loading branch information
k8s-ci-robot committed Aug 7, 2019
2 parents 13d88a6 + bf4884f commit 3ca30a5
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 @@ -785,24 +785,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 @@ -551,6 +552,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 3ca30a5

Please sign in to comment.