-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.go
83 lines (75 loc) · 1.79 KB
/
logging.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package logging
import (
"encoding/json"
"io"
"os"
"path/filepath"
"reflect"
log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)
type Formatter struct {
log.TextFormatter
}
func MakeFormatter() (formatter *Formatter) {
formatter = new(Formatter)
formatter.TextFormatter = log.TextFormatter{
ForceColors: true,
DisableTimestamp: false,
FullTimestamp: true,
TimestampFormat: "2006-01-02T15:04:05",
}
return
}
// Whole this thing is needed to convert fields that are structs to json format
func (f *Formatter) Format(entry *log.Entry) ([]byte, error) {
for k, v := range entry.Data {
// skip error field
if k == "error" {
continue
}
val := reflect.ValueOf(v)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() == reflect.Struct {
json, _ := json.Marshal(v)
entry.Data[k] = string(json)
}
}
return f.TextFormatter.Format(entry)
}
// PanicLevel Level = iota
// // FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
// // logging level is set to Panic.
// FatalLevel
func levelFromString(l string) log.Level {
switch l {
case "trace":
return log.TraceLevel
case "debug":
return log.DebugLevel
case "info":
return log.InfoLevel
case "warn":
return log.WarnLevel
case "error":
return log.ErrorLevel
case "fatal":
return log.FatalLevel
default:
log.WithField("level", l).Fatal("Incorrect log level passed. Could be: [trace, debug, info, warn, error, fatal]")
}
return log.InfoLevel
}
func Config(path, level string) {
mw := io.MultiWriter(os.Stdout, &lumberjack.Logger{
Filename: filepath.Join(path, "main.log"),
MaxSize: 100, // megabytes
MaxBackups: 5,
MaxAge: 2, //days
})
log.SetOutput(mw)
log.SetLevel(levelFromString(level))
log.SetFormatter(MakeFormatter())
}