Add a logger instance to bouncer instance (#134)

*  Add a logger instance to bouncer instance

* 🍱 fix test

* 🍱 fix lint

* 🍱 fix lint

* 🍱 fix lint

* 🍱 fix lint

* 🍱 fix lint

* 🍱 fix test

* 🍱 fix test

* 🍱 fix lint + test

* 🍱 fix test

* 🍱 fix test

* 🍱 fix test

* 🍱 fix lint

* 🍱 fix lint
This commit is contained in:
maxlerebourg
2024-02-11 11:52:47 +01:00
committed by GitHub
parent 6c183d9231
commit 575d3a02e5
7 changed files with 106 additions and 85 deletions
+25 -16
View File
@@ -8,32 +8,41 @@ import (
"os"
)
var (
loggerInfo = log.New(io.Discard, "INFO: CrowdsecBouncerTraefikPlugin: ", log.Ldate|log.Ltime) //nolint:gochecknoglobals
loggerDebug = log.New(io.Discard, "DEBUG: CrowdsecBouncerTraefikPlugin: ", log.Ldate|log.Ltime) //nolint:gochecknoglobals
loggerError = log.New(io.Discard, "ERROR: CrowdsecBouncerTraefikPlugin: ", log.Ldate|log.Ltime) //nolint:gochecknoglobals
)
// Log Logger struct.
type Log struct {
logError *log.Logger
logInfo *log.Logger
logDebug *log.Logger
}
// Init Set Default log level to info in case log level to defined.
func Init(logLevel string) {
loggerError.SetOutput(os.Stderr)
loggerInfo.SetOutput(os.Stdout)
// New Set Default log level to info in case log level to defined.
func New(logLevel string) *Log {
logError := log.New(io.Discard, "ERROR: CrowdsecBouncerTraefikPlugin: ", log.Ldate|log.Ltime)
logInfo := log.New(io.Discard, "INFO: CrowdsecBouncerTraefikPlugin: ", log.Ldate|log.Ltime)
logDebug := log.New(io.Discard, "DEBUG: CrowdsecBouncerTraefikPlugin: ", log.Ldate|log.Ltime)
logError.SetOutput(os.Stderr)
logInfo.SetOutput(os.Stdout)
if logLevel == "DEBUG" {
loggerDebug.SetOutput(os.Stdout)
logDebug.SetOutput(os.Stdout)
}
return &Log{
logError: logError,
logInfo: logInfo,
logDebug: logDebug,
}
}
// Info log to Stdout.
func Info(str string) {
loggerInfo.Printf(str)
func (l *Log) Info(str string) {
l.logInfo.Printf(str)
}
// Debug log to Stdout.
func Debug(str string) {
loggerDebug.Printf(str)
func (l *Log) Debug(str string) {
l.logDebug.Printf(str)
}
// Error log to Stderr.
func Error(str string) {
loggerError.Printf(str)
func (l *Log) Error(str string) {
l.logError.Printf(str)
}