-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
124 lines (104 loc) · 2.89 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
package logger
import (
"strings"
"sync"
"go.uber.org/zap"
)
const (
// LogTypeLog is normal log type.
LogTypeLog = "log"
// LogTypeRequest is Request log type.
LogTypeRequest = "request"
// Field names that defines Dapr log schema.
logFieldTimeStamp = "time"
logFieldLevel = "level"
logFieldType = "type"
logFieldScope = "scope"
logFieldMessage = "msg"
logFieldInstance = "instance"
logFieldDaprVer = "ver"
logFieldAppID = "app_id"
)
// LogLevel is Dapr Logger Level type.
type LogLevel string
const (
// DebugLevel has verbose message.
DebugLevel LogLevel = "debug"
// InfoLevel is default log level.
InfoLevel LogLevel = "info"
// WarnLevel is for logging messages about possible issues.
WarnLevel LogLevel = "warn"
// ErrorLevel is for logging errors.
ErrorLevel LogLevel = "error"
// FatalLevel is for logging fatal messages. The system shuts down after logging the message.
FatalLevel LogLevel = "fatal"
// UndefinedLevel is for undefined log level.
UndefinedLevel LogLevel = "undefined"
)
// globalLoggers is the collection of Logger that is shared globally.
// TODO: User will disable or enable logger on demand.
var (
globalLoggers = map[string]Logger{}
globalLoggersLock = sync.RWMutex{}
)
// Logger includes the logging api sets.
type Logger interface {
// Info logs a message at level Info.
Info(msg string, keysAndValues ...interface{})
// Debug logs a message at level Debug.
Debug(msg string, keysAndValues ...interface{})
// Warn logs a message at level Warn.
Warn(msg string, keysAndValues ...interface{})
// Error logs a message at level Error.
Error(errVal error, keysAndValues string, args ...interface{})
// Fatal logs a message at level Fatal then the process will exit with status set to 1.
Fatal(msg string, keysAndValues ...interface{})
}
// toLogLevel converts to LogLevel.
func toLogLevel(level string) LogLevel {
switch strings.ToLower(level) {
case "debug":
return DebugLevel
case "info":
return InfoLevel
case "warn":
return WarnLevel
case "error":
return ErrorLevel
case "fatal":
return FatalLevel
}
// unsupported log level by Dapr
return UndefinedLevel
}
// NewLogger creates new Logger instance.
func WithName(name string) Logger {
globalLoggersLock.Lock()
defer globalLoggersLock.Unlock()
logger, ok := globalLoggers[name]
if !ok {
logger = newZapLogger(name)
globalLoggers[name] = logger
}
return logger
}
// NewLogger creates new Logger instance.
func WithNameOptions(name string, options ...zap.Option) Logger {
globalLoggersLock.Lock()
defer globalLoggersLock.Unlock()
logger, ok := globalLoggers[name]
if !ok {
logger = newZapLoggerWithOptions(name, options...)
globalLoggers[name] = logger
}
return logger
}
func getLoggers() map[string]Logger {
globalLoggersLock.RLock()
defer globalLoggersLock.RUnlock()
l := map[string]Logger{}
for k, v := range globalLoggers {
l[k] = v
}
return l
}