Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 1 | package logrus |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 6 | "os" |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 7 | "sort" |
| 8 | "strings" |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 9 | "sync" |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | const ( |
| 14 | nocolor = 0 |
| 15 | red = 31 |
| 16 | green = 32 |
| 17 | yellow = 33 |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 18 | blue = 36 |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 19 | gray = 37 |
| 20 | ) |
| 21 | |
| 22 | var ( |
| 23 | baseTimestamp time.Time |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 24 | emptyFieldMap FieldMap |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func init() { |
| 28 | baseTimestamp = time.Now() |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 31 | // TextFormatter formats logs into text |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 32 | type 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 Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 39 | // Override coloring based on CLICOLOR and CLICOLOR_FORCE. - https://bixense.com/clicolors/ |
| 40 | EnvironmentOverrideColors bool |
| 41 | |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 42 | // 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 Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 57 | |
| 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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 82 | func (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 | |
| 92 | func (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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 109 | func (f *TextFormatter) Format(entry *Entry) ([]byte, error) { |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 110 | prefixFieldClashes(entry.Data, f.FieldMap, entry.HasCaller()) |
| 111 | |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 112 | keys := make([]string, 0, len(entry.Data)) |
| 113 | for k := range entry.Data { |
| 114 | keys = append(keys, k) |
| 115 | } |
| 116 | |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 117 | fixedKeys := make([]string, 0, 4+len(entry.Data)) |
| 118 | if !f.DisableTimestamp { |
| 119 | fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyTime)) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 120 | } |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 121 | 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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 150 | if entry.Buffer != nil { |
| 151 | b = entry.Buffer |
| 152 | } else { |
| 153 | b = &bytes.Buffer{} |
| 154 | } |
| 155 | |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 156 | f.terminalInitOnce.Do(func() { f.init(entry) }) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 157 | |
| 158 | timestampFormat := f.TimestampFormat |
| 159 | if timestampFormat == "" { |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 160 | timestampFormat = defaultTimestampFormat |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 161 | } |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 162 | if f.isColored() { |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 163 | f.printColored(b, entry, keys, timestampFormat) |
| 164 | } else { |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 165 | 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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | b.WriteByte('\n') |
| 188 | return b.Bytes(), nil |
| 189 | } |
| 190 | |
| 191 | func (f *TextFormatter) printColored(b *bytes.Buffer, entry *Entry, keys []string, timestampFormat string) { |
| 192 | var levelColor int |
| 193 | switch entry.Level { |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 194 | case DebugLevel, TraceLevel: |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 195 | levelColor = gray |
| 196 | case WarnLevel: |
| 197 | levelColor = yellow |
| 198 | case ErrorLevel, FatalLevel, PanicLevel: |
| 199 | levelColor = red |
| 200 | default: |
| 201 | levelColor = blue |
| 202 | } |
| 203 | |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 204 | 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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 219 | |
| 220 | if f.DisableTimestamp { |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 221 | fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m%s %-44s ", levelColor, levelText, caller, entry.Message) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 222 | } else if !f.FullTimestamp { |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 223 | fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%04d]%s %-44s ", levelColor, levelText, int(entry.Time.Sub(baseTimestamp)/time.Second), caller, entry.Message) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 224 | } else { |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 225 | fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s]%s %-44s ", levelColor, levelText, entry.Time.Format(timestampFormat), caller, entry.Message) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 226 | } |
| 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 Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 234 | func (f *TextFormatter) needsQuoting(text string) bool { |
| 235 | if f.QuoteEmptyFields && len(text) == 0 { |
| 236 | return true |
| 237 | } |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 238 | for _, ch := range text { |
| 239 | if !((ch >= 'a' && ch <= 'z') || |
| 240 | (ch >= 'A' && ch <= 'Z') || |
| 241 | (ch >= '0' && ch <= '9') || |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 242 | ch == '-' || ch == '.' || ch == '_' || ch == '/' || ch == '@' || ch == '^' || ch == '+') { |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 243 | return true |
| 244 | } |
| 245 | } |
| 246 | return false |
| 247 | } |
| 248 | |
| 249 | func (f *TextFormatter) appendKeyValue(b *bytes.Buffer, key string, value interface{}) { |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 250 | if b.Len() > 0 { |
| 251 | b.WriteByte(' ') |
| 252 | } |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 253 | b.WriteString(key) |
| 254 | b.WriteByte('=') |
| 255 | f.appendValue(b, value) |
Jonathan Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) { |
Matteo Scandolo | 1f49bf5 | 2018-11-20 13:56:45 -0800 | [diff] [blame^] | 259 | 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 Hart | f86817b | 2018-08-17 10:35:54 -0700 | [diff] [blame] | 268 | } |
| 269 | } |