Skip to content

Commit

Permalink
Inverse log level translation and add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
scorpionknifes committed Oct 19, 2024
1 parent c1410b8 commit 7e0528a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
4 changes: 2 additions & 2 deletions bridges/otellogr/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func Example() {
case 0:
return log.SeverityInfo
case 1:
return log.SeverityWarn
return log.SeverityDebug
default:
return log.SeverityFatal
return log.SeverityTrace
}
}),
))
Expand Down
25 changes: 18 additions & 7 deletions bridges/otellogr/logsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,29 @@
// added as attributes. If there are multiple [context.Context] the last one
// is used.
//
// The Level is transformed by using the [WithLevelSeverity] option. If this is
// not provided it would default to a function that adds an offset to the logr
// such that [logr.Info] is transformed to [log.SeverityInfo]. For example:
// 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:
//
// - [logr.Info] is transformed to [log.SeverityInfo].
// - [logr.V(0)] is transformed to [log.SeverityInfo].
// - [logr.V(1)] is transformed to [log.SeverityInfo2].
// - [logr.V(2)] is transformed to [log.SeverityInfo3].
// - [logr.V(1)] is transformed to [log.SeverityDebug4].
// - [logr.V(2)] is transformed to [log.SeverityDebug3].
// - ...
// - [logr.V(15)] is transformed to [log.SeverityFatal4].
// - [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].
//
// 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
// translation of log levels.
//
// KeysAndValues values are transformed based on their type. The following types are
// supported:
//
Expand Down Expand Up @@ -87,7 +98,7 @@ func newConfig(options []Option) config {
if c.levelSeverity == nil {
c.levelSeverity = func(level int) log.Severity {
const sevOffset = int(log.SeverityInfo)
return log.Severity(level + sevOffset)
return log.Severity(sevOffset - level)
}
}

Expand Down
10 changes: 8 additions & 2 deletions bridges/otellogr/logsink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,17 @@ func TestLogSink(t *testing.T) {
f: func(l *logr.Logger) {
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")
},
wantRecords: map[string][]log.Record{
name: {
buildRecord(log.StringValue("msg"), time.Time{}, log.SeverityInfo2, nil),
buildRecord(log.StringValue("msg"), time.Time{}, log.SeverityWarn, nil),
buildRecord(log.StringValue("msg"), time.Time{}, log.SeverityDebug4, 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),
},
},
},
Expand Down

0 comments on commit 7e0528a

Please sign in to comment.