-
Notifications
You must be signed in to change notification settings - Fork 19
/
logsetup.go
56 lines (46 loc) · 1.36 KB
/
logsetup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"io"
"os"
"path/filepath"
)
var lumberjackLogger *lumberjack.Logger
func SetupLogger(logLevel log.Level, fileLoggingEnabled bool, maxSize, maxAge, maxBackups int, compress bool) {
lumberjackLogger = &lumberjack.Logger{
// Log file absolute path, os agnostic
Filename: filepath.ToSlash("logs/golbat.log"),
MaxSize: maxSize, // MB
MaxBackups: maxBackups,
MaxAge: maxAge, // days
Compress: compress, // disabled by default
}
var output io.Writer
if fileLoggingEnabled {
// Fork writing into two outputs
output = io.MultiWriter(os.Stdout, lumberjackLogger)
} else {
output = os.Stdout
}
logFormatter := new(PlainFormatter)
logFormatter.TimestampFormat = "2006-01-02 15:04:05"
logFormatter.LevelDesc = []string{"PANC", "FATL", "ERRO", "WARN", "INFO", "DEBG"}
log.SetFormatter(logFormatter)
log.SetLevel(logLevel)
log.SetOutput(output)
}
func RotateLogs() {
if lumberjackLogger != nil {
_ = lumberjackLogger.Rotate()
}
}
type PlainFormatter struct {
TimestampFormat string
LevelDesc []string
}
func (f *PlainFormatter) Format(entry *log.Entry) ([]byte, error) {
timestamp := fmt.Sprintf(entry.Time.Format(f.TimestampFormat))
return []byte(fmt.Sprintf("%s %s %s\n", f.LevelDesc[entry.Level], timestamp, entry.Message)), nil
}