-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: logger initialization before flags parsing (#7372)
Signed-off-by: knqyf263 <knqyf263@gmail.com> Co-authored-by: knqyf263 <knqyf263@gmail.com>
- Loading branch information
1 parent
feaef96
commit c929290
Showing
2 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters