mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-17 23:03:49 +01:00
refactor(logging): add non-blocking writer for high-volume logging
Replace synchronous log writing with zerolog's diode-based non-blocking writer to prevent logging from blocking the main application during log bursts. The diode writer buffers up to 1024 messages and logs a warning when messages are dropped. - Extract multi-writer logic into separate `multiWriter` function - Wrap with `diode.NewWriter` for async buffering - Update both `NewLogger` and `NewLoggerWithFixedLevel` to use diode
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/yusing/godoxy/internal/common"
|
||||
|
||||
"github.com/rs/zerolog/diode"
|
||||
zerologlog "github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
@@ -68,7 +69,13 @@ func fmtMessage(msg string) string {
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func multiLevelWriter(out ...io.Writer) io.Writer {
|
||||
func diodeMultiWriter(out ...io.Writer) io.Writer {
|
||||
return diode.NewWriter(multiWriter(out...), 1024, 0, func(missed int) {
|
||||
zerologlog.Warn().Int("missed", missed).Msg("missed log messages")
|
||||
})
|
||||
}
|
||||
|
||||
func multiWriter(out ...io.Writer) io.Writer {
|
||||
if len(out) == 0 {
|
||||
return os.Stdout
|
||||
}
|
||||
@@ -80,7 +87,7 @@ func multiLevelWriter(out ...io.Writer) io.Writer {
|
||||
|
||||
func NewLogger(out ...io.Writer) zerolog.Logger {
|
||||
writer := zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
|
||||
w.Out = multiLevelWriter(out...)
|
||||
w.Out = diodeMultiWriter(out...)
|
||||
w.TimeFormat = timeFmt
|
||||
w.FormatMessage = func(msgI any) string { // pad spaces for each line
|
||||
if msgI == nil {
|
||||
@@ -94,7 +101,7 @@ func NewLogger(out ...io.Writer) zerolog.Logger {
|
||||
|
||||
func NewLoggerWithFixedLevel(lvl zerolog.Level, out ...io.Writer) zerolog.Logger {
|
||||
writer := zerolog.NewConsoleWriter(func(w *zerolog.ConsoleWriter) {
|
||||
w.Out = multiLevelWriter(out...)
|
||||
w.Out = diodeMultiWriter(out...)
|
||||
w.TimeFormat = timeFmt
|
||||
w.FormatMessage = func(msgI any) string { // pad spaces for each line
|
||||
if msgI == nil {
|
||||
|
||||
Reference in New Issue
Block a user