Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flag to include file's dir in logs #74

Merged
merged 1 commit into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be &logging.addDirHeader?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it should. cc @dims

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jhahn21 Could you please file a PR to fix it. good catch. thanks!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dims Sure, I will.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dims Here is the PR: #101

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