-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
142 lines (119 loc) · 3.05 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
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
package console
import (
"fmt"
stdLog "log"
"os"
"strings"
"time"
"github.com/getfider/fider/app/pkg/env"
"github.com/getfider/fider/app/pkg/log"
)
// Logger output messages to console
type Logger struct {
logger *stdLog.Logger
level log.Level
tag string
props log.Props
enabled bool
}
// NewLogger creates a new Logger
func NewLogger(tag string) *Logger {
level := strings.ToUpper(env.Config.Log.Level)
return &Logger{
tag: tag,
logger: stdLog.New(os.Stdout, "", 0),
level: log.ParseLevel(level),
props: make(log.Props, 0),
enabled: !env.IsTest(),
}
}
// Disable logs for this logger
func (l *Logger) Disable() {
l.enabled = false
}
// Enable logs for this logger
func (l *Logger) Enable() {
l.enabled = true
}
// SetLevel increases/decreases current log level
func (l *Logger) SetLevel(level log.Level) {
l.level = level
}
// IsEnabled returns true if given level is enabled
func (l *Logger) IsEnabled(level log.Level) bool {
return l.enabled && level >= l.level
}
// Debug logs a DEBUG message
func (l *Logger) Debug(message string) {
l.log(log.DEBUG, message, nil)
}
// Debugf logs a DEBUG message
func (l *Logger) Debugf(message string, props log.Props) {
l.log(log.DEBUG, message, props)
}
// Info logs a INFO message
func (l *Logger) Info(message string) {
l.log(log.INFO, message, nil)
}
// Infof logs a INFO message
func (l *Logger) Infof(message string, props log.Props) {
l.log(log.INFO, message, props)
}
// Warn logs a WARN message
func (l *Logger) Warn(message string) {
l.log(log.WARN, message, nil)
}
// Warnf logs a WARN message
func (l *Logger) Warnf(message string, props log.Props) {
l.log(log.WARN, message, props)
}
// Errorf logs a ERROR message
func (l *Logger) Errorf(message string, props log.Props) {
l.log(log.ERROR, message, props)
}
// Error logs a ERROR message
func (l *Logger) Error(err error) {
if err != nil {
l.log(log.ERROR, err.Error(), nil)
} else {
l.log(log.ERROR, "nil", nil)
}
}
// Write writes len(p) bytes from p to the underlying data stream.
func (l *Logger) Write(p []byte) (int, error) {
l.Debug(fmt.Sprintf("%s", p))
return len(p), nil
}
// New returns a copy of current logger with empty context
func (l *Logger) New() log.Logger {
return NewLogger(l.tag)
}
// SetProperty with given key:value into current logger context
func (l *Logger) SetProperty(key string, value interface{}) {
l.props[key] = value
}
func (l *Logger) log(level log.Level, message string, props log.Props) {
props = l.props.Merge(props)
if !l.IsEnabled(level) {
return
}
message = log.Parse(message, props, true)
contextID := l.props[log.PropertyKeyContextID]
l.logger.Printf("%s [%s] [%s] [%s] %s\n", colorizeLevel(level), time.Now().Format(time.RFC3339), l.tag, contextID, message)
}
func colorizeLevel(level log.Level) string {
var color func(interface{}) string
switch level {
case log.DEBUG:
color = log.Magenta
case log.INFO:
color = log.Blue
case log.WARN:
color = log.Yellow
case log.ERROR:
color = log.Red
default:
color = log.Red
}
return log.Bold(color(level.String()))
}