Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt slog.Logger instead of custom logger implementation #230

Merged
merged 8 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21
check-latest: true
cache: true

Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21
check-latest: true
cache: true

Expand Down Expand Up @@ -51,7 +51,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21
check-latest: true
cache: true

Expand Down Expand Up @@ -85,7 +85,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21
check-latest: true
cache: true

Expand All @@ -111,7 +111,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21
check-latest: true
cache: true

Expand Down
4 changes: 2 additions & 2 deletions activity/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package activity

import (
"context"
"log/slog"

"github.com/cschleiden/go-workflows/internal/activity"
"github.com/cschleiden/go-workflows/log"
)

// Logger returns a logger with the workflow instance this activity is executed for set as default fields
func Logger(ctx context.Context) log.Logger {
func Logger(ctx context.Context) *slog.Logger {
return activity.GetActivityState(ctx).Logger
}
7 changes: 3 additions & 4 deletions activitytester/activitytester.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package activitytester

import (
"context"
"log/slog"

"github.com/cschleiden/go-workflows/internal/activity"
"github.com/cschleiden/go-workflows/internal/core"
dlogger "github.com/cschleiden/go-workflows/internal/logger"
"github.com/cschleiden/go-workflows/log"
)

func WithActivityTestState(ctx context.Context, activityID, instanceID string, logger log.Logger) context.Context {
func WithActivityTestState(ctx context.Context, activityID, instanceID string, logger *slog.Logger) context.Context {
if logger == nil {
logger = dlogger.NewDefaultLogger()
logger = slog.Default()
}

return activity.WithActivityState(ctx, activity.NewActivityState(activityID, core.NewWorkflowInstance(instanceID, ""), logger))
Expand Down
4 changes: 2 additions & 2 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package backend
import (
"context"
"errors"
"log/slog"

"github.com/cschleiden/go-workflows/internal/contextpropagation"
"github.com/cschleiden/go-workflows/internal/converter"
core "github.com/cschleiden/go-workflows/internal/core"
"github.com/cschleiden/go-workflows/internal/history"
"github.com/cschleiden/go-workflows/internal/task"
"github.com/cschleiden/go-workflows/log"
"github.com/cschleiden/go-workflows/metrics"
"github.com/cschleiden/go-workflows/workflow"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -72,7 +72,7 @@ type Backend interface {
GetStats(ctx context.Context) (*Stats, error)

// Logger returns the configured logger for the backend
Logger() log.Logger
Logger() *slog.Logger

// Tracer returns the configured trace provider for the backend
Tracer() trace.Tracer
Expand Down
23 changes: 12 additions & 11 deletions backend/mock_Backend.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions backend/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"strings"
"time"

Expand All @@ -17,7 +18,6 @@ import (
"github.com/cschleiden/go-workflows/internal/history"
"github.com/cschleiden/go-workflows/internal/metrickeys"
"github.com/cschleiden/go-workflows/internal/task"
"github.com/cschleiden/go-workflows/log"
"github.com/cschleiden/go-workflows/metrics"
"github.com/cschleiden/go-workflows/workflow"
_ "github.com/go-sql-driver/mysql"
Expand Down Expand Up @@ -63,7 +63,7 @@ type mysqlBackend struct {
options backend.Options
}

func (b *mysqlBackend) Logger() log.Logger {
func (b *mysqlBackend) Logger() *slog.Logger {
return b.options.Logger
}

Expand Down
11 changes: 5 additions & 6 deletions backend/options.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package backend

import (
"log/slog"
"time"

"github.com/cschleiden/go-workflows/internal/contextpropagation"
"github.com/cschleiden/go-workflows/internal/converter"
"github.com/cschleiden/go-workflows/internal/logger"
mi "github.com/cschleiden/go-workflows/internal/metrics"
"github.com/cschleiden/go-workflows/internal/tracing"
"github.com/cschleiden/go-workflows/log"
"github.com/cschleiden/go-workflows/metrics"
"github.com/cschleiden/go-workflows/workflow"
"go.opentelemetry.io/otel/trace"
)

type Options struct {
Logger log.Logger
Logger *slog.Logger

Metrics metrics.Client

Expand Down Expand Up @@ -46,7 +45,7 @@ var DefaultOptions Options = Options{
WorkflowLockTimeout: time.Minute,
ActivityLockTimeout: time.Minute * 2,

Logger: logger.NewDefaultLogger(),
Logger: slog.Default(),
Metrics: mi.NewNoopMetricsClient(),
TracerProvider: trace.NewNoopTracerProvider(),
Converter: converter.DefaultConverter,
Expand All @@ -62,7 +61,7 @@ func WithStickyTimeout(timeout time.Duration) BackendOption {
}
}

func WithLogger(logger log.Logger) BackendOption {
func WithLogger(logger *slog.Logger) BackendOption {
return func(o *Options) {
o.Logger = logger
}
Expand Down Expand Up @@ -100,7 +99,7 @@ func ApplyOptions(opts ...BackendOption) Options {
}

if options.Logger == nil {
options.Logger = logger.NewDefaultLogger()
options.Logger = slog.Default()
}

return options
Expand Down
4 changes: 2 additions & 2 deletions backend/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package redis
import (
"context"
"fmt"
"log/slog"
"time"

"github.com/cschleiden/go-workflows/backend"
Expand All @@ -11,7 +12,6 @@ import (
"github.com/cschleiden/go-workflows/internal/core"
"github.com/cschleiden/go-workflows/internal/history"
"github.com/cschleiden/go-workflows/internal/metrickeys"
"github.com/cschleiden/go-workflows/log"
"github.com/cschleiden/go-workflows/metrics"
"github.com/redis/go-redis/v9"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -86,7 +86,7 @@ type activityData struct {
Event *history.Event `json:"event,omitempty"`
}

func (rb *redisBackend) Logger() log.Logger {
func (rb *redisBackend) Logger() *slog.Logger {
return rb.options.Logger
}

Expand Down
4 changes: 2 additions & 2 deletions backend/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"strings"
"time"

Expand All @@ -17,7 +18,6 @@ import (
"github.com/cschleiden/go-workflows/internal/history"
"github.com/cschleiden/go-workflows/internal/metrickeys"
"github.com/cschleiden/go-workflows/internal/task"
"github.com/cschleiden/go-workflows/log"
"github.com/cschleiden/go-workflows/metrics"
"github.com/cschleiden/go-workflows/workflow"
"github.com/google/uuid"
Expand Down Expand Up @@ -67,7 +67,7 @@ type sqliteBackend struct {

var _ backend.Backend = (*sqliteBackend)(nil)

func (sb *sqliteBackend) Logger() log.Logger {
func (sb *sqliteBackend) Logger() *slog.Logger {
return sb.options.Logger
}

Expand Down
2 changes: 1 addition & 1 deletion backend/test/e2e.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ func EndToEndBackendTest(t *testing.T, setup func(options ...backend.BackendOpti

instance := runWorkflow(t, ctx, c, wf, 0)

r, err := client.GetWorkflowResult[int](ctx, c, instance, time.Second*10)
r, err := client.GetWorkflowResult[int](ctx, c, instance, time.Second*20)
require.NoError(t, err)
require.Equal(t, 3, r)
},
Expand Down
3 changes: 2 additions & 1 deletion bench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"log"
"log/slog"
"os"
"sync"
"time"
Expand Down Expand Up @@ -38,7 +39,7 @@ func main() {
defer cancel()

mm := newMemMetrics()
ba := getBackend(*b, backend.WithLogger(&nullLogger{}), backend.WithMetrics(mm))
ba := getBackend(*b, backend.WithLogger(slog.New(&nullHandler{})), backend.WithMetrics(mm))

wo := worker.DefaultWorkerOptions
wo.WorkflowExecutorCacheSize = *cacheSize
Expand Down
33 changes: 17 additions & 16 deletions bench/nooplog.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
package main

import "github.com/cschleiden/go-workflows/log"
import (
"context"
"log/slog"
)

type nullLogger struct {
defaultFields []interface{}
type nullHandler struct {
}

// Debug implements log.Logger
func (*nullLogger) Debug(msg string, fields ...interface{}) {
// Enabled implements slog.Handler.
func (*nullHandler) Enabled(context.Context, slog.Level) bool {
return false
}

// Error implements log.Logger
func (*nullLogger) Error(msg string, fields ...interface{}) {
// Handle implements slog.Handler.
func (*nullHandler) Handle(context.Context, slog.Record) error {
return nil
}

// Panic implements log.Logger
func (*nullLogger) Panic(msg string, fields ...interface{}) {
}

// Warn implements log.Logger
func (*nullLogger) Warn(msg string, fields ...interface{}) {
// WithAttrs implements slog.Handler.
func (nl *nullHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
return nl
}

// With implements log.Logger
func (nl *nullLogger) With(fields ...interface{}) log.Logger {
// WithGroup implements slog.Handler.
func (nl *nullHandler) WithGroup(name string) slog.Handler {
return nl
}

var _ log.Logger = (*nullLogger)(nil)
var _ slog.Handler = (*nullHandler)(nil)
6 changes: 3 additions & 3 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"bytes"
"context"
"log/slog"
"testing"
"time"

Expand All @@ -11,7 +12,6 @@ import (
"github.com/cschleiden/go-workflows/internal/converter"
"github.com/cschleiden/go-workflows/internal/core"
"github.com/cschleiden/go-workflows/internal/history"
"github.com/cschleiden/go-workflows/internal/logger"
"github.com/cschleiden/go-workflows/workflow"
"github.com/google/uuid"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -103,7 +103,7 @@ func Test_Client_SignalWorkflow(t *testing.T) {

b := &backend.MockBackend{}
b.On("Tracer").Return(trace.NewNoopTracerProvider().Tracer("test"))
b.On("Logger").Return(logger.NewDefaultLogger())
b.On("Logger").Return(slog.Default())
b.On("Converter").Return(converter.DefaultConverter)
b.On("SignalWorkflow", mock.Anything, instanceID, mock.MatchedBy(func(event *history.Event) bool {
return event.Type == history.EventType_SignalReceived &&
Expand Down Expand Up @@ -132,7 +132,7 @@ func Test_Client_SignalWorkflow_WithArgs(t *testing.T) {

b := &backend.MockBackend{}
b.On("Tracer").Return(trace.NewNoopTracerProvider().Tracer("test"))
b.On("Logger").Return(logger.NewDefaultLogger())
b.On("Logger").Return(slog.Default())
b.On("Converter").Return(converter.DefaultConverter)
b.On("SignalWorkflow", mock.Anything, instanceID, mock.MatchedBy(func(event *history.Event) bool {
return event.Type == history.EventType_SignalReceived &&
Expand Down
Loading
Loading