Skip to content

Commit 3ca30a5

Browse files
authored
Merge pull request #78 from dims/dedup-log-attempt-2
Fix the log duplication issue for --log-file
2 parents 13d88a6 + bf4884f commit 3ca30a5

File tree

2 files changed

+56
-16
lines changed

2 files changed

+56
-16
lines changed

klog.go

+30-16
Original file line numberDiff line numberDiff line change
@@ -785,24 +785,38 @@ func (l *loggingT) output(s severity, buf *buffer, file string, line int, alsoTo
785785
if alsoToStderr || l.alsoToStderr || s >= l.stderrThreshold.get() {
786786
os.Stderr.Write(data)
787787
}
788-
if l.file[s] == nil {
789-
if err := l.createFiles(s); err != nil {
790-
os.Stderr.Write(data) // Make sure the message appears somewhere.
791-
l.exit(err)
788+
789+
if logging.logFile != "" {
790+
// Since we are using a single log file, all of the items in l.file array
791+
// will point to the same file, so just use one of them to write data.
792+
if l.file[infoLog] == nil {
793+
if err := l.createFiles(infoLog); err != nil {
794+
os.Stderr.Write(data) // Make sure the message appears somewhere.
795+
l.exit(err)
796+
}
792797
}
793-
}
794-
switch s {
795-
case fatalLog:
796-
l.file[fatalLog].Write(data)
797-
fallthrough
798-
case errorLog:
799-
l.file[errorLog].Write(data)
800-
fallthrough
801-
case warningLog:
802-
l.file[warningLog].Write(data)
803-
fallthrough
804-
case infoLog:
805798
l.file[infoLog].Write(data)
799+
} else {
800+
if l.file[s] == nil {
801+
if err := l.createFiles(s); err != nil {
802+
os.Stderr.Write(data) // Make sure the message appears somewhere.
803+
l.exit(err)
804+
}
805+
}
806+
807+
switch s {
808+
case fatalLog:
809+
l.file[fatalLog].Write(data)
810+
fallthrough
811+
case errorLog:
812+
l.file[errorLog].Write(data)
813+
fallthrough
814+
case warningLog:
815+
l.file[warningLog].Write(data)
816+
fallthrough
817+
case infoLog:
818+
l.file[infoLog].Write(data)
819+
}
806820
}
807821
}
808822
if s == fatalLog {

klog_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io/ioutil"
2424
stdLog "log"
25+
"os"
2526
"path/filepath"
2627
"runtime"
2728
"strconv"
@@ -551,6 +552,31 @@ func BenchmarkHeaderWithDir(b *testing.B) {
551552
}
552553
}
553554

555+
func BenchmarkLogs(b *testing.B) {
556+
setFlags()
557+
defer logging.swap(logging.newBuffers())
558+
559+
testFile, err := ioutil.TempFile("", "test.log")
560+
if err != nil {
561+
b.Error("unable to create temporary file")
562+
}
563+
defer os.Remove(testFile.Name())
564+
565+
logging.verbosity.Set("0")
566+
logging.toStderr = false
567+
logging.alsoToStderr = false
568+
logging.stderrThreshold = fatalLog
569+
logging.logFile = testFile.Name()
570+
logging.swap([numSeverity]flushSyncWriter{nil, nil, nil, nil})
571+
572+
for i := 0; i < b.N; i++ {
573+
Error("error")
574+
Warning("warning")
575+
Info("info")
576+
}
577+
logging.flushAll()
578+
}
579+
554580
// Test the logic on checking log size limitation.
555581
func TestFileSizeCheck(t *testing.T) {
556582
setFlags()

0 commit comments

Comments
 (0)