-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
75 lines (62 loc) · 1.9 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
package pgx
import (
"encoding/hex"
"fmt"
)
// Logger is the interface used to get logging from pgx internals.
// https://github.com/inconshreveable/log15 is the recommended logging package.
// This logging interface was extracted from there. However, it should be simple
// to adapt any logger to this interface.
type Logger interface {
// Log a message at the given level with context key/value pairs
Debug(msg string, ctx ...interface{})
Info(msg string, ctx ...interface{})
Warn(msg string, ctx ...interface{})
Error(msg string, ctx ...interface{})
}
type discardLogger struct{}
// default discardLogger instance
var dlogger = &discardLogger{}
func (l *discardLogger) Debug(msg string, ctx ...interface{}) {}
func (l *discardLogger) Info(msg string, ctx ...interface{}) {}
func (l *discardLogger) Warn(msg string, ctx ...interface{}) {}
func (l *discardLogger) Error(msg string, ctx ...interface{}) {}
type connLogger struct {
logger Logger
pid int32
}
func (l *connLogger) Debug(msg string, ctx ...interface{}) {
ctx = append(ctx, "pid", l.pid)
l.logger.Debug(msg, ctx...)
}
func (l *connLogger) Info(msg string, ctx ...interface{}) {
ctx = append(ctx, "pid", l.pid)
l.logger.Info(msg, ctx...)
}
func (l *connLogger) Warn(msg string, ctx ...interface{}) {
ctx = append(ctx, "pid", l.pid)
l.logger.Warn(msg, ctx...)
}
func (l *connLogger) Error(msg string, ctx ...interface{}) {
ctx = append(ctx, "pid", l.pid)
l.logger.Error(msg, ctx...)
}
func logQueryArgs(args []interface{}) []interface{} {
logArgs := make([]interface{}, 0, len(args))
for _, a := range args {
switch v := a.(type) {
case []byte:
if len(v) < 64 {
a = hex.EncodeToString(v)
} else {
a = fmt.Sprintf("%x (truncated %d bytes)", v[:64], len(v)-64)
}
case string:
if len(v) > 64 {
a = fmt.Sprintf("%s (truncated %d bytes)", v[:64], len(v)-64)
}
}
logArgs = append(logArgs, a)
}
return logArgs
}