work in progress: logging: setting up context-dependent events

This commit is contained in:
Helmut Merz 2023-06-28 11:21:28 +02:00
parent ea393bbb58
commit 86d6f2cfcb
3 changed files with 28 additions and 14 deletions

View file

@ -4,11 +4,11 @@ import (
"git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/action"
"git.sr.ht/~cco/go-scopes/logging/log"
"git.sr.ht/~cco/go-scopes/logging"
)
func Start(ctx lib.Context) {
log.Debug().Str("service", ctx.Config().Name()).Msg("core.Start")
logging.Debug(ctx).Msg("core.Start")
lib.RunCtx(ctx, Listen)
}
@ -32,8 +32,7 @@ func Step(ctx lib.Context) (loop bool) {
func HandleMessage(ctx lib.Context, msg lib.Message) (loop bool) {
loop = true
log.Debug().Str("service", ctx.Config().Name()).Str("msg.action", msg.Action()).
Msg("core.HandleMessage")
logging.Debug(ctx).Str("msg.action", msg.Action()).Msg("core.HandleMessage")
for _, act := range action.Select(ctx, msg) {
loop = act.Handle()
}

19
logging/event.go Normal file
View file

@ -0,0 +1,19 @@
package logging
import (
"git.sr.ht/~cco/go-scopes/lib"
"github.com/rs/zerolog"
)
type Evt = zerolog.Event
func Debug(ctx lib.Context) *Evt {
return WithContext(ctx, GetLogger(ctx).Debug())
}
func WithContext(ctx lib.Context, e *Evt) *Evt {
if e == nil || ctx == nil {
return e
}
return e.Str("service", ctx.Config().Name())
}

View file

@ -19,7 +19,12 @@ const (
ErrorLevel = zerolog.ErrorLevel
)
var Levels = map[string]zerolog.Level{}
var Levels = map[string]zerolog.Level{
"debug": DebugLevel,
"info": InfoLevel,
"warn": WarnLevel,
"error": ErrorLevel,
}
type Logger = zerolog.Logger
@ -84,12 +89,3 @@ func Parse(rec string) map[string]interface{} {
}
return buf
}
// set up static data
func init() {
Levels["debug"] = DebugLevel
Levels["info"] = InfoLevel
Levels["warn"] = WarnLevel
Levels["error"] = ErrorLevel
}