Skip to content

Commit

Permalink
fix slog logger constructor nil pointer bug (#296)
Browse files Browse the repository at this point in the history
* fix slog logger constructor nil pointer bug

* fix(slog): use defaultIntOptions that match the SLoggerOptions defaults
  • Loading branch information
samlaf authored Jul 23, 2024
1 parent 491cecd commit 9bbcdab
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions logging/slog_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,32 @@ type SLoggerOptions struct {
NoColor bool
}

// default SLogger options are used when no options are provided
// they are the development options (debug logs with source)
var defaultTintOptions = tint.Options{
AddSource: false,
Level: slog.LevelInfo,
ReplaceAttr: nil,
TimeFormat: time.StampMilli,
NoColor: false,
}

// NewSlogTextLogger creates a new SLogger with a text handler
// Default behavior is colored log outputs. To disable colors, set opts.NoColor to true.
func NewTextSLogger(outputWriter io.Writer, opts *SLoggerOptions) *SLogger {
tintOptions := &tint.Options{
AddSource: opts.AddSource,
Level: opts.Level,
ReplaceAttr: opts.ReplaceAttr,
TimeFormat: opts.TimeFormat,
NoColor: opts.NoColor,
var tintOptions tint.Options
if opts == nil {
tintOptions = defaultTintOptions
} else {
tintOptions = tint.Options{
AddSource: opts.AddSource,
Level: opts.Level,
ReplaceAttr: opts.ReplaceAttr,
TimeFormat: opts.TimeFormat,
NoColor: opts.NoColor,
}
}
handler := tint.NewHandler(outputWriter, tintOptions)
handler := tint.NewHandler(outputWriter, &tintOptions)
logger := slog.New(handler)
return &SLogger{
logger,
Expand All @@ -63,10 +78,15 @@ func NewTextSLogger(outputWriter io.Writer, opts *SLoggerOptions) *SLogger {
// Currently colors are not supported with json handler. If colors are required,
// use NewTextSLogger instead.
func NewJsonSLogger(outputWriter io.Writer, opts *SLoggerOptions) *SLogger {
handlerOpts := &slog.HandlerOptions{
AddSource: opts.AddSource,
Level: opts.Level,
ReplaceAttr: opts.ReplaceAttr,
var handlerOpts *slog.HandlerOptions
if opts == nil {
handlerOpts = &slog.HandlerOptions{}
} else {
handlerOpts = &slog.HandlerOptions{
AddSource: opts.AddSource,
Level: opts.Level,
ReplaceAttr: opts.ReplaceAttr,
}
}
handler := slog.NewJSONHandler(outputWriter, handlerOpts)
logger := slog.New(handler)
Expand Down

0 comments on commit 9bbcdab

Please sign in to comment.