Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: true up logger comments and minor refactors #3215

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,6 @@ func ConfigDefault() Config {

// New takes a Config and returns a validated logger.
func New(cfg Config) (*slog.Logger, error) {
var handler slog.Handler
opts := slog.HandlerOptions{}

// Use default destination if none
if cfg.Destination == nil {
cfg.Destination = DestinationDefault
Expand All @@ -167,8 +164,12 @@ func New(cfg Config) (*slog.Logger, error) {
if !validLevels[cfg.Level] {
return nil, fmt.Errorf("unsupported log level: %d", cfg.Level)
}
opts.Level = slog.Level(cfg.Level)

opts := slog.HandlerOptions{
Level: slog.Level(cfg.Level),
}

var handler slog.Handler
switch cfg.Format.ToLower() {
case FormatText:
handler = console.NewHandler(cfg.Destination, &console.HandlerOptions{
Expand All @@ -195,14 +196,13 @@ func New(cfg Config) (*slog.Logger, error) {
return nil, fmt.Errorf("unsupported log format: %s", cfg.Format)
}

log := slog.New(handler)
return log, nil
return slog.New(handler), nil
}

// ctxKey provides a location to store a logger in a context.
type ctxKey struct{}

// defaultCtxKey provides a default key if one is not passed into From.
// defaultCtxKey provides a key instance to get a logger from context
var defaultCtxKey = ctxKey{}

// WithContext takes a context.Context and a *slog.Logger, storing it on the key
Expand Down Expand Up @@ -239,12 +239,12 @@ func Enabled(ctx context.Context) bool {
func From(ctx context.Context) *slog.Logger {
// Check that we have a ctx
if ctx == nil {
return newEmpty()
return newDiscard()
}
// Grab value from key
log := ctx.Value(defaultCtxKey)
if log == nil {
return newEmpty()
return newDiscard()
}

// Ensure our value is a *slog.Logger before we cast.
Expand All @@ -258,7 +258,7 @@ func From(ctx context.Context) *slog.Logger {
}

// newDiscard returns a logger without any settings that goes to io.Discard
func newEmpty() *slog.Logger {
func newDiscard() *slog.Logger {
h := slog.NewTextHandler(DestinationNone, &slog.HandlerOptions{})
return slog.New(h)
}
Expand Down