70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package types
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
|
)
|
|
|
|
var logFormatter log.Formatter
|
|
var LogOutput *lumberjack.Logger //io.Writer
|
|
var logLevel log.Level
|
|
|
|
func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level, maxSize int, maxFiles int, maxAge int, compress *bool, forceColors bool) error {
|
|
/*Configure logs*/
|
|
if cfgMode == "file" {
|
|
_maxsize := 500
|
|
if maxSize != 0 {
|
|
_maxsize = maxSize
|
|
}
|
|
_maxfiles := 3
|
|
if maxFiles != 0 {
|
|
_maxfiles = maxFiles
|
|
}
|
|
_maxage := 28
|
|
if maxAge != 0 {
|
|
_maxage = maxAge
|
|
}
|
|
_compress := true
|
|
if compress != nil {
|
|
_compress = *compress
|
|
}
|
|
|
|
LogOutput = &lumberjack.Logger{
|
|
Filename: filepath.Join(cfgFolder, "crowdsec.log"),
|
|
MaxSize: _maxsize,
|
|
MaxBackups: _maxfiles,
|
|
MaxAge: _maxage,
|
|
Compress: _compress,
|
|
}
|
|
log.SetOutput(LogOutput)
|
|
} else if cfgMode != "stdout" {
|
|
return fmt.Errorf("log mode '%s' unknown", cfgMode)
|
|
}
|
|
logLevel = cfgLevel
|
|
log.SetLevel(logLevel)
|
|
logFormatter = &log.TextFormatter{TimestampFormat: "2006-01-02 15:04:05", FullTimestamp: true, ForceColors: forceColors}
|
|
log.SetFormatter(logFormatter)
|
|
return nil
|
|
}
|
|
|
|
func ConfigureLogger(clog *log.Logger) error {
|
|
/*Configure logs*/
|
|
if LogOutput != nil {
|
|
clog.SetOutput(LogOutput)
|
|
}
|
|
|
|
if logFormatter != nil {
|
|
clog.SetFormatter(logFormatter)
|
|
}
|
|
clog.SetLevel(logLevel)
|
|
return nil
|
|
}
|
|
|
|
func UtcNow() time.Time {
|
|
return time.Now().UTC()
|
|
}
|