-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from vgarvardt/feat/slog-adapter
feat: added slog logger adapter
- Loading branch information
Showing
9 changed files
with
128 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters