-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
168 lines (142 loc) · 5.13 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Package gonyan is a stream-based logging library.
package gonyan
import (
"fmt"
"time"
)
// Logger represents an instance of a thread safe logger. It can hold different
// streams to log to and a few useful settings to customise the logs such as
// custom tag, metadata and timestamp.
type Logger struct {
tag string
timestamp bool
streamManager *StreamManager
metadata map[string]string
m mutex
}
// NewLogger creates a new logger instance with provided configuration.
func NewLogger(tag string, timestamp bool) *Logger {
logger := &Logger{
tag: tag,
timestamp: timestamp,
streamManager: NewStreamManager(),
}
return logger
}
// SetMetadata sets the optional metadata values for this logger.
// Metadata will be added to each log streamed from the logger instace.
func (l *Logger) SetMetadata(metadata map[string]string) {
if metadata != nil {
l.metadata = metadata
}
}
// ClearMetadata clears the optional metadata from the logger instance.
func (l *Logger) ClearMetadata() {
l.metadata = nil
}
// DisableLock will disable the logger internal mutex operations.
func (l *Logger) DisableLock() {
l.m.Disable()
}
// ReenableLock will reenable the logger internal mutex operations.
func (l *Logger) ReenableLock() {
l.m.Enable()
}
// RegisterStream register provided stream associating it with provided level
// inside the interal StreamManager instance.
func (l *Logger) RegisterStream(level LogLevel, stream Stream) {
l.streamManager.Register(level, stream)
}
// Debugf logs provided message into registered debug level streams.
// The function accepts a format and a variadic number of arguments
// to compose the final log data.
func (l *Logger) Debugf(format string, args ...interface{}) {
l.Debug(fmt.Sprintf(format, args...))
}
// Debug logs provided message into registered debug level streams.
func (l *Logger) Debug(message string) {
l.Log(Debug, message)
}
// Verbosef logs provided message into registered verbose level streams.
// The function accepts a format and a variadic number of arguments
// to compose the final log data.
func (l *Logger) Verbosef(format string, args ...interface{}) {
l.Verbose(fmt.Sprintf(format, args...))
}
// Verbose logs provided message into registered verbose level streams.
func (l *Logger) Verbose(message string) {
l.Log(Verbose, message)
}
// Infof logs provided message into info level streams.
// The function accepts a format and a variadic number of arguments
// to compose the final log data.
func (l *Logger) Infof(format string, args ...interface{}) {
l.Info(fmt.Sprintf(format, args...))
}
// Info logs provided message into info level streams.
func (l *Logger) Info(message string) {
l.Log(Info, message)
}
// Warningf logs provided message into warning level streams.
// The function accepts a format and a variadic number of arguments
// to compose the final log data.
func (l *Logger) Warningf(format string, args ...interface{}) {
l.Warning(fmt.Sprintf(format, args...))
}
// Warning logs provided message into warning level streams.
func (l *Logger) Warning(message string) {
l.Log(Warning, message)
}
// Errorf logs provided message into error level streams.
// The function accepts a format and a variadic number of arguments
// to compose the final log data.
func (l *Logger) Errorf(format string, args ...interface{}) {
l.Error(fmt.Sprintf(format, args...))
}
// Error logs provided message into error level streams.
func (l *Logger) Error(message string) {
l.Log(Error, message)
}
// Fatalf logs provided message into fatal level streams.
// The function accepts a format and a variadic number of arguments
// to compose the final log data.
func (l *Logger) Fatalf(format string, args ...interface{}) {
l.Fatal(fmt.Sprintf(format, args...))
}
// Fatal logs provided message into fatal level streams.
func (l *Logger) Fatal(message string) {
l.Log(Fatal, message)
}
// Panicf logs provided message into panic level streams.
// The function accepts a format and a variadic number of arguments
// to compose the final log data.
// Note: When log is performed panic() is invoked.
func (l *Logger) Panicf(format string, args ...interface{}) {
l.Fatal(fmt.Sprintf(format, args...))
}
// Panic logs provided message into panic level streams.
// Note: When log is performed panic() is invoked.
func (l *Logger) Panic(message string) {
l.Log(Fatal, message)
panic(message)
}
// Logf logs provided message into the streams corresponding to provided level.
// The function accepts a format and a variadic number of arguments
// to compose the final log data.
func (l *Logger) Logf(level LogLevel, format string, args ...interface{}) {
l.Logf(level, fmt.Sprintf(format, args...))
}
// Log function builds the final JSON message and sends it to the correct streams.
func (l *Logger) Log(level LogLevel, message string) {
var t int64
if l.timestamp {
t = time.Now().UTC().UnixNano()
}
m := NewLogMessage(l.tag, t, message, l.metadata)
// Send message to streams via the StreamManager.
l.m.Lock()
defer l.m.Unlock()
if err := l.streamManager.Send(level, m); err != nil {
fmt.Printf("[FATAL] [gonyan] Can't send log `%s` to stream `%s`", message, GetLevelLabel(level))
}
}