Files
trbot/utils/task/zerolog.go
Hubert Chen 314cdc0772 add github.com/reugn/go-quartz as task scheduler
task:
    new package that wraps `github.com/reugn/go-quartz` for managing tasks
    encapsulates `zerolog.Logger` to implement the `github.com/reugn/go-quartz/logger` interface
yaml:
    change auto save goroutine as a task
database:
    add a save database task without trigger
internal_plugin:
    add two task without trigger to save and reload plugins databases
2025-11-10 02:42:12 +08:00

92 lines
2.0 KiB
Go

package task
import (
"fmt"
"github.com/rs/zerolog"
)
// FormatKeyValue adds every two args to the event by calling `zerolog.Event().Str()`.
func FormatKeyValue(event *zerolog.Event, args ...any) *zerolog.Event {
var key, value string
for index, arg := range args {
if index % 2 == 0 {
key = fmt.Sprint(arg)
} else {
value = fmt.Sprint(arg)
event = event.Str(key, value)
}
}
return event
}
// NewZerologWapped wraps `zerolog.Logger` to implements the github.com/reugn/go-quartz/logger interface.
//
// https://pkg.go.dev/github.com/reugn/go-quartz/logger#Logger
func NewZerologWappred(zerolog zerolog.Logger) *ZerologWappred {
return &ZerologWappred{
zerolog: zerolog.With().
Str("package", "go-quartz").
Logger(),
}
}
type ZerologWappred struct {
zerolog zerolog.Logger
}
func (z *ZerologWappred) Trace(msg string, args ...any) {
if len(args) == 0 {
z.zerolog.Trace().Msg(msg)
return
} else if len(args) % 2 == 0 {
FormatKeyValue(z.zerolog.Trace(), args...).Msg(msg)
} else {
z.zerolog.Trace().Msgf(msg, args...)
}
}
func (z *ZerologWappred) Debug(msg string, args ...any) {
if len(args) == 0 {
z.zerolog.Debug().Msg(msg)
return
} else if len(args)%2 == 0 {
FormatKeyValue(z.zerolog.Debug(), args...).Msg(msg)
} else {
z.zerolog.Debug().Msgf(msg, args...)
}
}
func (z *ZerologWappred) Info(msg string, args ...any) {
if len(args) == 0 {
z.zerolog.Info().Msg(msg)
return
} else if len(args)%2 == 0 {
FormatKeyValue(z.zerolog.Info(), args...).Msg(msg)
} else {
z.zerolog.Info().Msgf(msg, args...)
}
}
func (z *ZerologWappred) Warn(msg string, args ...any) {
if len(args) == 0 {
z.zerolog.Warn().Msg(msg)
return
} else if len(args)%2 == 0 {
FormatKeyValue(z.zerolog.Warn(), args...).Msg(msg)
} else {
z.zerolog.Warn().Msgf(msg, args...)
}
}
func (z *ZerologWappred) Error(msg string, args ...any) {
if len(args) == 0 {
z.zerolog.Error().Msg(msg)
return
} else if len(args)%2 == 0 {
FormatKeyValue(z.zerolog.Error(), args...).Msg(msg)
} else {
z.zerolog.Error().Msgf(msg, args...)
}
}