Skip to content

Commit

Permalink
feat: add WithHandler option (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
danteay committed Apr 17, 2024
1 parent ea75f8c commit 9fb7fb8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
13 changes: 5 additions & 8 deletions .github/workflows/tag_adapters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
with:
token: "${{ secrets.ACCESS_TOKEN }}"
fetch-depth: 2
ref: "main"

- name: Fetch tags for adapter
run: git fetch --tags origin '+refs/tags/adapters/${{ matrix.adapter }}/*:refs/tags/adapters/${{ matrix.adapter }}/*'

- name: Files modified
id: check
uses: tj-actions/[email protected]
with:
files: |
./adapters/${{ matrix.adapter }}/**/*.go
./adapters/${{ matrix.adapter }}/go.mod
./adapters/${{ matrix.adapter }}/go.sum
./adapters/${{ matrix.adapter }}/*
- name: Config Git User
if: steps.check.outputs.any_changed == 'true'
Expand All @@ -55,10 +56,6 @@ jobs:
if: steps.check.outputs.any_changed == 'true'
run: pip install -U commitizen

- name: Pull changes
if: steps.check.outputs.any_changed == 'true'
run: git fetch --tags origin '+refs/tags/adapters/${{ matrix.adapter }}/*:refs/tags/adapters/${{ matrix.adapter }}/*'

- id: cz
name: Create bump and changelog
if: steps.check.outputs.any_changed == 'true'
Expand Down
14 changes: 11 additions & 3 deletions adapters/slog/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

type options struct {
level levels.Level
writer io.Writer
logger *slog.Logger
level levels.Level
writer io.Writer
logger *slog.Logger
handler slog.Handler
}

type Option func(*options)
Expand All @@ -35,3 +36,10 @@ func WithLogger(logger *slog.Logger) Option {
opts.logger = logger
}
}

// WithHandler sets the handler for the logger. If a handler is set, the writer and level options will be ignored.
func WithHandler(handler slog.Handler) Option {
return func(opts *options) {
opts.handler = handler
}
}
10 changes: 10 additions & 0 deletions adapters/slog/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,13 @@ func TestOptionChaining(t *testing.T) {
t.Error("Expected writer to be the provided io.Writer, but it's not")
}
}

func TestWithHandler(t *testing.T) {
opts := &options{}

handler := slog.NewTextHandler(os.Stdout, nil)

WithHandler(handler)(opts)

assert.NotNil(t, opts.handler)
}
6 changes: 5 additions & 1 deletion adapters/slog/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@ func getSlogInstance(opts options) *slog.Logger {

level := getLevels(opts.level)

handler := slog.NewJSONHandler(opts.writer, &slog.HandlerOptions{
var handler slog.Handler = slog.NewJSONHandler(opts.writer, &slog.HandlerOptions{
AddSource: false,
Level: level,
})

if opts.handler != nil {
handler = opts.handler
}

return slog.New(handler)
}

Expand Down

0 comments on commit 9fb7fb8

Please sign in to comment.