Skip to content

Commit

Permalink
chore: readd context methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lvlcn-t committed Mar 2, 2024
1 parent 7ab3485 commit f764136
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 50 deletions.
64 changes: 48 additions & 16 deletions internal/logger/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,47 @@ var _ Logger = (*logger)(nil)
// It extends the Core interface with additional logging methods.
type Logger interface {
// Debug logs at LevelDebug.
Debug(ctx context.Context, msg string, args ...any)
Debug(msg string, args ...any)
// Debugf logs at LevelDebug.
// Arguments are handled in the manner of fmt.Printf.
Debugf(ctx context.Context, msg string, args ...any)
Debugf(msg string, args ...any)
// DebugContext logs at LevelDebug with the given context.
DebugContext(ctx context.Context, msg string, args ...any)
// Info logs at LevelInfo.
Info(ctx context.Context, msg string, args ...any)
Info(msg string, args ...any)
// Infof logs at LevelInfo.
// Arguments are handled in the manner of fmt.Printf.
Infof(ctx context.Context, msg string, args ...any)
Infof(msg string, args ...any)
// InfoContext logs at LevelInfo with the given context.
InfoContext(ctx context.Context, msg string, args ...any)
// Warn logs at LevelWarn.
Warn(ctx context.Context, msg string, args ...any)
Warn(msg string, args ...any)
// Warnf logs at LevelWarn.
// Arguments are handled in the manner of fmt.Printf.
Warnf(ctx context.Context, msg string, args ...any)
Warnf(msg string, args ...any)
// WarnContext logs at LevelWarn with the given context.
WarnContext(ctx context.Context, msg string, args ...any)
// Error logs at LevelError.
Error(ctx context.Context, msg string, args ...any)
Error(msg string, args ...any)
// Errorf logs at LevelError.
// Arguments are handled in the manner of fmt.Printf.
Errorf(ctx context.Context, msg string, args ...any)
Errorf(msg string, args ...any)
// ErrorContext logs at LevelError with the given context.
ErrorContext(ctx context.Context, msg string, args ...any)
// Panic logs at LevelPanic and then panics with the given message.
Panic(ctx context.Context, msg string, args ...any)
Panic(msg string, args ...any)
// Panicf logs at LevelPanic and then panics.
// Arguments are handled in the manner of fmt.Printf.
Panicf(ctx context.Context, msg string, args ...any)
Panicf(msg string, args ...any)
// PanicContext logs at LevelPanic with the given context and then panics with the given message.
PanicContext(ctx context.Context, msg string, args ...any)
// Fatal logs at LevelFatal and then calls os.Exit(1).
Fatal(ctx context.Context, msg string, args ...any)
Fatal(msg string, args ...any)
// Fatalf logs at LevelFatal and then calls os.Exit(1).
// Arguments are handled in the manner of fmt.Printf.
Fatalf(ctx context.Context, msg string, args ...any)
Fatalf(msg string, args ...any)
// FatalContext logs at LevelFatal with the given context and then calls os.Exit(1).
FatalContext(ctx context.Context, msg string, args ...any)

// With calls Logger.With on the default logger.
With(args ...any) Logger
Expand Down Expand Up @@ -81,22 +93,42 @@ type Logger interface {
type logger struct{ *slog.Logger }

// Debug logs at LevelDebug.
func (l *logger) Debug(ctx context.Context, msg string, a ...any) {
func (l *logger) Debug(msg string, a ...any) {
l.logAttrs(context.Background(), LevelDebug, msg, a...)
}

// DebugContext logs at LevelDebug.
func (l *logger) DebugContext(ctx context.Context, msg string, a ...any) {
l.logAttrs(ctx, LevelDebug, msg, a...)
}

// Info logs at LevelInfo.
func (l *logger) Info(ctx context.Context, msg string, a ...any) {
func (l *logger) Info(msg string, a ...any) {
l.logAttrs(context.Background(), LevelInfo, msg, a...)
}

// InfoContext logs at LevelInfo.
func (l *logger) InfoContext(ctx context.Context, msg string, a ...any) {
l.logAttrs(ctx, LevelInfo, msg, a...)
}

// Warn logs at LevelWarn.
func (l *logger) Warn(ctx context.Context, msg string, a ...any) {
func (l *logger) Warn(msg string, a ...any) {
l.logAttrs(context.Background(), LevelWarn, msg, a...)
}

// WarnContext logs at LevelWarn.
func (l *logger) WarnContext(ctx context.Context, msg string, a ...any) {
l.logAttrs(ctx, LevelWarn, msg, a...)
}

// Error logs at LevelError.
func (l *logger) Error(ctx context.Context, msg string, a ...any) {
func (l *logger) Error(msg string, a ...any) {
l.logAttrs(context.Background(), LevelError, msg, a...)
}

// ErrorContext logs at LevelError.
func (l *logger) ErrorContext(ctx context.Context, msg string, a ...any) {
l.logAttrs(ctx, LevelError, msg, a...)
}

Expand Down
44 changes: 28 additions & 16 deletions internal/logger/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,63 @@ import (

// Debugf logs at LevelDebug.
// Arguments are handled in the manner of fmt.Printf.
func (l *logger) Debugf(ctx context.Context, msg string, args ...any) {
l.logAttrs(ctx, LevelDebug, fmt.Sprintf(msg, args...))
func (l *logger) Debugf(msg string, args ...any) {
l.logAttrs(context.Background(), LevelDebug, fmt.Sprintf(msg, args...))
}

// Infof logs at LevelInfo.
// Arguments are handled in the manner of fmt.Printf.
func (l *logger) Infof(ctx context.Context, msg string, args ...any) {
l.logAttrs(ctx, LevelInfo, fmt.Sprintf(msg, args...))
func (l *logger) Infof(msg string, args ...any) {
l.logAttrs(context.Background(), LevelInfo, fmt.Sprintf(msg, args...))
}

// Warnf logs at LevelWarn.
// Arguments are handled in the manner of fmt.Printf.
func (l *logger) Warnf(ctx context.Context, msg string, args ...any) {
l.logAttrs(ctx, LevelWarn, fmt.Sprintf(msg, args...))
func (l *logger) Warnf(msg string, args ...any) {
l.logAttrs(context.Background(), LevelWarn, fmt.Sprintf(msg, args...))
}

// Errorf logs at LevelError.
// Arguments are handled in the manner of fmt.Printf.
func (l *logger) Errorf(ctx context.Context, msg string, args ...any) {
l.logAttrs(ctx, LevelError, fmt.Sprintf(msg, args...))
func (l *logger) Errorf(msg string, args ...any) {
l.logAttrs(context.Background(), LevelError, fmt.Sprintf(msg, args...))
}

// Panic logs at [LevelPanic] and then panics.
func (l *logger) Panic(ctx context.Context, msg string, args ...any) {
l.logAttrs(ctx, LevelPanic, msg, args...)
func (l *logger) Panic(msg string, args ...any) {
l.logAttrs(context.Background(), LevelPanic, msg, args...)
panic(msg)
}

// Panicf logs at LevelPanic and then panics.
// Arguments are handled in the manner of fmt.Printf.
func (l *logger) Panicf(ctx context.Context, msg string, args ...any) {
func (l *logger) Panicf(msg string, args ...any) {
fmsg := fmt.Sprintf(msg, args...)
l.logAttrs(ctx, LevelPanic, fmsg)
l.logAttrs(context.Background(), LevelPanic, fmsg)
panic(fmsg)
}

// PanicContext logs at [LevelPanic] and then panics.
func (l *logger) PanicContext(ctx context.Context, msg string, args ...any) {
l.logAttrs(ctx, LevelPanic, msg, args...)
panic(msg)
}

// Fatal logs at [LevelFatal] and then calls os.Exit(1).
func (l *logger) Fatal(ctx context.Context, msg string, args ...any) {
l.logAttrs(ctx, LevelFatal, msg, args...)
func (l *logger) Fatal(msg string, args ...any) {
l.logAttrs(context.Background(), LevelFatal, msg, args...)
os.Exit(1)
}

// Fatalf logs at LevelFatal and then calls os.Exit(1).
// Arguments are handled in the manner of fmt.Printf.
func (l *logger) Fatalf(ctx context.Context, msg string, args ...any) {
l.logAttrs(ctx, LevelFatal, fmt.Sprintf(msg, args...))
func (l *logger) Fatalf(msg string, args ...any) {
l.logAttrs(context.Background(), LevelFatal, fmt.Sprintf(msg, args...))
os.Exit(1)
}

// FatalContext logs at [LevelFatal] and then calls os.Exit(1).
func (l *logger) FatalContext(ctx context.Context, msg string, args ...any) {
l.logAttrs(ctx, LevelFatal, msg, args...)
os.Exit(1)
}
40 changes: 29 additions & 11 deletions internal/logger/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
type logFunc func(l Logger, msg string, args ...any)

func TestLogger_LevelExtensions(t *testing.T) {
ctx := context.Background()
tests := []struct {
name string
logFunc logFunc
Expand All @@ -20,7 +19,7 @@ func TestLogger_LevelExtensions(t *testing.T) {
{
name: "debug level disabled",
logFunc: func(l Logger, msg string, args ...any) {
l.Debugf(ctx, msg, args...)
l.Debugf(msg, args...)
},
handler: test.MockHandler{
EnabledFunc: func(ctx context.Context, level slog.Level) bool {
Expand All @@ -35,7 +34,7 @@ func TestLogger_LevelExtensions(t *testing.T) {
{
name: "debug level enabled",
logFunc: func(l Logger, msg string, args ...any) {
l.Debugf(ctx, msg, args...)
l.Debugf(msg, args...)
},
handler: test.MockHandler{
HandleFunc: func(ctx context.Context, r slog.Record) error {
Expand All @@ -53,7 +52,7 @@ func TestLogger_LevelExtensions(t *testing.T) {
{
name: "info level disabled",
logFunc: func(l Logger, msg string, args ...any) {
l.Infof(ctx, msg, args...)
l.Infof(msg, args...)
},
handler: test.MockHandler{
EnabledFunc: func(ctx context.Context, level slog.Level) bool {
Expand All @@ -68,7 +67,7 @@ func TestLogger_LevelExtensions(t *testing.T) {
{
name: "info level enabled",
logFunc: func(l Logger, msg string, args ...any) {
l.Infof(ctx, msg, args...)
l.Infof(msg, args...)
},
handler: test.MockHandler{
HandleFunc: func(ctx context.Context, r slog.Record) error {
Expand All @@ -86,7 +85,7 @@ func TestLogger_LevelExtensions(t *testing.T) {
{
name: "warn level disabled",
logFunc: func(l Logger, msg string, args ...any) {
l.Warnf(ctx, msg, args...)
l.Warnf(msg, args...)
},
handler: test.MockHandler{
EnabledFunc: func(ctx context.Context, level slog.Level) bool {
Expand All @@ -101,7 +100,7 @@ func TestLogger_LevelExtensions(t *testing.T) {
{
name: "warn level enabled",
logFunc: func(l Logger, msg string, args ...any) {
l.Warnf(ctx, msg, args...)
l.Warnf(msg, args...)
},
handler: test.MockHandler{
HandleFunc: func(ctx context.Context, r slog.Record) error {
Expand All @@ -119,7 +118,7 @@ func TestLogger_LevelExtensions(t *testing.T) {
{
name: "error level disabled",
logFunc: func(l Logger, msg string, args ...any) {
l.Errorf(ctx, msg, args...)
l.Errorf(msg, args...)
},
handler: test.MockHandler{
EnabledFunc: func(ctx context.Context, level slog.Level) bool {
Expand All @@ -134,7 +133,7 @@ func TestLogger_LevelExtensions(t *testing.T) {
{
name: "error level enabled",
logFunc: func(l Logger, msg string, args ...any) {
l.Errorf(ctx, msg, args...)
l.Errorf(msg, args...)
},
handler: test.MockHandler{
HandleFunc: func(ctx context.Context, r slog.Record) error {
Expand Down Expand Up @@ -170,7 +169,7 @@ func TestLogger_PanicLevel(t *testing.T) {
name: "panic level",
attrs: []any{"key", "value"},
logFunc: func(l Logger, msg string, args ...any) {
l.Panic(ctx, msg, args...)
l.Panic(msg, args...)
},
handler: test.MockHandler{
HandleFunc: func(ctx context.Context, r slog.Record) error {
Expand All @@ -188,7 +187,7 @@ func TestLogger_PanicLevel(t *testing.T) {
{
name: "panicf level",
logFunc: func(l Logger, msg string, args ...any) {
l.Panicf(ctx, msg, args...)
l.Panicf(msg, args...)
},
handler: test.MockHandler{
HandleFunc: func(ctx context.Context, r slog.Record) error {
Expand All @@ -203,6 +202,25 @@ func TestLogger_PanicLevel(t *testing.T) {
},
},
},
{
name: "panic level context",
attrs: []any{"key", "value"},
logFunc: func(l Logger, msg string, args ...any) {
l.PanicContext(ctx, msg, args...)
},
handler: test.MockHandler{
HandleFunc: func(ctx context.Context, r slog.Record) error {
level := LevelPanic
if r.Level != level {
t.Errorf("Expected level to be [%s], got [%s]", getLevelString(level), r.Level)
}
if r.NumAttrs() == 0 {
t.Errorf("Expected attributes, got %d", r.NumAttrs())
}
return nil
},
},
},
}

for _, tt := range tests {
Expand Down
12 changes: 5 additions & 7 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ import (
)

func TestSomeLogging(_ *testing.T) {
ctx := context.Background()
log := logger.NewNamedLogger("Main")
log.Info(ctx, "MainTest")
log.Info("MainTest")
log = logger.NewLogger()
log.Info(ctx, "NoNamedLoggerTest")
log.With("testKey", "testValue").Info(ctx, "WithTest")
log.Info("NoNamedLoggerTest")
log.With("testKey", "testValue").Info("WithTest")
}

func TestIntoContext(t *testing.T) {
Expand All @@ -28,13 +27,12 @@ func TestFromContext(_ *testing.T) {
log := logger.NewNamedLogger("Test")
ctx := logger.IntoContext(context.Background(), log)
loc := logger.FromContext(ctx)
loc.Info(ctx, "Test")
loc.Info("Test")
}

func TestSomeLoggingWithEnv(t *testing.T) {
t.Setenv("LOG_LEVEL", "debug")
t.Setenv("LOG_FORMAT", "text")
ctx := context.Background()
log := logger.NewLogger()
log.Info(ctx, "Test")
log.Info("Test")
}

0 comments on commit f764136

Please sign in to comment.