forked from AreaHQ/logging
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcoloured_formatter.go
40 lines (33 loc) · 989 Bytes
/
coloured_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
package logging
import (
"fmt"
)
const (
// For colouring
resetSeq = "\033[0m"
colourSeq = "\033[0;%dm"
)
// Colour map
var colour = map[level]string{
INFO: fmt.Sprintf(colourSeq, 94), // blue
WARNING: fmt.Sprintf(colourSeq, 95), // pink
ERROR: fmt.Sprintf(colourSeq, 91), // red
FATAL: fmt.Sprintf(colourSeq, 91), // red
}
// ColouredFormatter colours log messages with ASCI escape codes
// and adds filename and line number before the log message
// See https://en.wikipedia.org/wiki/ANSI_escape_code
type ColouredFormatter struct {
}
// GetPrefix returns colour escape code
func (f *ColouredFormatter) GetPrefix(lvl level) string {
return colour[lvl]
}
// GetSuffix returns reset sequence code
func (f *ColouredFormatter) GetSuffix(lvl level) string {
return resetSeq
}
// Format adds filename and line number before the log message
func (f *ColouredFormatter) Format(lvl level, v ...interface{}) []interface{} {
return append([]interface{}{header()}, v...)
}