Skip to content

Commit

Permalink
Merge pull request #12 from Roshick/handleroptions
Browse files Browse the repository at this point in the history
Secure logging.Logger() from panicking...
  • Loading branch information
Roshick authored Dec 13, 2023
2 parents 7b77eda + efee6f1 commit 8dc07fc
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ type Logging struct {
}

func New() *Logging {
return &Logging{
logger: slog.Default(), // ensure non-nil
}
return &Logging{}
}

func (l *Logging) WithLogger(logger *slog.Logger) *Logging {
Expand All @@ -32,34 +30,29 @@ func (l *Logging) WithLogger(logger *slog.Logger) *Logging {
}

func (l *Logging) Logger() *slog.Logger {
return l.logger
}

func (l *Logging) Ctx(ctx context.Context) auloggingapi.ContextAwareLoggingImplementation {
currentLogger := FromContext(ctx)
if currentLogger == nil {
currentLogger = l.logger
}
currentLogger := l.logger
if currentLogger == nil {
currentLogger = slog.Default()
}
if currentLogger == nil {
currentLogger = slog.New(noophandler.New())
}
return currentLogger
}

func (l *Logging) Ctx(ctx context.Context) auloggingapi.ContextAwareLoggingImplementation {
currentLogger := FromContext(ctx)
if currentLogger == nil {
currentLogger = l.Logger()
}
return &withContext{
Logging: l.WithLogger(currentLogger),
ctx: ctx,
}
}

func (l *Logging) NoCtx() auloggingapi.ContextAwareLoggingImplementation {
currentLogger := l.logger
if currentLogger == nil {
currentLogger = slog.Default()
}
if currentLogger == nil {
currentLogger = slog.New(noophandler.New())
}
currentLogger := l.Logger()
return &withContext{
Logging: l.WithLogger(currentLogger),
ctx: nil,
Expand Down

0 comments on commit 8dc07fc

Please sign in to comment.