logging: provide (and use) functions with context information
This commit is contained in:
parent
86d6f2cfcb
commit
7fcf4749ab
4 changed files with 79 additions and 11 deletions
|
@ -9,7 +9,6 @@ import (
|
|||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
"git.sr.ht/~cco/go-scopes/lib/message"
|
||||
"git.sr.ht/~cco/go-scopes/logging"
|
||||
"git.sr.ht/~cco/go-scopes/logging/log"
|
||||
)
|
||||
|
||||
type Cfg struct {
|
||||
|
@ -29,7 +28,7 @@ func start(ctx lib.Context) {
|
|||
cctx := ctx.ChildContext(cfg)
|
||||
cfg.Starter()(cctx)
|
||||
}
|
||||
log.Info().Msg("app.start: running")
|
||||
logging.Info(ctx).Msg("app.start: running")
|
||||
sig := make(chan os.Signal, 1)
|
||||
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
|
||||
for step(ctx, sig) {
|
||||
|
@ -39,12 +38,12 @@ func start(ctx lib.Context) {
|
|||
func step(ctx lib.Context, sig <-chan os.Signal) bool {
|
||||
select {
|
||||
case <-sig:
|
||||
log.Info().Msg("app.step: interrupted")
|
||||
logging.Info(ctx).Msg("app.step: interrupted")
|
||||
return false
|
||||
case msg := <-ctx.Mailbox():
|
||||
//log.Debug().Str("msg.action", msg.Action()).Msg("app.step: message")
|
||||
if msg == message.Quit {
|
||||
log.Info().Msg("app.step: stopped")
|
||||
logging.Info(ctx).Msg("app.step: stopped")
|
||||
return false
|
||||
}
|
||||
return lib.HandleMsg(ctx, msg)
|
||||
|
|
|
@ -3,7 +3,7 @@ package action
|
|||
import (
|
||||
"git.sr.ht/~cco/go-scopes/lib"
|
||||
"git.sr.ht/~cco/go-scopes/lib/message"
|
||||
"git.sr.ht/~cco/go-scopes/logging/log"
|
||||
"git.sr.ht/~cco/go-scopes/logging"
|
||||
)
|
||||
|
||||
type BaseSpec = baseSpec
|
||||
|
@ -33,6 +33,7 @@ func Select(ctx lib.Context, msg lib.Message) []lib.Action {
|
|||
if match(ac, msg) {
|
||||
for _, spec := range ac.Specs() {
|
||||
act := action{ctx, spec, msg}
|
||||
logging.DebugA(&act).Str("pattern", ac.Pattern()).Msg("action.Select")
|
||||
acts = append(acts, &act)
|
||||
}
|
||||
}
|
||||
|
@ -41,8 +42,6 @@ func Select(ctx lib.Context, msg lib.Message) []lib.Action {
|
|||
}
|
||||
|
||||
func match(ac lib.ActionConfig, msg lib.Message) bool {
|
||||
log.Debug().Str("pattern", ac.Pattern()).Str("msg.action", msg.Action()).
|
||||
Msg("action.match")
|
||||
return ac.Pattern() == msg.Action()
|
||||
//return false
|
||||
}
|
||||
|
|
|
@ -7,9 +7,7 @@ import (
|
|||
|
||||
type Evt = zerolog.Event
|
||||
|
||||
func Debug(ctx lib.Context) *Evt {
|
||||
return WithContext(ctx, GetLogger(ctx).Debug())
|
||||
}
|
||||
// logging with context information
|
||||
|
||||
func WithContext(ctx lib.Context, e *Evt) *Evt {
|
||||
if e == nil || ctx == nil {
|
||||
|
@ -17,3 +15,71 @@ func WithContext(ctx lib.Context, e *Evt) *Evt {
|
|||
}
|
||||
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) *Evt {
|
||||
return WithContext(ctx, GetLogger(ctx).Error())
|
||||
}
|
||||
|
||||
// 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) *Evt {
|
||||
return WithMessage(ctx, msg, GetLogger(ctx).Error())
|
||||
}
|
||||
|
||||
// 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) *Evt {
|
||||
return WithAction(act, GetLogger(act.Context()).Error())
|
||||
}
|
||||
|
|
|
@ -80,7 +80,11 @@ func (t *T) LogCount(pr bool) int {
|
|||
srv = data["service"].(string)
|
||||
delete(data, "service")
|
||||
}
|
||||
fmt.Printf("%d: %s: %s - %s; %+v\n", count, level, srv, message, data)
|
||||
dstr := ""
|
||||
if len(data) > 0 {
|
||||
dstr = fmt.Sprintf("; %+v", data)
|
||||
}
|
||||
fmt.Printf("%d: %s: %s - %s%s\n", count, level, srv, message, dstr)
|
||||
//fmt.Println(scanner.Text())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue