Skip to content
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 examples/hello-mysql/cmd/api/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func logStartup(logger modkitlogging.Logger, addr string) {
if logger == nil {
logger = modkitlogging.Nop()
logger = modkitlogging.NewNopLogger()
}
logger = logger.With(slog.String("scope", "api"))
logger.Info("server starting",
Expand Down
27 changes: 21 additions & 6 deletions examples/hello-mysql/cmd/api/startup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,32 @@ type captureLogger struct {
attrs []slog.Attr
}

func (c *captureLogger) Debug(string, ...slog.Attr) {}
func (c *captureLogger) Info(msg string, attrs ...slog.Attr) {
func (c *captureLogger) Debug(string, ...any) {}
func (c *captureLogger) Info(msg string, args ...any) {
c.messages = append(c.messages, msg)
c.attrs = append(c.attrs, attrs...)
c.attrs = append(c.attrs, attrsFromArgs(args)...)
}
func (c *captureLogger) Error(string, ...slog.Attr) {}
func (c *captureLogger) With(attrs ...slog.Attr) modkitlogging.Logger {
c.attrs = append(c.attrs, attrs...)
func (c *captureLogger) Warn(string, ...any) {}
func (c *captureLogger) Error(string, ...any) {}
func (c *captureLogger) With(args ...any) modkitlogging.Logger {
c.attrs = append(c.attrs, attrsFromArgs(args)...)
return c
}

func attrsFromArgs(args []any) []slog.Attr {
if len(args) == 0 {
return nil
}
attrs := make([]slog.Attr, 0, len(args))
for _, arg := range args {
attr, ok := arg.(slog.Attr)
if ok {
attrs = append(attrs, attr)
}
}
return attrs
}

func TestLogStartup_EmitsMessage(t *testing.T) {
logger := &captureLogger{}
logStartup(logger, ":8080")
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-mysql/cmd/migrate/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func logMigrateDebug(logger modkitlogging.Logger, msg string) {
if logger == nil {
logger = modkitlogging.Nop()
logger = modkitlogging.NewNopLogger()
}
logger.Debug(msg, slog.String("scope", "migrate"))
}
10 changes: 5 additions & 5 deletions examples/hello-mysql/cmd/migrate/debug_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"log/slog"
"testing"

modkitlogging "github.com/aryeko/modkit/modkit/logging"
Expand All @@ -11,12 +10,13 @@ type debugCaptureLogger struct {
debugMessages []string
}

func (c *debugCaptureLogger) Debug(msg string, _ ...slog.Attr) {
func (c *debugCaptureLogger) Debug(msg string, _ ...any) {
c.debugMessages = append(c.debugMessages, msg)
}
func (c *debugCaptureLogger) Info(string, ...slog.Attr) {}
func (c *debugCaptureLogger) Error(string, ...slog.Attr) {}
func (c *debugCaptureLogger) With(...slog.Attr) modkitlogging.Logger { return c }
func (c *debugCaptureLogger) Info(string, ...any) {}
func (c *debugCaptureLogger) Warn(string, ...any) {}
func (c *debugCaptureLogger) Error(string, ...any) {}
func (c *debugCaptureLogger) With(...any) modkitlogging.Logger { return c }

func TestLogMigrateDebug(t *testing.T) {
logger := &debugCaptureLogger{}
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-mysql/cmd/migrate/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func logMigrateComplete(logger modkitlogging.Logger) {
if logger == nil {
logger = modkitlogging.Nop()
logger = modkitlogging.NewNopLogger()
}
logger = logger.With(slog.String("scope", "migrate"))
logger.Info("migrations complete")
Expand Down
27 changes: 21 additions & 6 deletions examples/hello-mysql/cmd/migrate/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,32 @@ type captureLogger struct {
attrs []slog.Attr
}

func (c *captureLogger) Debug(string, ...slog.Attr) {}
func (c *captureLogger) Info(msg string, attrs ...slog.Attr) {
func (c *captureLogger) Debug(string, ...any) {}
func (c *captureLogger) Info(msg string, args ...any) {
c.messages = append(c.messages, msg)
c.attrs = append(c.attrs, attrs...)
c.attrs = append(c.attrs, attrsFromArgs(args)...)
}
func (c *captureLogger) Error(string, ...slog.Attr) {}
func (c *captureLogger) With(attrs ...slog.Attr) modkitlogging.Logger {
c.attrs = append(c.attrs, attrs...)
func (c *captureLogger) Warn(string, ...any) {}
func (c *captureLogger) Error(string, ...any) {}
func (c *captureLogger) With(args ...any) modkitlogging.Logger {
c.attrs = append(c.attrs, attrsFromArgs(args)...)
return c
}

func attrsFromArgs(args []any) []slog.Attr {
if len(args) == 0 {
return nil
}
attrs := make([]slog.Attr, 0, len(args))
for _, arg := range args {
attr, ok := arg.(slog.Attr)
if ok {
attrs = append(attrs, attr)
}
}
return attrs
}

func TestLogMigrateComplete(t *testing.T) {
logger := &captureLogger{}
logMigrateComplete(logger)
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-mysql/cmd/seed/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func logSeedDebug(logger modkitlogging.Logger, msg string) {
if logger == nil {
logger = modkitlogging.Nop()
logger = modkitlogging.NewNopLogger()
}
logger.Debug(msg, slog.String("scope", "seed"))
}
10 changes: 5 additions & 5 deletions examples/hello-mysql/cmd/seed/debug_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"log/slog"
"testing"

modkitlogging "github.com/aryeko/modkit/modkit/logging"
Expand All @@ -11,12 +10,13 @@ type debugCaptureLogger struct {
debugMessages []string
}

func (c *debugCaptureLogger) Debug(msg string, _ ...slog.Attr) {
func (c *debugCaptureLogger) Debug(msg string, _ ...any) {
c.debugMessages = append(c.debugMessages, msg)
}
func (c *debugCaptureLogger) Info(string, ...slog.Attr) {}
func (c *debugCaptureLogger) Error(string, ...slog.Attr) {}
func (c *debugCaptureLogger) With(...slog.Attr) modkitlogging.Logger { return c }
func (c *debugCaptureLogger) Info(string, ...any) {}
func (c *debugCaptureLogger) Warn(string, ...any) {}
func (c *debugCaptureLogger) Error(string, ...any) {}
func (c *debugCaptureLogger) With(...any) modkitlogging.Logger { return c }

func TestLogSeedDebug(t *testing.T) {
logger := &debugCaptureLogger{}
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-mysql/cmd/seed/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func logSeedComplete(logger modkitlogging.Logger) {
if logger == nil {
logger = modkitlogging.Nop()
logger = modkitlogging.NewNopLogger()
}
logger = logger.With(slog.String("scope", "seed"))
logger.Info("seed complete")
Expand Down
27 changes: 21 additions & 6 deletions examples/hello-mysql/cmd/seed/logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,32 @@ type captureLogger struct {
attrs []slog.Attr
}

func (c *captureLogger) Debug(string, ...slog.Attr) {}
func (c *captureLogger) Info(msg string, attrs ...slog.Attr) {
func (c *captureLogger) Debug(string, ...any) {}
func (c *captureLogger) Info(msg string, args ...any) {
c.messages = append(c.messages, msg)
c.attrs = append(c.attrs, attrs...)
c.attrs = append(c.attrs, attrsFromArgs(args)...)
}
func (c *captureLogger) Error(string, ...slog.Attr) {}
func (c *captureLogger) With(attrs ...slog.Attr) modkitlogging.Logger {
c.attrs = append(c.attrs, attrs...)
func (c *captureLogger) Warn(string, ...any) {}
func (c *captureLogger) Error(string, ...any) {}
func (c *captureLogger) With(args ...any) modkitlogging.Logger {
c.attrs = append(c.attrs, attrsFromArgs(args)...)
return c
}

func attrsFromArgs(args []any) []slog.Attr {
if len(args) == 0 {
return nil
}
attrs := make([]slog.Attr, 0, len(args))
for _, arg := range args {
attr, ok := arg.(slog.Attr)
if ok {
attrs = append(attrs, attr)
}
}
return attrs
}

func TestLogSeedComplete(t *testing.T) {
logger := &captureLogger{}
logSeedComplete(logger)
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-mysql/internal/modules/users/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type service struct {

func NewService(repo Repository, logger modkitlogging.Logger) Service {
if logger == nil {
logger = modkitlogging.Nop()
logger = modkitlogging.NewNopLogger()
}
logger = logger.With(slog.String("scope", "users"))
return &service{repo: repo, logger: logger}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,32 @@ type captureLogger struct {
attrs []slog.Attr
}

func (c *captureLogger) Debug(msg string, attrs ...slog.Attr) {
func (c *captureLogger) Debug(msg string, args ...any) {
c.debugMessages = append(c.debugMessages, msg)
c.attrs = append(c.attrs, attrs...)
c.attrs = append(c.attrs, attrsFromArgs(args)...)
}
func (c *captureLogger) Info(string, ...slog.Attr) {}
func (c *captureLogger) Error(string, ...slog.Attr) {}
func (c *captureLogger) With(attrs ...slog.Attr) modkitlogging.Logger {
c.attrs = append(c.attrs, attrs...)
func (c *captureLogger) Info(string, ...any) {}
func (c *captureLogger) Warn(string, ...any) {}
func (c *captureLogger) Error(string, ...any) {}
func (c *captureLogger) With(args ...any) modkitlogging.Logger {
c.attrs = append(c.attrs, attrsFromArgs(args)...)
return c
}

func attrsFromArgs(args []any) []slog.Attr {
if len(args) == 0 {
return nil
}
attrs := make([]slog.Attr, 0, len(args))
for _, arg := range args {
attr, ok := arg.(slog.Attr)
if ok {
attrs = append(attrs, attr)
}
}
return attrs
}

func TestService_EmitsDebugLogs(t *testing.T) {
repo := &stubRepo{}
logger := &captureLogger{}
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-mysql/internal/platform/logging/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type config struct {

func New() modkitlogging.Logger {
logger := newLogger(os.Stdout)
return modkitlogging.NewSlog(logger)
return modkitlogging.NewSlogLogger(logger)
}

func newLogger(w io.Writer) *slog.Logger {
Expand Down
Loading
Loading