Skip to content

Commit

Permalink
Add os.Exit to LogFatal to mimic the zaplogger behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Dec 11, 2023
1 parent dd20b2d commit dbe2565
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ func (c *Component) LogWarnf(template string, args ...interface{}) {
c.Logger().LogWarnf(template, args...)
}

// LogFatal uses fmt.Sprint to construct and log a message.
// LogFatal uses fmt.Sprint to construct and log a message, then calls os.Exit(1).
func (c *Component) LogFatal(msg string, args ...interface{}) {
c.Logger().LogFatal(msg, args...)
}

// LogFatalf uses fmt.Sprintf to log a templated message.
// LogFatalf uses fmt.Sprintf to log a templated message, then calls os.Exit(1).
func (c *Component) LogFatalf(template string, args ...interface{}) {
c.Logger().LogFatalf(template, args...)
}
Expand Down
6 changes: 3 additions & 3 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ type Logger interface {
// LogErrorAttrs emits a log message with the ERROR level and the given attributes.
LogErrorAttrs(msg string, args ...slog.Attr)

// LogFatal emits a log message with the FATAL level.
// LogFatal emits a log message with the FATAL level, then calls os.Exit(1).
LogFatal(msg string, args ...any)

// LogFatalf emits a formatted log message with the FATAL level.
// LogFatalf emits a formatted log message with the FATAL level, then calls os.Exit(1).
LogFatalf(fmtString string, args ...any)

// LogFatalAttrs emits a log message with the FATAL level and the given attributes.
// LogFatalAttrs emits a log message with the FATAL level and the given attributes, then calls os.Exit(1).
LogFatalAttrs(fmtString string, args ...slog.Attr)

// Log emits a log message with the given level.
Expand Down
10 changes: 7 additions & 3 deletions log/logger_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log/slog"
"os"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -192,19 +193,22 @@ func (l *logger) LogErrorAttrs(msg string, args ...slog.Attr) {
l.LogAttrs(msg, LevelError, args...)
}

// LogFatal emits a log message with the FATAL level.
// LogFatal emits a log message with the FATAL level, then calls os.Exit(1).
func (l *logger) LogFatal(msg string, args ...any) {
l.Log(msg, LevelPanic, args...)
os.Exit(1)
}

// LogFatalf emits a formatted log message with the FATAL level.
// LogFatalf emits a formatted log message with the FATAL level, then calls os.Exit(1).
func (l *logger) LogFatalf(fmtString string, args ...any) {
l.Logf(fmtString, LevelPanic, args...)
os.Exit(1)
}

// LogFatalAttrs emits a log message with the FATAL level and the given attributes.
// LogFatalAttrs emits a log message with the FATAL level and the given attributes, then calls os.Exit(1).
func (l *logger) LogFatalAttrs(msg string, args ...slog.Attr) {
l.LogAttrs(msg, LevelPanic, args...)
os.Exit(1)
}

// Log emits a log message with the given level.
Expand Down

0 comments on commit dbe2565

Please sign in to comment.