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

funcr: Add LogInfoLevel Option to skip logging level in the info log #240

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 14 additions & 1 deletion funcr/funcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ type Options struct {
// details, see docs for Go's time.Layout.
TimestampFormat string

// LogInfoLevel tells func what key to use to log the integer level. By default,
thockin marked this conversation as resolved.
Show resolved Hide resolved
// a "level" key will be used. If set it to "", the integer level will not be logged.
thockin marked this conversation as resolved.
Show resolved Hide resolved
LogInfoLevel *string

// Verbosity tells funcr which V logs to produce. Higher values enable
// more logs. Info logs at or below this level will be written, while logs
// above this level will be discarded.
Expand Down Expand Up @@ -740,7 +744,16 @@ func (f Formatter) FormatInfo(level int, msg string, kvList []any) (prefix, args
if policy := f.opts.LogCaller; policy == All || policy == Info {
args = append(args, "caller", f.caller())
}
args = append(args, "level", level, "msg", msg)
if f.opts.LogInfoLevel == nil {
thockin marked this conversation as resolved.
Show resolved Hide resolved
args = append(args, "level", level, "msg", msg)
} else {
lvKey := *f.opts.LogInfoLevel
if lvKey != "" {
args = append(args, lvKey, level, "msg", msg)
} else {
args = append(args, "msg", msg)
}
}
return prefix, f.render(args, kvList)
}

Expand Down
32 changes: 32 additions & 0 deletions funcr/funcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1361,3 +1361,35 @@ func TestOptionsTimestampFormat(t *testing.T) {
t.Errorf("\nexpected %q\n got %q", expect, capt.log)
}
}

func TestOptionsLogInfoLevel(t *testing.T) {
testCases := []struct {
thockin marked this conversation as resolved.
Show resolved Hide resolved
name string
level string
expect string
}{
{
name: "custom key",
level: "info_level",
expect: `"info_level"=0 "msg"="msg"`,
},
{
name: "no level",
level: "",
expect: `"msg"="msg"`,
},
}

for _, tc := range testCases {
t.Run("Run: "+tc.name, func(t *testing.T) {
capt := &capture{}
sink := newSink(capt.Func, NewFormatter(Options{LogInfoLevel: &tc.level}))
dSink, _ := sink.(logr.CallDepthLogSink)
sink = dSink.WithCallDepth(1)
sink.Info(0, "msg")
if capt.log != tc.expect {
t.Errorf("\nexpected %q\n got %q", tc.expect, capt.log)
}
})
}
}
Loading