-
Notifications
You must be signed in to change notification settings - Fork 0
/
logmessage.go
208 lines (179 loc) · 4.53 KB
/
logmessage.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package log
import (
"encoding/json"
"fmt"
"runtime"
"strings"
"time"
"unicode"
"github.com/ttacon/chalk"
)
type logMessage struct {
Timestamp string `json:"timestamp"`
Level string `json:"level"`
Tags []string `json:"tags"`
Message string `json:"message"`
Metadata interface{} `json:"metadata,omitempty"`
File string `json:"file,omitempty"`
format Format
rawLevel Level
colorize bool
logCaller bool
trimmedLeft string
trimmedRight string
}
// newLogMessage creates a new logMessage.
func newLogMessage(
format Format,
colorize bool,
logCaller bool,
callerSkip int,
t time.Time,
timeFormat TimeFormat,
level Level,
tags []string,
message string,
data interface{},
) *logMessage {
trimmedLeft := leadingWhitespace(message)
trimmedRight := trailingWhitespace(message)
modifiedMessage := strings.TrimSpace(message)
caller := ""
if logCaller {
var file string
var line int
var ok bool
_, file, line, ok = runtime.Caller(callerSkip + 1)
if ok {
fileComponents := strings.Split(file, "/")
if len(fileComponents) > 1 {
file = fileComponents[len(fileComponents)-1]
}
} else {
file = "???"
line = 0
}
caller = fmt.Sprintf("%s:%d", file, line)
}
// Make sure that error interface types in logMessage.Metadata are not
// marshalled as empty JSON objects
metadata := data
if e, ok := data.(error); ok {
if _, ok := e.(json.Marshaler); !ok {
metadata = e.Error()
}
}
// If logMessage.Metadata is a slice, convert make the same conversion for any
// errors in the slice
if s, ok := data.([]interface{}); ok {
for i, v := range s {
if e, ok := v.(error); ok {
if _, ok := e.(json.Marshaler); !ok {
s[i] = e.Error()
}
}
}
}
var timestamp string
if timeFormat == TimeFormatLoggly {
// According to loggly documentation:
// * The only timestamp format accepted is ISO 8601 (e.g., 2013-10-11T22:14:15.003Z).
// * Loggly supports microseconds/seconds fraction up to 6 digits, per the spec in RFC5424.
timestamp = t.UTC().Format("2006-01-02T15:04:05.000000Z07:00")
} else {
timestamp = t.String()
}
formattedMessage := &logMessage{
Timestamp: timestamp,
Level: level.String(),
Tags: tags,
Message: modifiedMessage,
Metadata: metadata,
File: caller,
format: format,
rawLevel: level,
colorize: colorize,
logCaller: logCaller,
trimmedLeft: trimmedLeft,
trimmedRight: trimmedRight,
}
return formattedMessage
}
// MARK: Methods
func (m logMessage) restoreWhitespace(output string) string {
return m.trimmedLeft + output + m.trimmedRight
}
func (m logMessage) colorizeIfNeeded(output string) string {
if !m.colorize {
return output
}
lines := strings.Split(output, "\n")
for i, line := range lines {
lines[i] = fmt.Sprintf("%s%s%s", m.rawLevel.color(), line, chalk.ResetColor)
}
return strings.Join(lines, "\n")
}
func (m logMessage) jsonString() string {
s, _ := json.Marshal(m)
return m.colorizeIfNeeded(string(s))
}
// MARK: String interface methods
func (m logMessage) String() string {
if m.format == LogFormatJSON {
return m.jsonString()
}
var timeAndLevel, logCaller, tags, data string
timeAndLevel = fmt.Sprintf("%s [%s] ", m.Timestamp, m.Level)
if m.logCaller {
logCaller = fmt.Sprintf("[%s] ", m.File)
}
if len(m.Tags) > 0 {
tags = fmt.Sprintf("(%s) ", strings.Join(m.Tags, ","))
}
if m.Metadata != nil {
switch m.Metadata.(type) {
case string:
data = m.Metadata.(string)
case fmt.Stringer:
data = m.Metadata.(fmt.Stringer).String()
case error:
data = m.Metadata.(error).Error()
default:
data = fmt.Sprintf("%#v", m.Metadata)
}
}
prettyMessage := timeAndLevel + logCaller + tags +
m.trimmedLeft + m.Message + " " + data + m.trimmedRight
return m.colorizeIfNeeded(prettyMessage)
}
// MARK: Helper Functions
// return the leading whitespace of the input string
func leadingWhitespace(s string) string {
var b strings.Builder
for i, _ := range s {
if !unicode.IsSpace(rune(s[i])) {
return b.String()
}
b.Write([]byte{s[i]})
}
return b.String()
}
// return the trailing whitespace of the input string
func trailingWhitespace(s string) string {
var rev strings.Builder
for i := len(s); i > 0; i-- {
if !unicode.IsSpace(rune(s[i-1])) {
return reverseString(rev.String())
}
rev.Write([]byte{s[i-1]})
}
return rev.String()
}
// reverse a string
func reverseString(s string) string {
r := []rune(s)
for i, j := 0, 0; i < j; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}