Skip to content

Commit

Permalink
Merge pull request #179 from vgarvardt/feat/slog-adapter
Browse files Browse the repository at this point in the history
feat: added slog logger adapter
  • Loading branch information
vgarvardt committed Apr 28, 2023
2 parents 22d09c3 + 8bca9b7 commit 8e9dc8c
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 4 deletions.
7 changes: 5 additions & 2 deletions .github/actions/setup-deps/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
name: setup-deps
description: Install all the dependencies required for workflows
inputs:
go-version:
required: true
description: The Go version to download (if necessary) and use. Supports semver spec and ranges.
token:
required: true
description: GitHub token, most likely "secrets.GITHUB_TOKEN"
runs:
using: composite
steps:
- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version-file: ./go.mod
go-version: ${{ inputs.go-version }}

- name: Install Task
uses: arduino/setup-task@v1
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
- name: Setup dependencies
uses: ./.github/actions/setup-deps
with:
go-version: '1.19'
token: ${{secrets.GITHUB_TOKEN}}

- name: Initialize CodeQL
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
- name: Setup dependencies
uses: ./.github/actions/setup-deps
with:
go-version: '1.19'
token: ${{secrets.GITHUB_TOKEN}}

- name: Lint Golang
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ jobs:
test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.19', '1.20', 'stable' ]
timeout-minutes: 10
steps:
- name: Check out code
Expand All @@ -22,6 +25,7 @@ jobs:
- name: Setup dependencies
uses: ./.github/actions/setup-deps
with:
go-version: '1.19'
token: ${{secrets.GITHUB_TOKEN}}

- name: Run tests
Expand All @@ -34,3 +38,12 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.txt
fail_ci_if_error: false

summary:
name: Test
runs-on: ubuntu-latest
needs: [ test ]
timeout-minutes: 1
steps:
- name: Dummy task
run: echo 'Dummy summary task to have one PR status for all tested versions'
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,9 @@ drivers have been implemented:
with `adapter.NewStdLogger(...)`.
- Uber `zap` - adapter that uses [`go.uber.org/zap`](https://pkg.go.dev/go.uber.org/zap) logger for logs output.
Instantiate it with `adapter/zap.New(...)`.
- Uber `zerolog` - adapter that uses [`github.com/rs/zerolog`](https://pkg.go.dev/github.com/rs/zerolog) logger for logs
output. Instantiate it with `adapter/zerolog.New(...)`.
- Olivier Poitrey's `zerolog` - adapter that uses [`github.com/rs/zerolog`](https://pkg.go.dev/github.com/rs/zerolog)
logger for logs output. Instantiate it with `adapter/zerolog.New(...)`.
- (Future) Stdlib `slog` - adapter that uses [`golang.org/x/exp/slog`](https://pkg.go.dev/golang.org/x/exp/slog)
logger for logs output. Instantiate it with `adapter/slog.New(...)`. Adapter is expected to be
[included into stdlib](https://github.com/golang/go/issues/56345), once it is there - dependency will be changed from
`golang.org/x/exp/slog` to `log/slog` or whenever it will land.
48 changes: 48 additions & 0 deletions adapter/slog/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//go:build go1.20

package slog

import (
libSLog "golang.org/x/exp/slog"

"github.com/vgarvardt/gue/v5/adapter"
)

var _ adapter.Logger = &slog{}

type slog struct {
l *libSLog.Logger
}

// New instantiates new adapter.Logger using go.uber.org/slog
func New(l *libSLog.Logger) adapter.Logger {
return &slog{l}
}

// Debug implements Logger.Debug for go.uber.org/slog logger
func (l *slog) Debug(msg string, fields ...adapter.Field) {
l.l.Debug(msg, l.slogFields(fields...)...)
}

// Info implements Logger.Debug for go.uber.org/slog logger
func (l *slog) Info(msg string, fields ...adapter.Field) {
l.l.Info(msg, l.slogFields(fields...)...)
}

// Error implements Logger.Debug for go.uber.org/slog logger
func (l *slog) Error(msg string, fields ...adapter.Field) {
l.l.Error(msg, l.slogFields(fields...)...)
}

// With implements nested logger for go.uber.org/slog logger
func (l *slog) With(fields ...adapter.Field) adapter.Logger {
return New(l.l.With(l.slogFields(fields...)...))
}

func (l *slog) slogFields(fields ...adapter.Field) []any {
result := make([]any, 0, len(fields)*2)
for _, f := range fields {
result = append(result, f.Key, f.Value)
}
return result
}
51 changes: 51 additions & 0 deletions adapter/slog/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//go:build go1.20

package slog

import (
"bytes"
"errors"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
libSLog "golang.org/x/exp/slog"

"github.com/vgarvardt/gue/v5/adapter"
)

func TestNew(t *testing.T) {
var buf bytes.Buffer
h := libSLog.HandlerOptions{
AddSource: true,
Level: libSLog.LevelDebug,
}.NewJSONHandler(&buf)
l := libSLog.New(h)
ll := New(l)

err := errors.New("something went wrong")

ll.Debug("debug-1", adapter.F("debug-key", "debug-val"))
ll.Info("info-1", adapter.F("info-key", "info-val"))
ll.Error("error-1", adapter.F("error-key", "error-val"))
ll.Error("error-2", adapter.Err(err))

lll := ll.With(adapter.F("nested-key", "nested-val"))
lll.Info("info-2", adapter.F("info-key-2", "info-val-2"))

lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
require.Len(t, lines, 5)

for line, contains := range [][]string{
{`"level":"DEBUG"`, `"msg":"debug-1"`, `"debug-key":"debug-val"`},
{`"level":"INFO"`, `"msg":"info-1"`, `"info-key":"info-val"`},
{`"level":"ERROR"`, `"msg":"error-1"`, `"error-key":"error-val"`},
{`"level":"ERROR"`, `"msg":"error-2"`, `"error":"something went wrong"`},
{`"level":"INFO"`, `"msg":"info-2"`, `"info-key-2":"info-val-2"`, `"nested-key":"nested-val"`},
} {
for _, sub := range contains {
assert.Contains(t, lines[line], sub)
}
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
go.opentelemetry.io/otel/trace v1.15.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.24.0
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53
golang.org/x/sync v0.1.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53 h1:5llv2sWeaMSnA3w2kS57ouQQ4pudlXrR0dCgw51QK9o=
golang.org/x/exp v0.0.0-20230425010034-47ecfdc1ba53/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
Expand Down

0 comments on commit 8e9dc8c

Please sign in to comment.