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 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
14 changes: 13 additions & 1 deletion funcr/funcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ type Options struct {
// details, see docs for Go's time.Layout.
TimestampFormat string

// LogInfoLevel tells funcr what key to use to log the info level.
// If not specified, the info level will be logged as "level".
// If this is set to "", the info level will not be logged at all.
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 @@ -213,6 +218,10 @@ func newFormatter(opts Options, outfmt outputFormat) Formatter {
if opts.MaxLogDepth == 0 {
opts.MaxLogDepth = defaultMaxLogDepth
}
if opts.LogInfoLevel == nil {
opts.LogInfoLevel = new(string)
*opts.LogInfoLevel = "level"
}
f := Formatter{
outputFormat: outfmt,
prefix: "",
Expand Down Expand Up @@ -740,7 +749,10 @@ 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 key := *f.opts.LogInfoLevel; key != "" {
args = append(args, key, level)
}
args = append(args, "msg", msg)
return prefix, f.render(args, kvList)
}

Expand Down
37 changes: 37 additions & 0 deletions funcr/funcr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1361,3 +1361,40 @@ 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: ptrstr("info_level"),
expect: `"info_level"=0 "msg"="msg"`,
},
{
name: "no level",
level: ptrstr(""),
expect: `"msg"="msg"`,
},
{
name: "default",
level: nil,
expect: `"level"=0 "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