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" "strings"
"git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/core"
) )
type Cfg struct { type Cfg struct {
@ -40,16 +39,10 @@ func (cfg *base) Starter() lib.StartProc {
} }
func (cfg *base) Step() lib.StepProc { func (cfg *base) Step() lib.StepProc {
if cfg.step == nil {
return core.Step
}
return cfg.step return cfg.step
} }
func (cfg *base) MessageHandler() lib.MessageHandler { func (cfg *base) MessageHandler() lib.MessageHandler {
if cfg.msgHandler == nil {
return core.HandleMessage
}
return cfg.msgHandler return cfg.msgHandler
} }
@ -88,10 +81,16 @@ func Base(name string, starter lib.StartProc) *base {
return &base{ return &base{
name: name, name: name,
starter: starter, starter: starter,
step: Step,
msgHandler: MsgHandler,
actions: nil, actions: nil,
} }
} }
// will be set by core.init()
var Step lib.StepProc
var MsgHandler lib.MessageHandler
// action configuration // action configuration
type action struct { type action struct {

View file

@ -5,6 +5,7 @@ import (
"git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/message" "git.sr.ht/~cco/go-scopes/lib/message"
"git.sr.ht/~cco/go-scopes/logging/log"
) )
type BaseSpec = baseSpec 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 { func match(ac lib.ActionConfig, msg lib.Message) bool {
fmt.Println("action.match", ac.Pattern(), msg.Action()) 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 ac.Pattern() == msg.Action()
//return false //return false
} }

View file

@ -3,6 +3,7 @@ package core
import ( import (
"fmt" "fmt"
"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"
) )
@ -38,3 +39,8 @@ func HandleMessage(ctx lib.Context, msg lib.Message) (loop bool) {
} }
return 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") GetLogger(nil).Err(err).Msg("logging.New: open log file")
return nil, err return nil, err
} }
lc := zerolog.New(f).With().Timestamp() logger := zerolog.New(f).With().Timestamp().Logger()
// TODO: evaluate cfg.Level if cfg.Level != "" {
// lc.Level(level) if lvl, ok := Levels[cfg.Level]; ok {
logger := lc.Logger() logger = logger.Level(lvl)
} else {
GetLogger(nil).Warn().Str("level", cfg.Level).
Msg("logging.New: undefined level")
}
}
return &logger, nil return &logger, nil
} }