Skip to content

Commit

Permalink
Simpler level transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpionknifes committed Oct 26, 2024
1 parent fd48772 commit be7773d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
4 changes: 2 additions & 2 deletions bridges/otellogr/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
32 changes: 14 additions & 18 deletions bridges/otellogr/logsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
}

Expand Down
12 changes: 5 additions & 7 deletions bridges/otellogr/logsink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
},
},
Expand Down

0 comments on commit be7773d

Please sign in to comment.