1
0
Fork 0
mirror of https://github.com/chainguard-dev/clog synced 2026-07-06 22:12:46 +00:00

add Fatal{Context}{f} (#8)

This adds `clog.Fatal{Context}{f}`, and
`clog.FromContext(ctx).Fatal{Context}{f}`

`Fatal` logs at `LevelError` then calls `os.Exit(1)`, like stdlib
`log.Fatal`:
https://cs.opensource.google/go/go/+/refs/tags/go1.21.6:src/log/log.go;l=413

---------

Signed-off-by: Jason Hall <jason@chainguard.dev>
Co-authored-by: Billy Lynch <1844673+wlynch@users.noreply.github.com>
This commit is contained in:
Jason Hall 2024-02-06 11:03:48 -05:00 committed by GitHub
parent 83244dc767
commit 4d1a9405b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 50 additions and 0 deletions

25
log.go
View file

@ -3,6 +3,7 @@ package clog
import ( import (
"context" "context"
"log/slog" "log/slog"
"os"
) )
// Info calls Info on the default logger. // Info calls Info on the default logger.
@ -89,3 +90,27 @@ func Debugf(format string, args ...any) {
func DebugContextf(ctx context.Context, format string, args ...any) { func DebugContextf(ctx context.Context, format string, args ...any) {
wrapf(ctx, FromContext(ctx), slog.LevelDebug, format, args...) 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)
}

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"log/slog" "log/slog"
"os"
"runtime" "runtime"
"time" "time"
) )
@ -86,6 +87,30 @@ func (l *Logger) DebugContextf(ctx context.Context, format string, args ...any)
wrapf(ctx, l, slog.LevelDebug, format, args...) 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, args ...any) {
wrap(context.Background(), l, slog.LevelError, msg, args...)
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, args ...any) {
wrap(ctx, l, slog.LevelError, msg, args...)
os.Exit(1)
}
// Base returns the underlying [slog.Logger]. // Base returns the underlying [slog.Logger].
func (l *Logger) Base() *slog.Logger { func (l *Logger) Base() *slog.Logger {
return &l.Logger return &l.Logger