-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlog.go
More file actions
208 lines (180 loc) · 4.84 KB
/
Copy pathlog.go
File metadata and controls
208 lines (180 loc) · 4.84 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package godatabend
import (
"context"
"fmt"
"io"
"log/slog"
"os"
"path"
"runtime"
"strings"
"sync"
)
type contextKey string
// DBSessionIDKey is context key of session id
const DBSessionIDKey contextKey = "LOG_SESSION_ID"
// SFSessionUserKey is context key of user id of a session
const SFSessionUserKey contextKey = "LOG_USER"
// LogKeys these keys in context should be included in logging messages when using logger.WithContext
var LogKeys = [...]contextKey{DBSessionIDKey, SFSessionUserKey}
// ContextLogger represents a logger that already captured desired context.
type ContextLogger interface {
Infoln(args ...interface{})
}
// DBLogger Databend logger interface backed by slog.
type DBLogger interface {
Debugf(format string, args ...interface{})
Error(args ...interface{})
Info(args ...interface{})
SetLogLevel(level string) error
SetOutput(output io.Writer)
WithContext(ctx context.Context) ContextLogger
}
// DBCallerPrettyfier to provide base file name and function name from calling frame used in SFLogger
func DBCallerPrettyfier(frame *runtime.Frame) (string, string) {
return path.Base(frame.Function), fmt.Sprintf("%s:%d", path.Base(frame.File), frame.Line)
}
type defaultLogger struct {
mu sync.RWMutex
levelVar *slog.LevelVar
handlerFn func(io.Writer) slog.Handler
inner *slog.Logger
output io.Writer
}
// CreateDefaultLogger return a new instance of logger with default config
func CreateDefaultLogger() DBLogger {
levelVar := &slog.LevelVar{}
levelVar.Set(slog.LevelInfo)
replaceAttr := func(groups []string, attr slog.Attr) slog.Attr {
if attr.Key == slog.SourceKey {
if src, ok := attr.Value.Any().(*slog.Source); ok && src != nil {
frame := &runtime.Frame{
Function: src.Function,
File: src.File,
Line: src.Line,
}
function, location := DBCallerPrettyfier(frame)
attr.Value = slog.StringValue(strings.TrimSpace(function + " " + location))
}
}
return attr
}
handlerFn := func(w io.Writer) slog.Handler {
if w == nil {
w = os.Stdout
}
return slog.NewTextHandler(w, &slog.HandlerOptions{
AddSource: true,
Level: levelVar,
ReplaceAttr: replaceAttr,
})
}
dLogger := &defaultLogger{
levelVar: levelVar,
handlerFn: handlerFn,
output: os.Stdout,
}
dLogger.inner = slog.New(handlerFn(dLogger.output))
return dLogger
}
func (log *defaultLogger) getLogger() *slog.Logger {
log.mu.RLock()
defer log.mu.RUnlock()
return log.inner
}
// SetLogLevel set logging level for calling defaultLogger
func (log *defaultLogger) SetLogLevel(level string) error {
lvl, err := parseLevel(level)
if err != nil {
return err
}
log.levelVar.Set(lvl)
return nil
}
func parseLevel(level string) (slog.Level, error) {
switch strings.ToLower(level) {
case "trace":
fallthrough
case "debug":
return slog.LevelDebug, nil
case "info":
return slog.LevelInfo, nil
case "warn", "warning":
return slog.LevelWarn, nil
case "error":
return slog.LevelError, nil
case "dpanic", "panic", "fatal":
return slog.LevelError, nil
default:
return slog.LevelInfo, fmt.Errorf("unknown log level: %s", level)
}
}
// WithContext return a ContextLogger to include fields in context
func (log *defaultLogger) WithContext(ctx context.Context) ContextLogger {
base := log.getLogger()
attrs := context2Attrs(ctx)
if len(attrs) > 0 {
args := make([]interface{}, len(attrs))
for i := range attrs {
args[i] = attrs[i]
}
base = base.With(args...)
}
return &contextLogger{inner: base}
}
func (log *defaultLogger) Info(args ...interface{}) {
log.getLogger().Info(fmt.Sprint(args...))
}
func (log *defaultLogger) Error(args ...interface{}) {
log.getLogger().Error(fmt.Sprint(args...))
}
func (log *defaultLogger) Debugf(format string, args ...interface{}) {
log.getLogger().Debug(fmt.Sprintf(format, args...))
}
func (log *defaultLogger) SetOutput(output io.Writer) {
if output == nil {
return
}
log.mu.Lock()
log.output = output
log.inner = slog.New(log.handlerFn(output))
log.mu.Unlock()
}
// SetLogger set a new logger of defaultLogger interface for godatabend
func SetLogger(inLogger DBLogger) {
if inLogger == nil {
return
}
logger = inLogger
}
// GetLogger return logger that is not public
func GetLogger() DBLogger {
return logger
}
func context2Attrs(ctx context.Context) []slog.Attr {
attrs := make([]slog.Attr, 0, len(LogKeys))
if ctx == nil {
return attrs
}
for i := 0; i < len(LogKeys); i++ {
if ctx.Value(LogKeys[i]) != nil {
attrs = append(attrs, slog.Any(string(LogKeys[i]), ctx.Value(LogKeys[i])))
}
}
return attrs
}
type contextLogger struct {
inner *slog.Logger
}
func (c *contextLogger) Infoln(args ...interface{}) {
if c == nil || c.inner == nil {
return
}
c.inner.Info(formatLine(args...))
}
func formatLine(args ...interface{}) string {
if len(args) == 0 {
return ""
}
return strings.TrimSuffix(fmt.Sprintln(args...), "\n")
}