-
Notifications
You must be signed in to change notification settings - Fork 31
/
logger.format.go
75 lines (65 loc) · 1.82 KB
/
logger.format.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
package main
import (
"fmt"
"github.com/sirupsen/logrus"
)
// LogFormat represents the possible string types that
// the log format can accept
type LogFormat string
// String returns a string representation of the format
func (lf *LogFormat) String() string {
return string(*lf)
}
// Get returns a formatter which logrus can use
func (lf *LogFormat) Get() logrus.Formatter {
switch *lf {
case "json":
return &logrus.JSONFormatter{}
case "production":
return new(productionFormat)
case "raw":
return new(rawFormat)
default:
return &logrus.TextFormatter{
ForceColors: true,
}
}
}
type rawFormat struct{}
func (f *rawFormat) Format(entry *logrus.Entry) ([]byte, error) {
return []byte(entry.Message + "\n"), nil
}
type productionFormat struct{}
var productionFormatColorMap = map[logrus.Level]string{
logrus.TraceLevel: "gray",
logrus.DebugLevel: "gray",
logrus.InfoLevel: "green",
logrus.WarnLevel: "yellow",
logrus.ErrorLevel: "red",
logrus.FatalLevel: "lred",
}
func (f *productionFormat) Format(entry *logrus.Entry) ([]byte, error) {
var moduleLabel string
var submoduleLabel string
timestamp := entry.Time.Format("Jan02/15:04")
data := entry.Data
if data["module"] != nil {
moduleLabel = fmt.Sprintf("%v", data["module"])
}
if data["submodule"] != nil {
submoduleLabel = fmt.Sprintf("/%v", data["submodule"])
}
message := fmt.Sprintf("|%v| [%v%v] %s", timestamp, moduleLabel, submoduleLabel, entry.Message)
if entry.Level > logrus.InfoLevel {
var otherKeys string
for key, value := range data {
if key != "module" && key != "submodule" {
otherKeys = fmt.Sprintf("%s\n %s: %v", otherKeys, key, value)
}
}
message = fmt.Sprintf("%s%s", message, otherKeys)
}
color := productionFormatColorMap[entry.Level]
log := []byte(Color(color, fmt.Sprintf("%s\n", message)))
return log, nil
}