-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathlogging.go
117 lines (104 loc) · 3.13 KB
/
logging.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
package centrifuge
// LogLevel describes the chosen log level.
type LogLevel int
const (
// LogLevelNone means no logging.
LogLevelNone LogLevel = iota
// LogLevelTrace turns on trace logs - should only be used during development. This
// log level shows all client-server communication.
LogLevelTrace
// LogLevelDebug turns on debug logs - it's generally too much for production in normal
// conditions but can help when developing and investigating problems in production.
LogLevelDebug
// LogLevelInfo logs useful server information. This includes various information
// about problems with client connections which is not Centrifuge errors but
// in most situations malformed client behaviour.
LogLevelInfo
// LogLevelWarn logs server warnings. This may contain tips for a developer about things
// which should be addressed but usually not immediately.
LogLevelWarn
// LogLevelError level logs only server errors. This is logging that means non-working
// Centrifuge and may require effort from developers/administrators to make things
// work again.
LogLevelError
)
// levelToString matches LogLevel to its string representation.
var levelToString = map[LogLevel]string{
LogLevelTrace: "trace",
LogLevelDebug: "debug",
LogLevelInfo: "info",
LogLevelWarn: "warn",
LogLevelError: "error",
LogLevelNone: "none",
}
// LogLevelToString transforms Level to its string representation.
func LogLevelToString(l LogLevel) string {
return levelToString[l]
}
// LogEntry represents log entry.
type LogEntry struct {
Level LogLevel
Message string
Fields map[string]any
Error error
}
// buildLogEntry helps to create Entry.
func buildLogEntry(level LogLevel, err error, message string, fields ...map[string]any) LogEntry {
var f map[string]any
if len(fields) == 1 {
f = fields[0]
} else if len(fields) > 1 {
f = make(map[string]any)
for _, field := range fields {
for k, v := range field {
f[k] = v
}
}
}
if err != nil {
f["error"] = err.Error()
}
return LogEntry{
Level: level,
Message: message,
Fields: f,
Error: err,
}
}
// newLogEntry creates new LogEntry.
func newLogEntry(level LogLevel, message string, fields ...map[string]any) LogEntry {
return buildLogEntry(level, nil, message, fields...)
}
// newErrorLogEntry creates new LogEntry with LogLevelError and error attached to it.
func newErrorLogEntry(err error, message string, fields ...map[string]any) LogEntry {
return buildLogEntry(LogLevelError, err, message, fields...)
}
// LogHandler handles log entries - i.e. writes into correct destination if necessary.
type LogHandler func(LogEntry)
func newLogger(level LogLevel, handler LogHandler) *logger {
return &logger{
level: level,
handler: handler,
}
}
// logger can log entries.
type logger struct {
level LogLevel
handler LogHandler
}
// log calls log handler with provided LogEntry.
func (l *logger) log(entry LogEntry) {
if l == nil {
return
}
if l.enabled(entry.Level) {
l.handler(entry)
}
}
// enabled says whether specified Level enabled or not.
func (l *logger) enabled(level LogLevel) bool {
if l == nil {
return false
}
return level >= l.level && l.level != LogLevelNone
}