Skip to content

Commit

Permalink
reintroduce atomic package default
Browse files Browse the repository at this point in the history
Signed-off-by: Kit Patella <kit@defenseunicorns.com>
  • Loading branch information
mkcp committed Oct 17, 2024
1 parent 9b72fc6 commit 1e88bd4
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ import (
"log/slog"
"os"
"strings"
"sync/atomic"
)

var defaultLogger atomic.Pointer[slog.Logger]

// init sets a logger with default config when the package is initialized.
func init() {
l, _ := New(ConfigDefault()) //nolint:errcheck
SetDefault(l)
}

// Level declares each supported log level. These are 1:1 what log/slog supports by default. Info is the default level.
type Level int

Expand Down Expand Up @@ -136,12 +145,15 @@ func New(cfg Config) (*slog.Logger, error) {
return log, nil
}

// Default gets a logger from the atomic slog default.
// Default retrieves a logger from the package default. This is intended as a fallback when a logger cannot easily be
// passed in as a dependency, like when developing a new function. Use it like you would use context.TODO().
func Default() *slog.Logger {
return slog.Default()
return defaultLogger.Load()
}

// SetDefault takes a logger and sets it as the atomic slog default.
// SetDefault takes a logger and atomically stores it as the package default. This is intended to be called when the
// application starts to override the default config with application-specific config. See Default() for more usage
// details.
func SetDefault(l *slog.Logger) {
slog.SetDefault(l)
defaultLogger.Store(l)
}

0 comments on commit 1e88bd4

Please sign in to comment.