Skip to content

Commit

Permalink
Add flag to include file's dir in logs
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <vincepri@vmware.com>
  • Loading branch information
vincepri committed Jul 9, 2019
1 parent 05be6e5 commit b33ae69
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
16 changes: 13 additions & 3 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ func InitFlags(flagset *flag.FlagSet) {
logging.toStderr = true
logging.alsoToStderr = false
logging.skipHeaders = false
logging.addDirHeader = false
logging.skipLogHeaders = false
})

Expand All @@ -432,6 +433,7 @@ func InitFlags(flagset *flag.FlagSet) {
flagset.BoolVar(&logging.toStderr, "logtostderr", logging.toStderr, "log to standard error instead of files")
flagset.BoolVar(&logging.alsoToStderr, "alsologtostderr", logging.alsoToStderr, "log to standard error as well as files")
flagset.Var(&logging.verbosity, "v", "number for the log level verbosity")
flagset.BoolVar(&logging.skipHeaders, "add_dir_header", logging.addDirHeader, "If true, adds the file directory to the header")
flagset.BoolVar(&logging.skipHeaders, "skip_headers", logging.skipHeaders, "If true, avoid header prefixes in the log messages")
flagset.BoolVar(&logging.skipLogHeaders, "skip_log_headers", logging.skipLogHeaders, "If true, avoid headers when opening log files")
flagset.Var(&logging.stderrThreshold, "stderrthreshold", "logs at or above this threshold go to stderr")
Expand Down Expand Up @@ -500,6 +502,9 @@ type loggingT struct {

// If true, do not add the headers to log files
skipLogHeaders bool

// If true, add the file directory to the header
addDirHeader bool
}

// buffer holds a byte Buffer for reuse. The zero value is ready for use.
Expand Down Expand Up @@ -585,9 +590,14 @@ func (l *loggingT) header(s severity, depth int) (*buffer, string, int) {
file = "???"
line = 1
} else {
slash := strings.LastIndex(file, "/")
if slash >= 0 {
file = file[slash+1:]
if slash := strings.LastIndex(file, "/"); slash >= 0 {
path := file
file = path[slash+1:]
if l.addDirHeader {
if dirsep := strings.LastIndex(path[:slash], "/"); dirsep >= 0 {
file = path[dirsep+1:]
}
}
}
}
return l.formatHeader(s, file, line), file, line
Expand Down
33 changes: 33 additions & 0 deletions klog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func contains(s severity, str string, t *testing.T) bool {
// setFlags configures the logging flags how the test expects them.
func setFlags() {
logging.toStderr = false
logging.addDirHeader = false
}

// Test that Info works as advertised.
Expand Down Expand Up @@ -198,6 +199,30 @@ func TestHeader(t *testing.T) {
}
}

func TestHeaderWithDir(t *testing.T) {
setFlags()
logging.addDirHeader = true
defer logging.swap(logging.newBuffers())
defer func(previous func() time.Time) { timeNow = previous }(timeNow)
timeNow = func() time.Time {
return time.Date(2006, 1, 2, 15, 4, 5, .067890e9, time.Local)
}
pid = 1234
Info("test")
var line int
format := "I0102 15:04:05.067890 1234 klog/klog_test.go:%d] test\n"
n, err := fmt.Sscanf(contents(infoLog), format, &line)
if n != 1 || err != nil {
t.Errorf("log format error: %d elements, error %s:\n%s", n, err, contents(infoLog))
}
// Scanf treats multiple spaces as equivalent to a single space,
// so check for correct space-padding also.
want := fmt.Sprintf(format, line)
if contents(infoLog) != want {
t.Errorf("log format error: got:\n\t%q\nwant:\t%q", contents(infoLog), want)
}
}

// Test that an Error log goes to Warning and Info.
// Even in the Info log, the source character will be E, so the data should
// all be identical.
Expand Down Expand Up @@ -488,6 +513,14 @@ func BenchmarkHeader(b *testing.B) {
}
}

func BenchmarkHeaderWithDir(b *testing.B) {
logging.addDirHeader = true
for i := 0; i < b.N; i++ {
buf, _, _ := logging.header(infoLog, 0)
logging.putBuffer(buf)
}
}

// Test the logic on checking log size limitation.
func TestFileSizeCheck(t *testing.T) {
setFlags()
Expand Down

0 comments on commit b33ae69

Please sign in to comment.