-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bin/crowdsec: avoid writing errors twice when log_media=stdout (#2729)
* bin/crowdsec: avoid writing errors twice when log_media=stdout * lint
- Loading branch information
Showing
3 changed files
with
62 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"os" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type ConditionalHook struct { | ||
Writer io.Writer | ||
LogLevels []log.Level | ||
Enabled bool | ||
} | ||
|
||
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)) | ||
|
||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (hook *ConditionalHook) Levels() []log.Level { | ||
return hook.LogLevels | ||
} | ||
|
||
// The primal logging hook is set up before parsing config.yaml. | ||
// Once config.yaml is parsed, the primal hook is disabled if the | ||
// configured logger is writing to stderr. Otherwise it's used to | ||
// report fatal errors and panics to stderr in addition to the log file. | ||
var primalHook = &ConditionalHook{ | ||
Writer: os.Stderr, | ||
LogLevels: []log.Level{log.FatalLevel, log.PanicLevel}, | ||
Enabled: true, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters