Skip to content

Commit

Permalink
Add slog logger provider
Browse files Browse the repository at this point in the history
  • Loading branch information
maksim.konovalov committed Jan 23, 2025
1 parent 94ae0cc commit 2cd8479
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Unreleased

FEATURES:
- Slog provider is available now. Use NewSlogLogger for slog log provider creation.

BUG FIXES:
- Fixed go.mod and go.sum files with go mod tidy.

Expand Down
36 changes: 36 additions & 0 deletions providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package vshard_router //nolint:revive

import (
"context"
"fmt"
"log"
"log/slog"
"time"
)

Expand All @@ -12,6 +14,8 @@ var (

// Ensure StdoutLoggerf implements LogfProvider
_ LogfProvider = StdoutLoggerf{}
// Ensure SlogLoggerf implements LogfProvider
_ LogfProvider = &SlogLoggerf{}
)

// LogfProvider an interface to inject a custom logger.
Expand Down Expand Up @@ -83,6 +87,38 @@ func (s StdoutLoggerf) Errorf(_ context.Context, format string, v ...any) {
s.printLevel(StdoutLogError, "[ERROR] ", format, v...)
}

// NewSlogLogger wraps slog logger
func NewSlogLogger(logger *slog.Logger) LogfProvider {
return &SlogLoggerf{
Logger: logger,
}
}

// SlogLoggerf is adapter for slog to Logger interface.
type SlogLoggerf struct {
Logger *slog.Logger
}

// Debugf implements Debugf method for LogfProvider interface
func (s *SlogLoggerf) Debugf(ctx context.Context, format string, v ...any) {
s.Logger.DebugContext(ctx, fmt.Sprintf(format, v...))
}

// Infof implements Infof method for LogfProvider interface
func (s SlogLoggerf) Infof(ctx context.Context, format string, v ...any) {
s.Logger.InfoContext(ctx, fmt.Sprintf(format, v...))
}

// Warnf implements Warnf method for LogfProvider interface
func (s SlogLoggerf) Warnf(ctx context.Context, format string, v ...any) {
s.Logger.WarnContext(ctx, fmt.Sprintf(format, v...))
}

// Errorf implements Errorf method for LogfProvider interface
func (s SlogLoggerf) Errorf(ctx context.Context, format string, v ...any) {
s.Logger.ErrorContext(ctx, fmt.Sprintf(format, v...))
}

// Metrics

// MetricsProvider is an interface for passing library metrics to your prometheus/graphite and other metrics
Expand Down
43 changes: 43 additions & 0 deletions providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package vshard_router_test

import (
"context"
"log/slog"
"os"
"testing"
"time"

Expand Down Expand Up @@ -49,3 +51,44 @@ func TestStdoutLogger(t *testing.T) {
stdoutLogger.Debugf(ctx, "")
})
}

func TestNewSlogLogger(t *testing.T) {
var slogProvider vshardrouter.LogfProvider

require.NotPanics(t, func() {
slogProvider = vshardrouter.NewSlogLogger(nil)
})

require.Panics(t, func() {
slogProvider.Warnf(context.TODO(), "")
})
}

func TestSlogProvider(t *testing.T) {
ctx := context.Background()

// create new logger handler
handler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelError,
})
// create new SLogger instance
sLogger := slog.New(handler)

logProvider := vshardrouter.NewSlogLogger(sLogger)

require.NotPanics(t, func() {
logProvider.Infof(ctx, "test %s", "s")
})

require.NotPanics(t, func() {
logProvider.Warnf(ctx, "test %s", "s")
})

require.NotPanics(t, func() {
logProvider.Errorf(ctx, "test %s", "s")
})

require.NotPanics(t, func() {
logProvider.Debugf(ctx, "test %s", "s")
})
}

0 comments on commit 2cd8479

Please sign in to comment.