From be7773d48118387fdcec31a04753450f4fe0e959 Mon Sep 17 00:00:00 2001 From: Cheng-Zhen Yang Date: Sun, 27 Oct 2024 02:00:22 +1300 Subject: [PATCH] Simpler level transformer --- bridges/otellogr/example_test.go | 4 ++-- bridges/otellogr/logsink.go | 32 ++++++++++++++------------------ bridges/otellogr/logsink_test.go | 12 +++++------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/bridges/otellogr/example_test.go b/bridges/otellogr/example_test.go index 10cfda271c9..e8858a7521f 100644 --- a/bridges/otellogr/example_test.go +++ b/bridges/otellogr/example_test.go @@ -20,8 +20,8 @@ func Example() { "my/pkg/name", otellogr.WithLoggerProvider(provider), // Optionally, set the log level severity mapping. - otellogr.WithLevelSeverity(func(i int) log.Severity { - switch i { + otellogr.WithLevelSeverity(func(level int) log.Severity { + switch level { case 0: return log.SeverityInfo case 1: diff --git a/bridges/otellogr/logsink.go b/bridges/otellogr/logsink.go index bac072a2063..7e51e815a30 100644 --- a/bridges/otellogr/logsink.go +++ b/bridges/otellogr/logsink.go @@ -18,27 +18,17 @@ // added as attributes. If there are multiple [context.Context] the last one // is used. // -// The V-level is transformed by using the [WithLevelSeverity] option. If this is -// not provided it would default to a function that inverses the level with an -// offset such that [logr.Info] is transformed to [log.SeverityInfo] as the higher -// the V-level of a log line, the less critical it is considered. By default, -// V-level higher than 8 is not supported and will result in negative severity. -// For example: +// The V-level is transformed by using the [WithLevelSeverity] option. If option is +// not provided then V-level is transformed in the following way: // -// - [logr.Info] is transformed to [log.SeverityInfo]. -// - [logr.V(0)] is transformed to [log.SeverityInfo]. -// - [logr.V(1)] is transformed to [log.SeverityDebug4]. -// - [logr.V(2)] is transformed to [log.SeverityDebug3]. -// - ... -// - [logr.V(8)] is transformed to [log.SeverityTrace]. -// - [logr.V(9)] is transformed to [log.SeverityUndefined]. -// - [logr.V(10)] is transformed to log.Severity(-1). -// - [logr.Error] is transformed to [log.SeverityError]. +// - logr.Info and logr.V(0) are transformed to [log.SeverityInfo]. +// - logr.V(1) is transformed to [log.SeverityDebug]. +// - logr.V(2) and higher are transformed to [log.SeverityTrace]. // // If possible, look at alternative log bridges that provide less abstract log // level mapping such as [go.opentelemetry.io/contrib/bridges/otelslog], // [go.opentelemetry.io/contrib/bridges/otelzap], or -// [go.opentelemetry.io/contrib/bridges/otellogrus]. Which provide more direct +// [go.opentelemetry.io/contrib/bridges/otellogrus]. They provide more direct // translation of log levels. // // KeysAndValues values are transformed based on their type. The following types are @@ -97,8 +87,14 @@ func newConfig(options []Option) config { if c.levelSeverity == nil { c.levelSeverity = func(level int) log.Severity { - const sevOffset = int(log.SeverityInfo) - return log.Severity(sevOffset - level) + switch level { + case 0: + return log.SeverityInfo + case 1: + return log.SeverityDebug + default: + return log.SeverityTrace + } } } diff --git a/bridges/otellogr/logsink_test.go b/bridges/otellogr/logsink_test.go index 30820ea181f..9e310e443e0 100644 --- a/bridges/otellogr/logsink_test.go +++ b/bridges/otellogr/logsink_test.go @@ -145,19 +145,17 @@ func TestLogSink(t *testing.T) { { name: "info_with_level_severity", f: func(l *logr.Logger) { + l.V(0).Info("msg") l.V(1).Info("msg") - l.V(4).Info("msg") - l.V(8).Info("msg") - l.V(9).Info("msg") - l.V(10).Info("msg") + l.V(2).Info("msg") + l.V(3).Info("msg") }, wantRecords: map[string][]log.Record{ name: { - buildRecord(log.StringValue("msg"), time.Time{}, log.SeverityDebug4, nil), + buildRecord(log.StringValue("msg"), time.Time{}, log.SeverityInfo, nil), buildRecord(log.StringValue("msg"), time.Time{}, log.SeverityDebug, nil), buildRecord(log.StringValue("msg"), time.Time{}, log.SeverityTrace, nil), - buildRecord(log.StringValue("msg"), time.Time{}, log.SeverityUndefined, nil), - buildRecord(log.StringValue("msg"), time.Time{}, log.Severity(-1), nil), + buildRecord(log.StringValue("msg"), time.Time{}, log.SeverityTrace, nil), }, }, },