2020-05-15 09:39:16 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2020-05-27 09:51:24 +00:00
|
|
|
"fmt"
|
2023-06-08 13:49:37 +00:00
|
|
|
"path/filepath"
|
2024-03-07 13:04:50 +00:00
|
|
|
"strings"
|
2020-11-30 09:37:17 +00:00
|
|
|
"time"
|
2020-05-27 09:51:24 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
2020-05-15 09:39:16 +00:00
|
|
|
)
|
|
|
|
|
2020-05-27 10:37:18 +00:00
|
|
|
var logFormatter log.Formatter
|
2020-06-19 11:57:44 +00:00
|
|
|
var LogOutput *lumberjack.Logger //io.Writer
|
2020-05-27 09:51:24 +00:00
|
|
|
var logLevel log.Level
|
|
|
|
|
2024-04-14 18:28:34 +00:00
|
|
|
func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level, LogFormat string, maxSize int, maxFiles int, maxAge int, compress *bool, forceColors bool) error {
|
2020-05-27 09:51:24 +00:00
|
|
|
/*Configure logs*/
|
2020-05-27 10:37:18 +00:00
|
|
|
if cfgMode == "file" {
|
2021-12-28 10:59:03 +00:00
|
|
|
_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
|
|
|
|
}
|
|
|
|
|
2020-06-19 11:57:44 +00:00
|
|
|
LogOutput = &lumberjack.Logger{
|
2023-06-07 10:58:35 +00:00
|
|
|
Filename: filepath.Join(cfgFolder, "crowdsec.log"),
|
2021-12-28 10:59:03 +00:00
|
|
|
MaxSize: _maxsize,
|
|
|
|
MaxBackups: _maxfiles,
|
|
|
|
MaxAge: _maxage,
|
|
|
|
Compress: _compress,
|
2020-05-27 10:37:18 +00:00
|
|
|
}
|
2020-06-19 11:57:44 +00:00
|
|
|
log.SetOutput(LogOutput)
|
2020-05-27 10:37:18 +00:00
|
|
|
} else if cfgMode != "stdout" {
|
|
|
|
return fmt.Errorf("log mode '%s' unknown", cfgMode)
|
2020-05-27 09:51:24 +00:00
|
|
|
}
|
2020-05-27 10:37:18 +00:00
|
|
|
logLevel = cfgLevel
|
2020-05-27 09:51:24 +00:00
|
|
|
log.SetLevel(logLevel)
|
2024-04-14 18:27:34 +00:00
|
|
|
|
|
|
|
if LogFormat == 'JSON' {
|
|
|
|
logFormatter = &log.JSONFormatter{}
|
|
|
|
} else {
|
|
|
|
logFormatter = &log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors}
|
|
|
|
}
|
|
|
|
|
2021-02-25 10:20:36 +00:00
|
|
|
log.SetFormatter(logFormatter)
|
2020-05-27 09:51:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConfigureLogger(clog *log.Logger) error {
|
|
|
|
/*Configure logs*/
|
2020-06-19 11:57:44 +00:00
|
|
|
if LogOutput != nil {
|
|
|
|
clog.SetOutput(LogOutput)
|
2020-05-27 09:51:24 +00:00
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
2020-05-27 10:45:16 +00:00
|
|
|
if logFormatter != nil {
|
|
|
|
clog.SetFormatter(logFormatter)
|
|
|
|
}
|
2020-05-27 10:37:18 +00:00
|
|
|
clog.SetLevel(logLevel)
|
2020-05-27 09:51:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-06-10 10:14:27 +00:00
|
|
|
|
2022-01-19 13:56:05 +00:00
|
|
|
func UtcNow() time.Time {
|
|
|
|
return time.Now().UTC()
|
|
|
|
}
|
2024-03-07 13:04:50 +00:00
|
|
|
|
|
|
|
func IsNetworkFS(path string) (bool, string, error) {
|
|
|
|
fsType, err := GetFSType(path)
|
|
|
|
if err != nil {
|
|
|
|
return false, "", err
|
|
|
|
}
|
|
|
|
fsType = strings.ToLower(fsType)
|
|
|
|
return fsType == "nfs" || fsType == "cifs" || fsType == "smb" || fsType == "smb2", fsType, nil
|
|
|
|
}
|