From b2c61510f1dc57f87ce3cc9a83936207f4710a9f Mon Sep 17 00:00:00 2001 From: marco Date: Thu, 7 Mar 2024 09:28:21 +0100 Subject: [PATCH] crowdsec: avoid writing errors twice when log_media=stdout fix https://github.com/crowdsecurity/crowdsec/pull/2729 for level=fatal --- cmd/crowdsec/hook.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/cmd/crowdsec/hook.go b/cmd/crowdsec/hook.go index 28515d9e474..f21bbf303cc 100644 --- a/cmd/crowdsec/hook.go +++ b/cmd/crowdsec/hook.go @@ -14,18 +14,21 @@ type ConditionalHook struct { } func (hook *ConditionalHook) Fire(entry *log.Entry) error { - if hook.Enabled { - line, err := entry.String() - if err != nil { - return err - } - - _, err = hook.Writer.Write([]byte(line)) + // don't log if the hook is disabled + // or if the level is fatal (the standard logger will handle it) + + if !hook.Enabled || entry.Level == log.FatalLevel { + return nil + } + line, err := entry.String() + if err != nil { return err } - return nil + _, err = hook.Writer.Write([]byte(line)) + + return err } func (hook *ConditionalHook) Levels() []log.Level {