-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
115 lines (92 loc) · 3 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
package logger
import (
"sync"
)
var _ Logger = (*logger)(nil)
// logger is the primary structure used to interact with the productive code
type logger struct {
name string
mutLevel sync.RWMutex
logLevel LogLevel
logOutput LogOutputHandler
}
// NewLogger create a new logger instance
func NewLogger(name string, logLevel LogLevel, logOutput LogOutputHandler) *logger {
log := &logger{
name: name,
logLevel: logLevel,
logOutput: logOutput,
}
return log
}
func (l *logger) shouldSkipOutput(compareLogLevel LogLevel) bool {
if compareLogLevel == LogNone {
return true // do not output log level none
}
l.mutLevel.RLock()
shouldSkipOutput := l.logLevel > compareLogLevel
l.mutLevel.RUnlock()
return shouldSkipOutput
}
func (l *logger) outputMessageFromLogLevel(level LogLevel, message string, args ...interface{}) {
if l.shouldSkipOutput(level) {
return
}
logLine := newLogLine(l.name, GetCorrelation(), message, level, args...)
l.logOutput.Output(logLine)
}
// Trace outputs a tracing log message with optional provided arguments
func (l *logger) Trace(message string, args ...interface{}) {
l.outputMessageFromLogLevel(LogTrace, message, args...)
}
// Debug outputs a debugging log message with optional provided arguments
func (l *logger) Debug(message string, args ...interface{}) {
l.outputMessageFromLogLevel(LogDebug, message, args...)
}
// Info outputs an information log message with optional provided arguments
func (l *logger) Info(message string, args ...interface{}) {
l.outputMessageFromLogLevel(LogInfo, message, args...)
}
// Warn outputs a warning log message with optional provided arguments
func (l *logger) Warn(message string, args ...interface{}) {
l.outputMessageFromLogLevel(LogWarning, message, args...)
}
// Error outputs an error log message with optional provided arguments
func (l *logger) Error(message string, args ...interface{}) {
l.outputMessageFromLogLevel(LogError, message, args...)
}
// Log outputs a defined log level message with optional provided arguments
func (l *logger) Log(logLevel LogLevel, message string, args ...interface{}) {
l.outputMessageFromLogLevel(logLevel, message, args...)
}
// LogIfError outputs an error log message with optional provided arguments if the provided error parameter is not nil
func (l *logger) LogIfError(err error, args ...interface{}) {
if err == nil {
return
}
l.Error(err.Error(), args...)
}
// LogLine forwards the log line towards underlying log output handler
func (l *logger) LogLine(line *LogLine) {
if line == nil {
return
}
l.logOutput.Output(line)
}
// SetLevel sets the current level of the logger
func (l *logger) SetLevel(logLevel LogLevel) {
l.mutLevel.Lock()
l.logLevel = logLevel
l.mutLevel.Unlock()
}
// GetLevel gets the current level of the logger
func (l *logger) GetLevel() LogLevel {
l.mutLevel.RLock()
level := l.logLevel
l.mutLevel.RUnlock()
return level
}
// IsInterfaceNil returns true if there is no value under the interface
func (l *logger) IsInterfaceNil() bool {
return l == nil
}