mirror of
https://github.com/maxlerebourg/crowdsec-bouncer-traefik-plugin.git
synced 2026-07-21 19:48:59 +02:00
✨ feat(logs) add supports write logs to files (#217)
* ✨ feat(logs) add supports write logs to files * fix(lint) 🚨 fix go lint * 🐛 fix(bug) check path is done only if provided * 📝 doc(vars) add LogFilePath to vars * 🦺 chore(review) update doc, configuration check and logger
This commit is contained in:
+18
-1
@@ -3,9 +3,11 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Log Logger struct.
|
||||
@@ -16,15 +18,30 @@ type Log struct {
|
||||
}
|
||||
|
||||
// New Set Default log level to info in case log level to defined.
|
||||
func New(logLevel string) *Log {
|
||||
func New(logLevel string, logFilePath 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)
|
||||
// we initialize logger to STDOUT/STDERR first so if the file logger cannot be initialized we can inform the user
|
||||
if logLevel == "DEBUG" {
|
||||
logDebug.SetOutput(os.Stdout)
|
||||
}
|
||||
if logFilePath != "" {
|
||||
logFile, err := os.OpenFile(filepath.Clean(logFilePath), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
|
||||
if err != nil {
|
||||
_ = fmt.Errorf("LogFilePath is not writable %w", err)
|
||||
} else {
|
||||
logInfo.SetOutput(logFile)
|
||||
logError.SetOutput(logFile)
|
||||
if logLevel == "DEBUG" {
|
||||
logDebug.SetOutput(logFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return &Log{
|
||||
logError: logError,
|
||||
logInfo: logInfo,
|
||||
|
||||
Reference in New Issue
Block a user