blob: 49ec92f172c73780baee1ef38934fb8da3fc2ce7 [file] [log] [blame]
Jonathan Hartf86817b2018-08-17 10:35:54 -07001package logrus
2
3import (
4 "bytes"
5 "fmt"
Matteo Scandolo1f49bf52018-11-20 13:56:45 -08006 "os"
Jonathan Hartf86817b2018-08-17 10:35:54 -07007 "sort"
8 "strings"
Matteo Scandolo1f49bf52018-11-20 13:56:45 -08009 "sync"
Jonathan Hartf86817b2018-08-17 10:35:54 -070010 "time"
11)
12
13const (
14 nocolor = 0
15 red = 31
16 green = 32
17 yellow = 33
Matteo Scandolo1f49bf52018-11-20 13:56:45 -080018 blue = 36
Jonathan Hartf86817b2018-08-17 10:35:54 -070019 gray = 37
20)
21
22var (
23 baseTimestamp time.Time
Matteo Scandolo1f49bf52018-11-20 13:56:45 -080024 emptyFieldMap FieldMap
Jonathan Hartf86817b2018-08-17 10:35:54 -070025)
26
27func init() {
28 baseTimestamp = time.Now()
Jonathan Hartf86817b2018-08-17 10:35:54 -070029}
30
Matteo Scandolo1f49bf52018-11-20 13:56:45 -080031// TextFormatter formats logs into text
Jonathan Hartf86817b2018-08-17 10:35:54 -070032type TextFormatter struct {
33 // Set to true to bypass checking for a TTY before outputting colors.
34 ForceColors bool
35
36 // Force disabling colors.
37 DisableColors bool
38
Matteo Scandolo1f49bf52018-11-20 13:56:45 -080039 // Override coloring based on CLICOLOR and CLICOLOR_FORCE. - https://bixense.com/clicolors/
40 EnvironmentOverrideColors bool
41
Jonathan Hartf86817b2018-08-17 10:35:54 -070042 // Disable timestamp logging. useful when output is redirected to logging
43 // system that already adds timestamps.
44 DisableTimestamp bool
45
46 // Enable logging the full timestamp when a TTY is attached instead of just
47 // the time passed since beginning of execution.
48 FullTimestamp bool
49
50 // TimestampFormat to use for display when a full timestamp is printed
51 TimestampFormat string
52
53 // The fields are sorted by default for a consistent output. For applications
54 // that log extremely frequently and don't use the JSON formatter this may not
55 // be desired.
56 DisableSorting bool
Matteo Scandolo1f49bf52018-11-20 13:56:45 -080057
58 // The keys sorting function, when uninitialized it uses sort.Strings.
59 SortingFunc func([]string)
60
61 // Disables the truncation of the level text to 4 characters.
62 DisableLevelTruncation bool
63
64 // QuoteEmptyFields will wrap empty fields in quotes if true
65 QuoteEmptyFields bool
66
67 // Whether the logger's out is to a terminal
68 isTerminal bool
69
70 // FieldMap allows users to customize the names of keys for default fields.
71 // As an example:
72 // formatter := &TextFormatter{
73 // FieldMap: FieldMap{
74 // FieldKeyTime: "@timestamp",
75 // FieldKeyLevel: "@level",
76 // FieldKeyMsg: "@message"}}
77 FieldMap FieldMap
78
79 terminalInitOnce sync.Once
Jonathan Hartf86817b2018-08-17 10:35:54 -070080}
81
Matteo Scandolo1f49bf52018-11-20 13:56:45 -080082func (f *TextFormatter) init(entry *Entry) {
83 if entry.Logger != nil {
84 f.isTerminal = checkIfTerminal(entry.Logger.Out)
85
86 if f.isTerminal {
87 initTerminal(entry.Logger.Out)
88 }
89 }
90}
91
92func (f *TextFormatter) isColored() bool {
93 isColored := f.ForceColors || f.isTerminal
94
95 if f.EnvironmentOverrideColors {
96 if force, ok := os.LookupEnv("CLICOLOR_FORCE"); ok && force != "0" {
97 isColored = true
98 } else if ok && force == "0" {
99 isColored = false
100 } else if os.Getenv("CLICOLOR") == "0" {
101 isColored = false
102 }
103 }
104
105 return isColored && !f.DisableColors
106}
107
108// Format renders a single log entry
Jonathan Hartf86817b2018-08-17 10:35:54 -0700109func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800110 prefixFieldClashes(entry.Data, f.FieldMap, entry.HasCaller())
111
Jonathan Hartf86817b2018-08-17 10:35:54 -0700112 keys := make([]string, 0, len(entry.Data))
113 for k := range entry.Data {
114 keys = append(keys, k)
115 }
116
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800117 fixedKeys := make([]string, 0, 4+len(entry.Data))
118 if !f.DisableTimestamp {
119 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyTime))
Jonathan Hartf86817b2018-08-17 10:35:54 -0700120 }
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800121 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLevel))
122 if entry.Message != "" {
123 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyMsg))
124 }
125 if entry.err != "" {
126 fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLogrusError))
127 }
128 if entry.HasCaller() {
129 fixedKeys = append(fixedKeys,
130 f.FieldMap.resolve(FieldKeyFunc), f.FieldMap.resolve(FieldKeyFile))
131 }
132
133 if !f.DisableSorting {
134 if f.SortingFunc == nil {
135 sort.Strings(keys)
136 fixedKeys = append(fixedKeys, keys...)
137 } else {
138 if !f.isColored() {
139 fixedKeys = append(fixedKeys, keys...)
140 f.SortingFunc(fixedKeys)
141 } else {
142 f.SortingFunc(keys)
143 }
144 }
145 } else {
146 fixedKeys = append(fixedKeys, keys...)
147 }
148
149 var b *bytes.Buffer
Jonathan Hartf86817b2018-08-17 10:35:54 -0700150 if entry.Buffer != nil {
151 b = entry.Buffer
152 } else {
153 b = &bytes.Buffer{}
154 }
155
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800156 f.terminalInitOnce.Do(func() { f.init(entry) })
Jonathan Hartf86817b2018-08-17 10:35:54 -0700157
158 timestampFormat := f.TimestampFormat
159 if timestampFormat == "" {
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800160 timestampFormat = defaultTimestampFormat
Jonathan Hartf86817b2018-08-17 10:35:54 -0700161 }
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800162 if f.isColored() {
Jonathan Hartf86817b2018-08-17 10:35:54 -0700163 f.printColored(b, entry, keys, timestampFormat)
164 } else {
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800165 for _, key := range fixedKeys {
166 var value interface{}
167 switch {
168 case key == f.FieldMap.resolve(FieldKeyTime):
169 value = entry.Time.Format(timestampFormat)
170 case key == f.FieldMap.resolve(FieldKeyLevel):
171 value = entry.Level.String()
172 case key == f.FieldMap.resolve(FieldKeyMsg):
173 value = entry.Message
174 case key == f.FieldMap.resolve(FieldKeyLogrusError):
175 value = entry.err
176 case key == f.FieldMap.resolve(FieldKeyFunc) && entry.HasCaller():
177 value = entry.Caller.Function
178 case key == f.FieldMap.resolve(FieldKeyFile) && entry.HasCaller():
179 value = fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
180 default:
181 value = entry.Data[key]
182 }
183 f.appendKeyValue(b, key, value)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700184 }
185 }
186
187 b.WriteByte('\n')
188 return b.Bytes(), nil
189}
190
191func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string, timestampFormat string) {
192 var levelColor int
193 switch entry.Level {
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800194 case DebugLevel, TraceLevel:
Jonathan Hartf86817b2018-08-17 10:35:54 -0700195 levelColor = gray
196 case WarnLevel:
197 levelColor = yellow
198 case ErrorLevel, FatalLevel, PanicLevel:
199 levelColor = red
200 default:
201 levelColor = blue
202 }
203
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800204 levelText := strings.ToUpper(entry.Level.String())
205 if !f.DisableLevelTruncation {
206 levelText = levelText[0:4]
207 }
208
209 // Remove a single newline if it already exists in the message to keep
210 // the behavior of logrus text_formatter the same as the stdlib log package
211 entry.Message = strings.TrimSuffix(entry.Message, "\n")
212
213 caller := ""
214
215 if entry.HasCaller() {
216 caller = fmt.Sprintf("%s:%d %s()",
217 entry.Caller.File, entry.Caller.Line, entry.Caller.Function)
218 }
Jonathan Hartf86817b2018-08-17 10:35:54 -0700219
220 if f.DisableTimestamp {
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800221 fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m%s %-44s ", levelColor, levelText, caller, entry.Message)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700222 } else if !f.FullTimestamp {
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800223 fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d]%s %-44s ", levelColor, levelText, int(entry.Time.Sub(baseTimestamp)/time.Second), caller, entry.Message)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700224 } else {
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800225 fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s]%s %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), caller, entry.Message)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700226 }
227 for _, k := range keys {
228 v := entry.Data[k]
229 fmt.Fprintf(b, " \x1b[%dm%s\x1b[0m=", levelColor, k)
230 f.appendValue(b, v)
231 }
232}
233
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800234func (f *TextFormatter) needsQuoting(text string) bool {
235 if f.QuoteEmptyFields && len(text) == 0 {
236 return true
237 }
Jonathan Hartf86817b2018-08-17 10:35:54 -0700238 for _, ch := range text {
239 if !((ch >= 'a' && ch <= 'z') ||
240 (ch >= 'A' && ch <= 'Z') ||
241 (ch >= '0' && ch <= '9') ||
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800242 ch == '-' || ch == '.' || ch == '_' || ch == '/' || ch == '@' || ch == '^' || ch == '+') {
Jonathan Hartf86817b2018-08-17 10:35:54 -0700243 return true
244 }
245 }
246 return false
247}
248
249func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interface{}) {
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800250 if b.Len() > 0 {
251 b.WriteByte(' ')
252 }
Jonathan Hartf86817b2018-08-17 10:35:54 -0700253 b.WriteString(key)
254 b.WriteByte('=')
255 f.appendValue(b, value)
Jonathan Hartf86817b2018-08-17 10:35:54 -0700256}
257
258func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) {
Matteo Scandolo1f49bf52018-11-20 13:56:45 -0800259 stringVal, ok := value.(string)
260 if !ok {
261 stringVal = fmt.Sprint(value)
262 }
263
264 if !f.needsQuoting(stringVal) {
265 b.WriteString(stringVal)
266 } else {
267 b.WriteString(fmt.Sprintf("%q", stringVal))
Jonathan Hartf86817b2018-08-17 10:35:54 -0700268 }
269}