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

add Fatal{Context}{f} #8

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package clog
import (
"context"
"log/slog"
"os"
)

// Info calls Info on the default logger.
Expand Down Expand Up @@ -89,3 +90,27 @@ func Debugf(format string, args ...any) {
func DebugContextf(ctx context.Context, format string, args ...any) {
wrapf(ctx, FromContext(ctx), slog.LevelDebug, format, args...)
}

// Fatal calls Error on the default logger, then exits.
func Fatal(msg string, args ...any) {
wrap(context.Background(), DefaultLogger(), slog.LevelError, msg, args...)
os.Exit(1)
}

// FatalContext calls ErrorContext on the context logger, then exits.
func FatalContext(ctx context.Context, msg string, args ...any) {
wrap(ctx, FromContext(ctx), slog.LevelError, msg, args...)
os.Exit(1)
}

// Fatalf calls Errorf on the default logger, then exits.
func Fatalf(format string, args ...any) {
wrapf(context.Background(), DefaultLogger(), slog.LevelError, format, args...)
os.Exit(1)
}

// FatalContextf calls ErrorContextf on the context logger, then exits.
func FatalContextf(ctx context.Context, format string, args ...any) {
wrapf(ctx, FromContext(ctx), slog.LevelError, format, args...)
os.Exit(1)
}
25 changes: 25 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log/slog"
"os"
"runtime"
"time"
)
Expand Down Expand Up @@ -86,6 +87,30 @@ func (l *Logger) DebugContextf(ctx context.Context, format string, args ...any)
wrapf(ctx, l, slog.LevelDebug, format, args...)
}

// Fatalf logs at LevelError with the given format and arguments, then exits.
func (l *Logger) Fatalf(format string, args ...any) {
wrapf(context.Background(), l, slog.LevelError, format, args...)
os.Exit(1)
}

// Fatal logs at LevelError with the given message, then exits.
func (l *Logger) Fatal(msg string) {
wrapf(context.Background(), l, slog.LevelError, msg)
imjasonh marked this conversation as resolved.
Show resolved Hide resolved
os.Exit(1)
}

// FatalfContextf logs at LevelError with the given context, format and arguments, then exits.
func (l *Logger) FatalContextf(ctx context.Context, format string, args ...any) {
wrapf(ctx, l, slog.LevelError, format, args...)
os.Exit(1)
}

// FatalfContext logs at LevelError with the given context and message, then exits.
func (l *Logger) FatalContext(ctx context.Context, msg string) {
wrapf(ctx, l, slog.LevelError, msg)
imjasonh marked this conversation as resolved.
Show resolved Hide resolved
os.Exit(1)
}

// Base returns the underlying [slog.Logger].
func (l *Logger) Base() *slog.Logger {
return &l.Logger
Expand Down
Loading