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

prevent string formatting in Entry.Logf when log level is not enabled #903

Merged
merged 1 commit into from Feb 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions entry.go
Expand Up @@ -298,7 +298,9 @@ func (entry *Entry) Panic(args ...interface{}) {
// Entry Printf family functions

func (entry *Entry) Logf(level Level, format string, args ...interface{}) {
entry.Log(level, fmt.Sprintf(format, args...))
if entry.Logger.IsLevelEnabled(level) {
entry.Log(level, fmt.Sprintf(format, args...))
}
}

func (entry *Entry) Tracef(format string, args ...interface{}) {
Expand Down Expand Up @@ -390,4 +392,4 @@ func (entry *Entry) Panicln(args ...interface{}) {
func (entry *Entry) sprintlnn(args ...interface{}) string {
msg := fmt.Sprintln(args...)
return msg[:len(msg)-1]
}
}
14 changes: 14 additions & 0 deletions entry_test.go
Expand Up @@ -139,3 +139,17 @@ func TestEntryWithIncorrectField(t *testing.T) {
assert.Equal(eWithFunc.err, `can not add field "func"`)
assert.Equal(eWithFuncPtr.err, `can not add field "funcPtr"`)
}

func TestEntryLogfLevel(t *testing.T) {
logger := New()
buffer := &bytes.Buffer{}
logger.Out = buffer
logger.SetLevel(InfoLevel)
entry := NewEntry(logger)

entry.Logf(DebugLevel, "%s", "debug")
assert.NotContains(t, buffer.String(), "debug", )

entry.Logf(WarnLevel, "%s", "warn")
assert.Contains(t, buffer.String(), "warn", )
}