-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.go
73 lines (62 loc) · 1.75 KB
/
logger.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
package mqrr
import (
"fmt"
"github.com/sirupsen/logrus"
"io"
"os"
"strings"
)
var log = logrus.New()
// LogDisableColors disables the color of the log output.
var LogDisableColors = false
func init() {
log.SetOutput(os.Stdout)
log.SetFormatter(newTextFormatter())
}
// SetLogWriter sets the default io.Writer used by mqrr for log output.
func SetLogWriter(w io.Writer) {
log.SetOutput(w)
}
// SetLogFormatter sets the log formatter of the mqrr log output.
func SetLogFormatter(formatter logrus.Formatter) {
log.SetFormatter(formatter)
}
func debugPrint(format string, values ...interface{}) {
if IsDebugging() {
if !strings.HasSuffix(format, "\n") {
format += "\n"
}
fmt.Fprintf(log.Out, "[MQRR] "+format, values...)
}
}
// textFormatter is the default log formatter of mqrr.
type textFormatter logrus.TextFormatter
func newTextFormatter() *textFormatter {
tf := textFormatter(logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05.000",
DisableLevelTruncation: true,
})
return &tf
}
func (f *textFormatter) Format(entry *logrus.Entry) ([]byte, error) {
var levelColor int
switch entry.Level {
case logrus.DebugLevel, logrus.TraceLevel:
levelColor = 31 // gray
case logrus.WarnLevel:
levelColor = 33 // yellow
case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
levelColor = 31 // red
default:
levelColor = 36 // blue
}
timestamp := entry.Time.Format(f.TimestampFormat)
level := strings.ToUpper(entry.Level.String())
if LogDisableColors {
return []byte(fmt.Sprintf("%s [%s] [MQRR] %s\n", timestamp, level, entry.Message)), nil
} else {
return []byte(fmt.Sprintf("%s \x1b[%dm[%s]\x1b[0m \x1b[36m[MQRR]\x1b[0m %s\n",
timestamp, levelColor, level, entry.Message)), nil
}
}