Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,17 @@ ctrl, _ := NewAppController(
)
```

The controller stores contextual information (channel, correlation ID,
direction, …) in the `context.Context` passed to your logger. To retrieve it
without unwrapping each key manually, use `extensions.LogInfosFromContext`:

```golang
func (logger SimpleLogger) Info(ctx context.Context, msg string, info ...extensions.LogInfo) {
info = append(info, extensions.LogInfosFromContext(ctx)...)
// ... log msg with info
}
```

### Versioning

If you are in need to do a migration or support multiple versions of your
Expand Down
25 changes: 25 additions & 0 deletions pkg/extensions/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ type LogInfo struct {
Value any
}

// LogInfosFromContext extracts the asyncapi-codegen values carried in the
// context (provider, channel, direction, correlation ID, broker message and
// specification version) into a slice of LogInfo.
//
// It is a convenience for custom Logger implementations that want this
// information without having to know and unwrap each context key individually.
func LogInfosFromContext(ctx context.Context) []LogInfo {
var infos []LogInfo

add := func(key string, ctxKey ContextKey) {
IfContextSetWith(ctx, ctxKey, func(value any) {
infos = append(infos, LogInfo{Key: key, Value: value})
})
}

add("version", ContextKeyIsVersion)
add("provider", ContextKeyIsProvider)
add("channel", ContextKeyIsChannel)
add("direction", ContextKeyIsDirection)
add("correlationID", ContextKeyIsCorrelationID)
add("brokerMessage", ContextKeyIsBrokerMessage)

return infos
}

// Logger is the interface that must be implemented by a logger.
type Logger interface {
// Info logs information based on a message and key-value elements
Expand Down
36 changes: 36 additions & 0 deletions pkg/extensions/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package extensions

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

// TestLogInfosFromContext ensures the helper extracts the asyncapi-codegen
// context values into LogInfo entries, and only the ones that are set (#133).
func TestLogInfosFromContext(t *testing.T) {
ctx := context.Background()
ctx = context.WithValue(ctx, ContextKeyIsChannel, "my-channel")
ctx = context.WithValue(ctx, ContextKeyIsCorrelationID, "corr-1")

infos := LogInfosFromContext(ctx)

got := make(map[string]any, len(infos))
for _, info := range infos {
got[info.Key] = info.Value
}

assert.Equal(t, "my-channel", got["channel"])
assert.Equal(t, "corr-1", got["correlationID"])

// Values that were not set must not be present.
_, hasProvider := got["provider"]
assert.False(t, hasProvider)
assert.Len(t, infos, 2)
}

// TestLogInfosFromContextEmpty ensures an empty context yields no LogInfo.
func TestLogInfosFromContextEmpty(t *testing.T) {
assert.Empty(t, LogInfosFromContext(context.Background()))
}
Loading