2020-05-15 09:39:16 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2022-03-29 12:20:26 +00:00
|
|
|
"bufio"
|
2020-06-10 10:14:27 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2020-05-27 09:51:24 +00:00
|
|
|
"fmt"
|
2020-11-30 09:37:17 +00:00
|
|
|
"os"
|
2023-06-08 13:49:37 +00:00
|
|
|
"path/filepath"
|
2020-11-30 09:37:17 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"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
|
|
|
|
|
2022-06-17 14:12:49 +00:00
|
|
|
func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level, 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)
|
2022-06-17 14:12:49 +00:00
|
|
|
logFormatter = &log.TextFormatter{TimestampFormat: "02-01-2006 15:04:05", 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
|
|
|
|
|
|
|
func Clone(a, b interface{}) error {
|
|
|
|
buff := new(bytes.Buffer)
|
|
|
|
enc := gob.NewEncoder(buff)
|
|
|
|
dec := gob.NewDecoder(buff)
|
|
|
|
if err := enc.Encode(a); err != nil {
|
|
|
|
return fmt.Errorf("failed cloning %T", a)
|
|
|
|
}
|
|
|
|
if err := dec.Decode(b); err != nil {
|
|
|
|
return fmt.Errorf("failed cloning %T", b)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-11-30 09:37:17 +00:00
|
|
|
|
|
|
|
func ParseDuration(d string) (time.Duration, error) {
|
|
|
|
durationStr := d
|
|
|
|
if strings.HasSuffix(d, "d") {
|
|
|
|
days := strings.Split(d, "d")[0]
|
|
|
|
if len(days) == 0 {
|
|
|
|
return 0, fmt.Errorf("'%s' can't be parsed as duration", d)
|
|
|
|
}
|
|
|
|
daysInt, err := strconv.Atoi(days)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
durationStr = strconv.Itoa(daysInt*24) + "h"
|
|
|
|
}
|
|
|
|
duration, err := time.ParseDuration(durationStr)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return duration, nil
|
|
|
|
}
|
|
|
|
|
2022-01-19 13:56:05 +00:00
|
|
|
func UtcNow() time.Time {
|
|
|
|
return time.Now().UTC()
|
|
|
|
}
|
2022-03-29 12:20:26 +00:00
|
|
|
|
|
|
|
func GetLineCountForFile(filepath string) int {
|
|
|
|
f, err := os.Open(filepath)
|
|
|
|
if err != nil {
|
2022-04-19 17:12:23 +00:00
|
|
|
log.Fatalf("unable to open log file %s : %s", filepath, err)
|
2022-03-29 12:20:26 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
lc := 0
|
|
|
|
fs := bufio.NewScanner(f)
|
|
|
|
for fs.Scan() {
|
|
|
|
lc++
|
|
|
|
}
|
|
|
|
return lc
|
|
|
|
}
|