Skip to content

Commit

Permalink
Allow inline context information in log calls
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertWHurst committed Jul 7, 2023
1 parent 3e87284 commit 0befb82
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions blackbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,18 @@ func TestLoggerCtxToReturnLogger(t *testing.T) {
assert.Equal(t, "Message", logged.Values[0].(string))
assert.Equal(t, blackbox.Ctx{"key": "value"}, logged.Context)
}

func TestLoggerInlineCtx(t *testing.T) {
logger := blackbox.New()
testTarget := blackbox.NewTestTarget()
logger.AddTarget(testTarget)

logger.Log(blackbox.Info, "Message", blackbox.Ctx{"key": "value"})

logged, ok := testTarget.LastLogged()

assert.Equal(t, true, ok)
assert.Equal(t, blackbox.Info, logged.Level)
assert.Equal(t, "Message", logged.Values[0].(string))
assert.Equal(t, blackbox.Ctx{"key": "value"}, logged.Context)
}
7 changes: 7 additions & 0 deletions target.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ type targetSet struct {
}

func (t *targetSet) log(level Level, values []interface{}, context Ctx) {
for index, value := range values {
if ctx, ok := value.(Ctx); ok {
context = context.Extend(ctx)
values = append(values[:index], values[index+1:]...)
}
}

for _, target := range t.targets {
target.Log(level, values, context)
}
Expand Down

0 comments on commit 0befb82

Please sign in to comment.