From 0befb827237eb0040a9b25df88864fbcd0a77a79 Mon Sep 17 00:00:00 2001 From: Robert Hurst Date: Fri, 7 Jul 2023 15:32:33 -0700 Subject: [PATCH] Allow inline context information in log calls --- blackbox_test.go | 15 +++++++++++++++ target.go | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/blackbox_test.go b/blackbox_test.go index bb9323e..23e0111 100644 --- a/blackbox_test.go +++ b/blackbox_test.go @@ -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) +} diff --git a/target.go b/target.go index 93128b0..6dcc787 100644 --- a/target.go +++ b/target.go @@ -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) }