forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
147 lines (120 loc) · 4.26 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
package gocb
import (
"fmt"
"log"
"strings"
"gopkg.in/couchbase/gocbcore.v7"
)
// 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(gocbcore.LogError)
LogWarn = LogLevel(gocbcore.LogWarn)
LogInfo = LogLevel(gocbcore.LogInfo)
LogDebug = LogLevel(gocbcore.LogDebug)
LogTrace = LogLevel(gocbcore.LogTrace)
LogSched = LogLevel(gocbcore.LogSched)
LogMaxVerbosity = LogLevel(gocbcore.LogMaxVerbosity)
)
// LogRedactLevel specifies the degree with which to redact the logs.
type LogRedactLevel int
const (
// RedactNone indicates to perform no redactions
RedactNone = LogRedactLevel(0)
// RedactPartial indicates to redact all possible user-identifying information from logs.
RedactPartial = LogRedactLevel(1)
// RedactFull indicates to fully redact all possible identifying information from logs.
RedactFull = LogRedactLevel(1)
)
// SetLogRedactionLevel specifies the level with which logs should be redacted.
func SetLogRedactionLevel(level LogRedactLevel) {
// We don't current log any data that falls under our current redaction rules.
// This function is included as a stub for future implementations of log redaction
// that act at a higher level and may need to perform actual redaction's.
}
// 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
)
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))
}
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)
}