-
Notifications
You must be signed in to change notification settings - Fork 104
/
logging.go
148 lines (121 loc) · 4.29 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
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
package gocb
import (
"fmt"
"log"
"strings"
gocbcore "github.com/couchbase/gocbcore/v10"
)
// LogLevel specifies the severity of a log message.
type LogLevel gocbcore.LogLevel
// Various logging levels (or subsystems) which can categorize the message.
// Currently these are ordered in decreasing severity.
const (
LogError LogLevel = LogLevel(gocbcore.LogError)
LogWarn LogLevel = LogLevel(gocbcore.LogWarn)
LogInfo LogLevel = LogLevel(gocbcore.LogInfo)
LogDebug LogLevel = LogLevel(gocbcore.LogDebug)
LogTrace LogLevel = LogLevel(gocbcore.LogTrace)
LogSched LogLevel = LogLevel(gocbcore.LogSched)
LogMaxVerbosity LogLevel = LogLevel(gocbcore.LogMaxVerbosity)
)
// LogRedactLevel specifies the degree with which to redact the logs.
type LogRedactLevel uint
const (
// RedactNone indicates to perform no redactions
RedactNone LogRedactLevel = iota
// RedactPartial indicates to redact all possible user-identifying information from logs.
RedactPartial
// RedactFull indicates to fully redact all possible identifying information from logs.
RedactFull
)
// SetLogRedactionLevel specifies the level with which logs should be redacted.
func SetLogRedactionLevel(level LogRedactLevel) {
globalLogRedactionLevel = level
gocbcore.SetLogRedactionLevel(gocbcore.LogRedactLevel(level))
}
// Logger defines a logging interface. You can either use one of the default loggers
// (DefaultStdioLogger(), VerboseStdioLogger()) or implement your own.
type Logger interface {
// Outputs logging information:
// level is the verbosity level
// offset is the position within the calling stack from which the message
// originated. This is useful for contextual loggers which retrieve file/line
// information.
Log(level LogLevel, offset int, format string, v ...interface{}) error
}
var (
globalLogger Logger
globalLogRedactionLevel LogRedactLevel
)
type coreLogWrapper struct {
wrapped gocbcore.Logger
}
func (wrapper coreLogWrapper) Log(level LogLevel, offset int, format string, v ...interface{}) error {
return wrapper.wrapped.Log(gocbcore.LogLevel(level), offset+2, format, v...)
}
// DefaultStdioLogger gets the default standard I/O logger.
// gocb.SetLogger(gocb.DefaultStdioLogger())
func DefaultStdioLogger() Logger {
return &coreLogWrapper{
wrapped: gocbcore.DefaultStdioLogger(),
}
}
// VerboseStdioLogger is a more verbose level of DefaultStdioLogger(). Messages
// pertaining to the scheduling of ordinary commands (and their responses) will
// also be emitted.
// gocb.SetLogger(gocb.VerboseStdioLogger())
func VerboseStdioLogger() Logger {
return coreLogWrapper{
wrapped: gocbcore.VerboseStdioLogger(),
}
}
type coreLogger struct {
wrapped Logger
}
func (wrapper coreLogger) Log(level gocbcore.LogLevel, offset int, format string, v ...interface{}) error {
return wrapper.wrapped.Log(LogLevel(level), offset+2, format, v...)
}
func getCoreLogger(logger Logger) gocbcore.Logger {
typedLogger, isCoreLogger := logger.(*coreLogWrapper)
if isCoreLogger {
return typedLogger.wrapped
}
return &coreLogger{
wrapped: logger,
}
}
// SetLogger sets a logger to be used by the library. A logger can be obtained via
// the DefaultStdioLogger() or VerboseStdioLogger() functions. You can also implement
// your own logger using the Logger interface.
func SetLogger(logger Logger) {
globalLogger = logger
gocbcore.SetLogger(getCoreLogger(logger))
// gocbcore.SetLogRedactionLevel(gocbcore.LogRedactLevel(globalLogRedactionLevel))
}
func logExf(level LogLevel, offset int, format string, v ...interface{}) {
if globalLogger != nil {
err := globalLogger.Log(level, offset+1, format, v...)
if err != nil {
log.Printf("Logger error occurred (%s)\n", err)
}
}
}
func logInfof(format string, v ...interface{}) {
logExf(LogInfo, 1, format, v...)
}
func logDebugf(format string, v ...interface{}) {
logExf(LogDebug, 1, format, v...)
}
func logSchedf(format string, v ...interface{}) {
logExf(LogSched, 1, format, v...)
}
func logWarnf(format string, v ...interface{}) {
logExf(LogWarn, 1, format, v...)
}
func logErrorf(format string, v ...interface{}) {
logExf(LogError, 1, format, v...)
}
func reindentLog(indent, message string) string {
reindentedMessage := strings.Replace(message, "\n", "\n"+indent, -1)
return fmt.Sprintf("%s%s", indent, reindentedMessage)
}