logging config: +level; avoid import cycle

This commit is contained in:
Helmut Merz 2023-06-27 08:55:26 +02:00
parent 8dad97f6be
commit e063bb1b03
4 changed files with 27 additions and 14 deletions

View file

@ -5,7 +5,6 @@ import (
"strings"
"git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/core"
)
type Cfg struct {
@ -40,16 +39,10 @@ func (cfg *base) Starter() lib.StartProc {
}
func (cfg *base) Step() lib.StepProc {
if cfg.step == nil {
return core.Step
}
return cfg.step
}
func (cfg *base) MessageHandler() lib.MessageHandler {
if cfg.msgHandler == nil {
return core.HandleMessage
}
return cfg.msgHandler
}
@ -86,12 +79,18 @@ func (cfg *base) AddAction(pattern string, specs ...lib.ActionSpec) lib.Config {
func Base(name string, starter lib.StartProc) *base {
return &base{
name: name,
starter: starter,
actions: nil,
name: name,
starter: starter,
step: Step,
msgHandler: MsgHandler,
actions: nil,
}
}
// will be set by core.init()
var Step lib.StepProc
var MsgHandler lib.MessageHandler
// action configuration
type action struct {

View file

@ -5,6 +5,7 @@ import (
"git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/message"
"git.sr.ht/~cco/go-scopes/logging/log"
)
type BaseSpec = baseSpec
@ -43,6 +44,8 @@ func Select(ctx lib.Context, msg lib.Message) []lib.Action {
func match(ac lib.ActionConfig, msg lib.Message) bool {
fmt.Println("action.match", ac.Pattern(), msg.Action())
log.Debug().Str("pattern", ac.Pattern()).Str("action", msg.Action()).
Msg("action.match")
return ac.Pattern() == msg.Action()
//return false
}

View file

@ -3,6 +3,7 @@ package core
import (
"fmt"
"git.sr.ht/~cco/go-scopes/config"
"git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/action"
)
@ -38,3 +39,8 @@ func HandleMessage(ctx lib.Context, msg lib.Message) (loop bool) {
}
return
}
func init() {
config.Step = Step // avoid import cycle
config.MsgHandler = HandleMessage
}

View file

@ -53,10 +53,15 @@ func New(cfg *Cfg, home string) (*Logger, error) {
GetLogger(nil).Err(err).Msg("logging.New: open log file")
return nil, err
}
lc := zerolog.New(f).With().Timestamp()
// TODO: evaluate cfg.Level
// lc.Level(level)
logger := lc.Logger()
logger := zerolog.New(f).With().Timestamp().Logger()
if cfg.Level != "" {
if lvl, ok := Levels[cfg.Level]; ok {
logger = logger.Level(lvl)
} else {
GetLogger(nil).Warn().Str("level", cfg.Level).
Msg("logging.New: undefined level")
}
}
return &logger, nil
}