-
Notifications
You must be signed in to change notification settings - Fork 25
/
logger.go
133 lines (113 loc) · 3.38 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
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
* SPDX-License-Identifier: MIT
*/
package logger
import (
"strings"
"sync"
)
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 Dapr Logger that is shared globally.
// TODO: User will disable or enable logger on demand.
var globalLoggers = map[string]Logger{}
var globalLoggersLock = sync.RWMutex{}
// Logger includes the logging api sets
type Logger interface {
// EnableJSONOutput enables JSON formatted output log
EnableJSONOutput(enabled bool)
// SetAppID sets dapr_id field in the log. Default value is empty string
SetAppID(id string)
// SetOutputLevel sets log output level
SetOutputLevel(outputLevel LogLevel)
// WithLogType specify the log_type field in log. Default value is LogTypeLog
WithLogType(logType string) Logger
// Info logs a message at level Info.
Info(args ...interface{})
// Infof logs a message at level Info.
Infof(format string, args ...interface{})
// Debug logs a message at level Debug.
Debug(args ...interface{})
// Debugf logs a message at level Debug.
Debugf(format string, args ...interface{})
// Warn logs a message at level Warn.
Warn(args ...interface{})
// Warnf logs a message at level Warn.
Warnf(format string, args ...interface{})
// Error logs a message at level Error.
Error(args ...interface{})
// Errorf logs a message at level Error.
Errorf(format string, args ...interface{})
// Fatal logs a message at level Fatal then the process will exit with status set to 1.
Fatal(args ...interface{})
// Fatalf logs a message at level Fatal then the process will exit with status set to 1.
Fatalf(format string, args ...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 NewLogger(name string) Logger {
globalLoggersLock.Lock()
defer globalLoggersLock.Unlock()
logger, ok := globalLoggers[name]
if !ok {
logger = newDaprLogger(name)
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
}