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

fix: logger initialization before flags parsing #7372

Merged
Show file tree
Hide file tree
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
62 changes: 62 additions & 0 deletions pkg/log/deferred.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package log

import (
"context"
"log/slog"
"slices"
)

func init() {
// Set the default logger so that logs are buffered until the logger is initialized.
slog.SetDefault(New(&DeferredHandler{records: new([]deferredRecord)}))
}

// DeferredHandler is needed to save logs and print them after calling `PrintLogs()` command.
// For example, this may be necessary when the logger is not yet initialized, but messages need to be transmitted.
// In this case, the messages are saved and printed when the logger is initialized.
type DeferredHandler struct {
attrs []slog.Attr

// Shared with all instances of the handler.
// NOTE: non-thread safe
records *[]deferredRecord
}

type deferredRecord struct {
ctx context.Context
slog.Record
}

func (*DeferredHandler) Enabled(context.Context, slog.Level) bool {
return true
}

func (d *DeferredHandler) Handle(ctx context.Context, record slog.Record) error {
record.AddAttrs(d.attrs...)
*d.records = append(*d.records, deferredRecord{
ctx: ctx,
Record: record,
})
return nil
}

func (d *DeferredHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
h := *d
h.attrs = slices.Clip(d.attrs)
h.attrs = append(h.attrs, attrs...)
return &h
}

func (*DeferredHandler) WithGroup(_ string) slog.Handler {
panic("WithGroup is not implemented")
}

func (d *DeferredHandler) Flush(h slog.Handler) {
for _, record := range *d.records {
if !h.Enabled(record.ctx, record.Level) {
continue
}
_ = h.Handle(record.ctx, record.Record)
}
d.records = nil
}
11 changes: 9 additions & 2 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ func New(h slog.Handler) *Logger {
return slog.New(h)
}

// InitLogger initialize the logger variable
// InitLogger initializes the logger variable and flushes the buffered logs if needed.
func InitLogger(debug, disable bool) {
level := lo.Ternary(debug, slog.LevelDebug, slog.LevelInfo)
out := lo.Ternary(disable, io.Discard, io.Writer(os.Stderr))
slog.SetDefault(New(NewHandler(out, &Options{Level: level})))
h := NewHandler(out, &Options{Level: level})

// Flush the buffered logs if needed.
if d, ok := slog.Default().Handler().(*DeferredHandler); ok {
d.Flush(h)
}

slog.SetDefault(New(h))
}

var (
Expand Down