85 lines
1.8 KiB
Go
85 lines
1.8 KiB
Go
package logging
|
|
|
|
import (
|
|
lib "git.sr.ht/~cco/go-scopes"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
type Evt = zerolog.Event
|
|
|
|
// logging with context information
|
|
|
|
func WithContext(ctx lib.Context, e *Evt) *Evt {
|
|
if e == nil || ctx == nil {
|
|
return e
|
|
}
|
|
return e.Str("service", ctx.Config().Name())
|
|
}
|
|
|
|
func Debug(ctx lib.Context) *Evt {
|
|
return WithContext(ctx, GetLogger(ctx).Debug())
|
|
}
|
|
|
|
func Info(ctx lib.Context) *Evt {
|
|
return WithContext(ctx, GetLogger(ctx).Info())
|
|
}
|
|
|
|
func Warn(ctx lib.Context) *Evt {
|
|
return WithContext(ctx, GetLogger(ctx).Warn())
|
|
}
|
|
|
|
func Error(ctx lib.Context, err error) *Evt {
|
|
return WithContext(ctx, GetLogger(ctx).Error()).Err(err)
|
|
}
|
|
|
|
// logging for message handlers
|
|
|
|
func WithMessage(ctx lib.Context, msg lib.Message, e *Evt) *Evt {
|
|
if e == nil {
|
|
return e
|
|
}
|
|
return WithContext(ctx, e).Str("action", msg.Action())
|
|
}
|
|
|
|
func DebugM(ctx lib.Context, msg lib.Message) *Evt {
|
|
return WithMessage(ctx, msg, GetLogger(ctx).Debug())
|
|
}
|
|
|
|
func InfoM(ctx lib.Context, msg lib.Message) *Evt {
|
|
return WithMessage(ctx, msg, GetLogger(ctx).Info())
|
|
}
|
|
|
|
func WarnM(ctx lib.Context, msg lib.Message) *Evt {
|
|
return WithMessage(ctx, msg, GetLogger(ctx).Warn())
|
|
}
|
|
|
|
func ErrorM(ctx lib.Context, msg lib.Message, err error) *Evt {
|
|
return WithMessage(ctx, msg, GetLogger(ctx).Error()).Err(err)
|
|
}
|
|
|
|
// logging for action handlers
|
|
|
|
func WithAction(act lib.Action, e *Evt) *Evt {
|
|
if e == nil {
|
|
return e
|
|
}
|
|
ctx := act.Context()
|
|
msg := act.Message()
|
|
return WithMessage(ctx, msg, e)
|
|
}
|
|
|
|
func DebugA(act lib.Action) *Evt {
|
|
return WithAction(act, GetLogger(act.Context()).Debug())
|
|
}
|
|
|
|
func InfoA(act lib.Action) *Evt {
|
|
return WithAction(act, GetLogger(act.Context()).Info())
|
|
}
|
|
|
|
func WarnA(act lib.Action) *Evt {
|
|
return WithAction(act, GetLogger(act.Context()).Warn())
|
|
}
|
|
|
|
func ErrorA(act lib.Action, err error) *Evt {
|
|
return WithAction(act, GetLogger(act.Context()).Error()).Err(err)
|
|
}
|