From 71cd89a4f1b5d99a48dd02d4e87d1e461b5c94dc Mon Sep 17 00:00:00 2001 From: Gibheer Date: Tue, 5 Sep 2023 15:23:43 +0200 Subject: add debug log support for the checker This makes it possible to use this library and get a debug checker output. --- monzero.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/monzero.go b/monzero.go index f2481a5..0e9fa76 100644 --- a/monzero.go +++ b/monzero.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "fmt" + "log/slog" "time" ) @@ -19,6 +20,7 @@ type ( executor func(Check, context.Context) CheckResult timeout time.Duration ident string // the host identifier + logger *slog.Logger } CheckerConfig struct { @@ -42,6 +44,9 @@ type ( // HostIdentifier is used in notifications to point to the source of the // notification. HostIdentifier string + + // Checker will send debug details to the logger for each command executed. + Logger *slog.Logger } // Check is contains the metadata to run a check and its current state. @@ -75,6 +80,7 @@ func NewChecker(cfg CheckerConfig) (*Checker, error) { executor: cfg.Executor, timeout: cfg.Timeout, ident: cfg.HostIdentifier, + logger: cfg.Logger, } if c.executor == nil { return nil, fmt.Errorf("executor must not be nil") @@ -116,6 +122,13 @@ func (c *Checker) Next() error { result.Message = fmt.Sprintf("check took longer than %s", c.timeout) result.ExitCode = 2 } + c.logger.Debug( + "check command run", + "id", check.id, + "command", check.Command, + "exit code", result.ExitCode, + "message", result.Message, + ) backToOkay := false if len(check.ExitCodes) == 0 && result.ExitCode == 0 { -- cgit v1.2.3-70-g09d2