-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathformat.go
129 lines (108 loc) · 2.64 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package astilog
import (
"encoding/json"
"fmt"
"log"
"sort"
"strconv"
"strings"
"time"
"github.com/asticode/go-astikit"
)
type formatter interface {
format(msg string, l astikit.LoggerLevel, fs map[string]interface{}) []byte
}
type textFormatter struct {
c Configuration
createdAt time.Time
}
func newTextFormatter(c Configuration, createdAt time.Time) *textFormatter {
return &textFormatter{
c: c,
createdAt: createdAt,
}
}
func (f *textFormatter) format(msg string, l astikit.LoggerLevel, fs map[string]interface{}) (b []byte) {
// Add level
switch l {
case astikit.LoggerLevelDebug:
b = append(b, []byte("DEBUG")...)
case astikit.LoggerLevelWarn:
b = append(b, []byte(" WARN")...)
case astikit.LoggerLevelError:
b = append(b, []byte("ERROR")...)
case astikit.LoggerLevelFatal:
b = append(b, []byte("FATAL")...)
default:
b = append(b, []byte(" INFO")...)
}
// Add timestamp
b = append(b, []byte("[")...)
if f.c.TimestampFormat == "" {
b = append(b, astikit.BytesPad([]byte(strconv.Itoa(int(now().Sub(f.createdAt).Seconds()))), '0', 4)...)
} else {
b = append(b, []byte(now().Format(f.c.TimestampFormat))...)
}
b = append(b, []byte("]")...)
// Add msg
b = append(b, []byte(msg)...)
// Add fields
if len(fs) > 0 {
// Add spaces
b = append(b, []byte(" ")...)
// Sort fields
var vs []string
for k, v := range fs {
vs = append(vs, k+"="+fmt.Sprintf("%v", v))
}
sort.Strings(vs)
b = append(b, []byte(strings.Join(vs, " "))...)
}
// Add newline
b = append(b, newLine...)
return
}
type jsonFormatter struct {
c Configuration
createdAt time.Time
msgKey string
}
func newJSONFormatter(c Configuration, createdAt time.Time) (f *jsonFormatter) {
f = &jsonFormatter{
c: c,
createdAt: createdAt,
msgKey: "msg",
}
if c.MessageKey != "" {
f.msgKey = c.MessageKey
}
return
}
func (f *jsonFormatter) format(msg string, l astikit.LoggerLevel, fs map[string]interface{}) []byte {
// Add msg
fs[f.msgKey] = msg
// Add level
fs["level"] = l
// Add timestamp
if f.c.TimestampFormat == "" {
fs["time"] = int(now().Sub(f.createdAt).Seconds())
} else {
fs["time"] = now().Format(f.c.TimestampFormat)
}
// Marshal
b, err := json.Marshal(fs)
if err != nil {
log.Println(fmt.Errorf("astilog: marshaling failed: %w", err))
return nil
}
// Add newline
b = append(b, newLine...)
return b
}
type minimalistFormatter struct{}
func newMinimalistFormatter() *minimalistFormatter {
return &minimalistFormatter{}
}
func (f *minimalistFormatter) format(msg string, l astikit.LoggerLevel, fs map[string]interface{}) []byte {
return append([]byte(msg), newLine...)
}