-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter.go
71 lines (57 loc) · 1.44 KB
/
formatter.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
package log
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/sirupsen/logrus"
)
const (
defaultLogFormat = "[%lvl%]: %time% - %msg%"
defaultTimestampFormat = "02-Jan-06 15:04:05 MST"
)
type Formatter struct {
TimestampFormat string
LogFormat string
}
func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error) {
output := f.LogFormat
if output == "" {
output = defaultLogFormat
}
timestampFormat := f.TimestampFormat
if timestampFormat == "" {
timestampFormat = defaultTimestampFormat
}
output = strings.Replace(output, "%time%", entry.Time.UTC().Format(timestampFormat), 1)
output = strings.Replace(output, "%msg%", entry.Message, 1)
level := strings.ToUpper(entry.Level.String())
output = strings.Replace(output, "%lvl%", level, 1)
output += getFieldsString(entry.Data)
output += "\n"
return []byte(output), nil
}
func getFieldsString(fields logrus.Fields) string {
var output string
keys := make([]string, 0, len(fields))
for k := range fields {
keys = append(keys, k)
}
sort.Strings(keys)
for _, key := range keys {
val := fields[key]
switch v := val.(type) {
case error:
output += fmt.Sprintf("\t\t%s = %s", key, v.Error())
case string:
output += fmt.Sprintf("\t\t%s = %s", key, v)
case int:
s := strconv.Itoa(v)
output += fmt.Sprintf("\t\t%s = %s", key, s)
case bool:
s := strconv.FormatBool(v)
output += fmt.Sprintf("\t\t%s = %s", key, s)
}
}
return output
}