github.com/crowdsecurity/crowdsec@v1.6.1/pkg/types/utils.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  	"time"
     8  
     9  	log "github.com/sirupsen/logrus"
    10  	"gopkg.in/natefinch/lumberjack.v2"
    11  )
    12  
    13  var logFormatter log.Formatter
    14  var LogOutput *lumberjack.Logger //io.Writer
    15  var logLevel log.Level
    16  
    17  func SetDefaultLoggerConfig(cfgMode string, cfgFolder string, cfgLevel log.Level, maxSize int, maxFiles int, maxAge int, compress *bool, forceColors bool) error {
    18  	/*Configure logs*/
    19  	if cfgMode == "file" {
    20  		_maxsize := 500
    21  		if maxSize != 0 {
    22  			_maxsize = maxSize
    23  		}
    24  		_maxfiles := 3
    25  		if maxFiles != 0 {
    26  			_maxfiles = maxFiles
    27  		}
    28  		_maxage := 28
    29  		if maxAge != 0 {
    30  			_maxage = maxAge
    31  		}
    32  		_compress := true
    33  		if compress != nil {
    34  			_compress = *compress
    35  		}
    36  
    37  		LogOutput = &lumberjack.Logger{
    38  			Filename:   filepath.Join(cfgFolder, "crowdsec.log"),
    39  			MaxSize:    _maxsize,
    40  			MaxBackups: _maxfiles,
    41  			MaxAge:     _maxage,
    42  			Compress:   _compress,
    43  		}
    44  		log.SetOutput(LogOutput)
    45  	} else if cfgMode != "stdout" {
    46  		return fmt.Errorf("log mode '%s' unknown", cfgMode)
    47  	}
    48  	logLevel = cfgLevel
    49  	log.SetLevel(logLevel)
    50  	logFormatter = &log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true, ForceColors: forceColors}
    51  	log.SetFormatter(logFormatter)
    52  	return nil
    53  }
    54  
    55  func ConfigureLogger(clog *log.Logger) error {
    56  	/*Configure logs*/
    57  	if LogOutput != nil {
    58  		clog.SetOutput(LogOutput)
    59  	}
    60  
    61  	if logFormatter != nil {
    62  		clog.SetFormatter(logFormatter)
    63  	}
    64  	clog.SetLevel(logLevel)
    65  	return nil
    66  }
    67  
    68  func UtcNow() time.Time {
    69  	return time.Now().UTC()
    70  }
    71  
    72  func IsNetworkFS(path string) (bool, string, error) {
    73  	fsType, err := GetFSType(path)
    74  	if err != nil {
    75  		return false, "", err
    76  	}
    77  	fsType = strings.ToLower(fsType)
    78  	return fsType == "nfs" || fsType == "cifs" || fsType == "smb" || fsType == "smb2", fsType, nil
    79  }