Skip to content

Commit

Permalink
Support logging to the journal with priority "notice" (#763)
Browse files Browse the repository at this point in the history
UDENG-5920
  • Loading branch information
adombeck authored Feb 3, 2025
2 parents 331e79b + bb2fe1e commit a499f29
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
TEXTDOMAIN = "adsys"

// DefaultLogLevel is the default logging level selected without any option.
DefaultLogLevel = log.WarnLevel
DefaultLogLevel = log.NoticeLevel

// DefaultSocketPath is the default socket path.
DefaultSocketPath = "/run/authd.sock"
Expand Down
3 changes: 3 additions & 0 deletions log/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func mapPriority(level Level) journal.Priority {
if level <= InfoLevel {
return journal.PriInfo
}
if level <= NoticeLevel {
return journal.PriNotice
}
if level <= WarnLevel {
return journal.PriWarning
}
Expand Down
30 changes: 24 additions & 6 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const (
ErrorLevel = slog.LevelError
// WarnLevel level. Non-critical entries that deserve eyes.
WarnLevel = slog.LevelWarn
// NoticeLevel level. Normal but significant conditions. Conditions that are not error conditions, but that may
// require special handling. slog doesn't have a Notice level, so we use the average between Info and Warn.
NoticeLevel = (slog.LevelInfo + slog.LevelWarn) / 2
// InfoLevel level. General operational entries about what's going on inside the application.
InfoLevel = slog.LevelInfo
// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
Expand All @@ -43,17 +46,20 @@ func logFuncAdapter(slogFunc func(ctx context.Context, msg string, args ...inter
}

var allLevels = []slog.Level{
slog.LevelDebug,
slog.LevelInfo,
slog.LevelWarn,
slog.LevelError,
DebugLevel,
InfoLevel,
NoticeLevel,
WarnLevel,
ErrorLevel,
}

var defaultHandlers = map[Level]Handler{
DebugLevel: logFuncAdapter(slog.DebugContext),
InfoLevel: logFuncAdapter(slog.InfoContext),
WarnLevel: logFuncAdapter(slog.WarnContext),
ErrorLevel: logFuncAdapter(slog.ErrorContext),
// slog doesn't have a Notice level, so in the default handler, we use Warn instead.
NoticeLevel: logFuncAdapter(slog.WarnContext),
WarnLevel: logFuncAdapter(slog.WarnContext),
ErrorLevel: logFuncAdapter(slog.ErrorContext),
}
var handlers = maps.Clone(defaultHandlers)
var handlersMu = sync.RWMutex{}
Expand Down Expand Up @@ -166,6 +172,18 @@ func Infof(context context.Context, format string, args ...interface{}) {
logf(context, InfoLevel, format, args...)
}

// Notice outputs messages with the level [NoticeLevel] (when that is enabled) using the
// configured logging handler.
func Notice(context context.Context, args ...interface{}) {
log(context, NoticeLevel, args...)
}

// Noticef outputs messages with the level [NoticeLevel] (when that is enabled) using the
// configured logging handler.
func Noticef(context context.Context, format string, args ...interface{}) {
logf(context, NoticeLevel, format, args...)
}

// Warning outputs messages with the level [WarningLevel] (when that is enabled) using the
// configured logging handler.
func Warning(context context.Context, args ...interface{}) {
Expand Down
5 changes: 5 additions & 0 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
var supportedLevels = []log.Level{
log.DebugLevel,
log.InfoLevel,
log.NoticeLevel,
log.WarnLevel,
log.ErrorLevel,
}
Expand All @@ -38,6 +39,8 @@ func callLogHandler(ctx context.Context, level log.Level, args ...any) {
log.Error(ctx, args...)
case log.WarnLevel:
log.Warning(ctx, args...)
case log.NoticeLevel:
log.Notice(ctx, args...)
case log.InfoLevel:
log.Info(ctx, args...)
case log.DebugLevel:
Expand All @@ -51,6 +54,8 @@ func callLogHandlerf(ctx context.Context, level log.Level, format string, args .
log.Errorf(ctx, format, args...)
case log.WarnLevel:
log.Warningf(ctx, format, args...)
case log.NoticeLevel:
log.Noticef(ctx, format, args...)
case log.InfoLevel:
log.Infof(ctx, format, args...)
case log.DebugLevel:
Expand Down

0 comments on commit a499f29

Please sign in to comment.