Skip to content

Commit

Permalink
refactor: use dedicated ctx key for context logger
Browse files Browse the repository at this point in the history
Signed-off-by: lvlcn-t <75443136+lvlcn-t@users.noreply.github.com>
  • Loading branch information
lvlcn-t committed Sep 24, 2024
1 parent edc1089 commit b690357
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
6 changes: 5 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ linters-settings:
disabled: true
- name: unused-parameter

gosec:
excludes:
- "G115" # Excluded per default https://github.com/golangci/golangci-lint/pull/4941

linters:
disable-all: true
enable:
Expand All @@ -86,7 +90,7 @@ linters:
- dogsled
- dupl
- errcheck
- exportloopref
- copyloopvar
- funlen
- gocheckcompilerdirectives
- gochecknoinits
Expand Down
2 changes: 1 addition & 1 deletion internal/logger/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestLogger_LevelExtensions(t *testing.T) {
}
}

func TestLogger_CustomLevels(t *testing.T) { //nolint:gocyclo // Either higher complexity or code duplication
func TestLogger_CustomLevels(t *testing.T) {
tests := []struct {
name string
attrs []any
Expand Down
7 changes: 5 additions & 2 deletions internal/logger/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,21 @@ func NewContextWithLogger(ctx context.Context) (context.Context, context.CancelF
return IntoContext(c, FromContext(ctx)), cancel
}

// ctxKey is the key used to store the logger in the context.
type ctxKey struct{}

// IntoContext embeds the provided slog.Logger into the given context and returns the modified context.
// This function is used for passing loggers through context, allowing for context-aware logging.
func IntoContext(ctx context.Context, log Logger) context.Context {
return context.WithValue(ctx, logger{}, log)
return context.WithValue(ctx, ctxKey{}, log)
}

// FromContext extracts the slog.Logger from the provided context.
// If the context does not have a logger, it returns a new logger with the default configuration.
// This function is useful for retrieving loggers from context in different parts of an application.
func FromContext(ctx context.Context) Logger {
if ctx != nil {
if logger, ok := ctx.Value(logger{}).(Logger); ok {
if logger, ok := ctx.Value(ctxKey{}).(Logger); ok {
return logger
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/logger/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func TestNewContextWithLogger(t *testing.T) {
ctx, cancel := NewContextWithLogger(tt.parentCtx)
defer cancel()

log := ctx.Value(logger{})
log := ctx.Value(ctxKey{})
if _, ok := log.(Logger); !ok {
t.Errorf("Context does not contain Logger, got %T", log)
}
Expand Down Expand Up @@ -259,7 +259,7 @@ func TestMiddleware(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
middleware := Middleware(tt.parentCtx)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, ok := r.Context().Value(logger{}).(Logger)
_, ok := r.Context().Value(ctxKey{}).(Logger)
if tt.expectInCtx != ok {
t.Errorf("Middleware() did not inject logger correctly, got %v, want %v", ok, tt.expectInCtx)
}
Expand Down

0 comments on commit b690357

Please sign in to comment.