utils.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package types
  2. import (
  3. "bufio"
  4. "bytes"
  5. "encoding/gob"
  6. "fmt"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "time"
  11. log "github.com/sirupsen/logrus"
  12. "gopkg.in/natefinch/lumberjack.v2"
  13. )
  14. var logFormatter log.Formatter
  15. var LogOutput *lumberjack.Logger //io.Writer
  16. var logLevel log.Level
  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. LogOutput = &lumberjack.Logger{
  37. Filename: filepath.Join(cfgFolder, "crowdsec.log"),
  38. MaxSize: _maxsize,
  39. MaxBackups: _maxfiles,
  40. MaxAge: _maxage,
  41. Compress: _compress,
  42. }
  43. log.SetOutput(LogOutput)
  44. } else if cfgMode != "stdout" {
  45. return fmt.Errorf("log mode '%s' unknown", cfgMode)
  46. }
  47. logLevel = cfgLevel
  48. log.SetLevel(logLevel)
  49. logFormatter = &log.TextFormatter{TimestampFormat: "02-01-2006 15:04:05", FullTimestamp: true, ForceColors: forceColors}
  50. log.SetFormatter(logFormatter)
  51. return nil
  52. }
  53. func ConfigureLogger(clog *log.Logger) error {
  54. /*Configure logs*/
  55. if LogOutput != nil {
  56. clog.SetOutput(LogOutput)
  57. }
  58. if logFormatter != nil {
  59. clog.SetFormatter(logFormatter)
  60. }
  61. clog.SetLevel(logLevel)
  62. return nil
  63. }
  64. func Clone(a, b interface{}) error {
  65. buff := new(bytes.Buffer)
  66. enc := gob.NewEncoder(buff)
  67. dec := gob.NewDecoder(buff)
  68. if err := enc.Encode(a); err != nil {
  69. return fmt.Errorf("failed cloning %T", a)
  70. }
  71. if err := dec.Decode(b); err != nil {
  72. return fmt.Errorf("failed cloning %T", b)
  73. }
  74. return nil
  75. }
  76. func ParseDuration(d string) (time.Duration, error) {
  77. durationStr := d
  78. if strings.HasSuffix(d, "d") {
  79. days := strings.Split(d, "d")[0]
  80. if len(days) == 0 {
  81. return 0, fmt.Errorf("'%s' can't be parsed as duration", d)
  82. }
  83. daysInt, err := strconv.Atoi(days)
  84. if err != nil {
  85. return 0, err
  86. }
  87. durationStr = strconv.Itoa(daysInt*24) + "h"
  88. }
  89. duration, err := time.ParseDuration(durationStr)
  90. if err != nil {
  91. return 0, err
  92. }
  93. return duration, nil
  94. }
  95. func UtcNow() time.Time {
  96. return time.Now().UTC()
  97. }
  98. func GetLineCountForFile(filepath string) int {
  99. f, err := os.Open(filepath)
  100. if err != nil {
  101. log.Fatalf("unable to open log file %s : %s", filepath, err)
  102. }
  103. defer f.Close()
  104. lc := 0
  105. fs := bufio.NewScanner(f)
  106. for fs.Scan() {
  107. lc++
  108. }
  109. return lc
  110. }