|
1 | | -//go:generate go tool aliaspkg -docs=decls -include Fatal,Fatalf,Fatalln,Panic,Panicf,Panicln,Print,Printf,Println |
| 1 | +//go:generate go tool aliaspkg -docs=decls -include Panic,Panicf,Panicln,Print,Printf,Println |
2 | 2 |
|
3 | 3 | package log |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "fmt" |
6 | 7 | "log/slog" |
| 8 | + "os" |
7 | 9 | "sync" |
8 | 10 |
|
9 | 11 | "go.chrisrx.dev/x/env" |
| 12 | + "go.chrisrx.dev/x/errors" |
| 13 | + "go.chrisrx.dev/x/slices" |
| 14 | + "go.chrisrx.dev/x/strings" |
10 | 15 | ) |
11 | 16 |
|
12 | 17 | // New constructs a new [*slog.Logger] with the provided options. |
@@ -37,3 +42,80 @@ func SetDefault() { |
37 | 42 | slog.Debug("default slog configured from environment", slog.Any("options", opts)) |
38 | 43 | }) |
39 | 44 | } |
| 45 | + |
| 46 | +// Fatal is similar to [log.Fatal], but uses the default [slog.Logger]. It is |
| 47 | +// meant to be used at the end of an error chain only. |
| 48 | +// |
| 49 | +// If GO_BACKTRACE=1 environment variable is set, any arguments containing a |
| 50 | +// stack trace will be printed directly to stderr. Only the first argument |
| 51 | +// containing a stack trace will be printed. |
| 52 | +func Fatal(v ...any) { |
| 53 | + msg := fmt.Sprint(v...) |
| 54 | + if isBacktraceEnabled() { |
| 55 | + printBacktrace(msg, v) |
| 56 | + } |
| 57 | + slog.Error(msg) |
| 58 | + os.Exit(1) |
| 59 | +} |
| 60 | + |
| 61 | +// Fatalf is similar to [log.Fatalf], but uses the default [slog.Logger]. It is |
| 62 | +// meant to be used at the end of an error chain only. |
| 63 | +// |
| 64 | +// If GO_BACKTRACE=1 environment variable is set, any arguments containing a |
| 65 | +// stack trace will be printed directly to stderr. Only the first argument |
| 66 | +// containing a stack trace will be printed. |
| 67 | +func Fatalf(format string, v ...any) { |
| 68 | + msg := fmt.Sprintf(format, v...) |
| 69 | + if isBacktraceEnabled() { |
| 70 | + printBacktrace(msg, v) |
| 71 | + } |
| 72 | + slog.Error(msg) |
| 73 | + os.Exit(1) |
| 74 | +} |
| 75 | + |
| 76 | +// Fatalln is similar to [log.Fatalln], but uses the default [slog.Logger]. It |
| 77 | +// is meant to be used at the end of an error chain only. |
| 78 | +// |
| 79 | +// If GO_BACKTRACE=1 environment variable is set, any arguments containing a |
| 80 | +// stack trace will be printed directly to stderr. Only the first argument |
| 81 | +// containing a stack trace will be printed. |
| 82 | +func Fatalln(v ...any) { |
| 83 | + msg := fmt.Sprintln(v...) |
| 84 | + if isBacktraceEnabled() { |
| 85 | + printBacktrace(msg, v) |
| 86 | + } |
| 87 | + slog.Error(msg) |
| 88 | + os.Exit(1) |
| 89 | +} |
| 90 | + |
| 91 | +const backtraceEnvVar = "GO_BACKTRACE" |
| 92 | + |
| 93 | +func isBacktraceEnabled() bool { |
| 94 | + if value, ok := os.LookupEnv(backtraceEnvVar); ok && value == "1" { |
| 95 | + return true |
| 96 | + } |
| 97 | + return false |
| 98 | +} |
| 99 | + |
| 100 | +// checkBacktrace checks if any arguments implement [errors.StackError] and |
| 101 | +// prints the full stack trace to stderr. Arguments are checked in order and if |
| 102 | +// multiple stack traces are provided, only the first is printed. |
| 103 | +// |
| 104 | +// This behavior is opt-in, requiring environment variable GO_BACKTRACE=1 to be |
| 105 | +// set. |
| 106 | +func printBacktrace(msg string, args []any) { |
| 107 | + if len(args) == 0 { |
| 108 | + return |
| 109 | + } |
| 110 | + for _, arg := range args { |
| 111 | + if err, ok := arg.(error); ok { |
| 112 | + if err, ok := errors.As[errors.StackError](err); ok { |
| 113 | + if v, ok := os.LookupEnv(backtraceEnvVar); ok && v == "1" { |
| 114 | + slog.Error(msg) |
| 115 | + fmt.Fprintln(os.Stderr, strings.Join(slices.Map(err.Trace(), strings.ToString), "\n\t")) |
| 116 | + os.Exit(1) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments