work in progress: logging: setting up context-dependent events
This commit is contained in:
parent
ea393bbb58
commit
86d6f2cfcb
3 changed files with 28 additions and 14 deletions
|
@ -4,11 +4,11 @@ import (
|
||||||
"git.sr.ht/~cco/go-scopes/config"
|
"git.sr.ht/~cco/go-scopes/config"
|
||||||
"git.sr.ht/~cco/go-scopes/lib"
|
"git.sr.ht/~cco/go-scopes/lib"
|
||||||
"git.sr.ht/~cco/go-scopes/lib/action"
|
"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) {
|
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)
|
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) {
|
func HandleMessage(ctx lib.Context, msg lib.Message) (loop bool) {
|
||||||
loop = true
|
loop = true
|
||||||
log.Debug().Str("service", ctx.Config().Name()).Str("msg.action", msg.Action()).
|
logging.Debug(ctx).Str("msg.action", msg.Action()).Msg("core.HandleMessage")
|
||||||
Msg("core.HandleMessage")
|
|
||||||
for _, act := range action.Select(ctx, msg) {
|
for _, act := range action.Select(ctx, msg) {
|
||||||
loop = act.Handle()
|
loop = act.Handle()
|
||||||
}
|
}
|
||||||
|
|
19
logging/event.go
Normal file
19
logging/event.go
Normal 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())
|
||||||
|
}
|
|
@ -19,7 +19,12 @@ const (
|
||||||
ErrorLevel = zerolog.ErrorLevel
|
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
|
type Logger = zerolog.Logger
|
||||||
|
|
||||||
|
@ -84,12 +89,3 @@ func Parse(rec string) map[string]interface{} {
|
||||||
}
|
}
|
||||||
return buf
|
return buf
|
||||||
}
|
}
|
||||||
|
|
||||||
// set up static data
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
Levels["debug"] = DebugLevel
|
|
||||||
Levels["info"] = InfoLevel
|
|
||||||
Levels["warn"] = WarnLevel
|
|
||||||
Levels["error"] = ErrorLevel
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue