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:
parent
83244dc767
commit
4d1a9405b3
2 changed files with 50 additions and 0 deletions
25
log.go
25
log.go
|
|
@ -3,6 +3,7 @@ package clog
|
|||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
// 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) {
|
||||
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
logger.go
25
logger.go
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -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, 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].
|
||||
func (l *Logger) Base() *slog.Logger {
|
||||
return &l.Logger
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue