forked from gomods/athens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.go
69 lines (60 loc) · 1.41 KB
/
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
package log
import (
"bytes"
"fmt"
"sort"
"strings"
"time"
"github.com/fatih/color"
"github.com/sirupsen/logrus"
)
func getGCPFormatter() logrus.Formatter {
return &logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyLevel: "severity",
logrus.FieldKeyMsg: "message",
logrus.FieldKeyTime: "timestamp",
},
}
}
func getDevFormatter() logrus.Formatter {
return devFormatter{}
}
type devFormatter struct{}
const lightGrey = 0xffccc
func (devFormatter) Format(e *logrus.Entry) ([]byte, error) {
var buf bytes.Buffer
var sprintf func(format string, a ...interface{}) string
switch e.Level {
case logrus.DebugLevel:
sprintf = color.New(lightGrey).Sprintf
case logrus.WarnLevel:
sprintf = color.YellowString
case logrus.ErrorLevel:
sprintf = color.RedString
default:
sprintf = color.CyanString
}
lvl := strings.ToUpper(e.Level.String())
buf.WriteString(sprintf(lvl))
buf.WriteString("[" + e.Time.Format(time.Kitchen) + "]")
buf.WriteString(": ")
buf.WriteString(e.Message)
buf.WriteByte('\t')
for _, k := range sortFields(e.Data) {
fmt.Fprintf(&buf, "%s=%s ", color.MagentaString(k), e.Data[k])
}
buf.WriteByte('\n')
return buf.Bytes(), nil
}
func sortFields(data logrus.Fields) []string {
keys := []string{}
for k := range data {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func getDefaultFormatter() logrus.Formatter {
return &logrus.JSONFormatter{}
}