-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
133 lines (119 loc) · 2.64 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
package pgx
import (
"encoding/hex"
"fmt"
uuid "github.com/ronaldslc/go.uuid"
"github.com/pkg/errors"
)
// The values for log levels are chosen such that the zero value means that no
// log level was specified.
const (
LogLevelTrace = 6
LogLevelDebug = 5
LogLevelInfo = 4
LogLevelWarn = 3
LogLevelError = 2
LogLevelNone = 1
)
// LogLevel represents the pgx logging level. See LogLevel* constants for
// possible values.
type LogLevel int
func (ll LogLevel) String() string {
switch ll {
case LogLevelTrace:
return "trace"
case LogLevelDebug:
return "debug"
case LogLevelInfo:
return "info"
case LogLevelWarn:
return "warn"
case LogLevelError:
return "error"
case LogLevelNone:
return "none"
default:
return fmt.Sprintf("invalid level %d", ll)
}
}
type LogData []KV
func (l *LogData) Add(key string, data interface{}) {
*l = append(*l, KV{Key: key, Value: data})
}
type KV struct {
Key string
Value interface{}
}
// Logger is the interface used to get logging from pgx internals.
type Logger interface {
// Log a message at the given level with data key/value pairs. data may be nil.
Log(level LogLevel, msg string, ld LogData)
}
// LogLevelFromString converts log level string to constant
//
// Valid levels:
// trace
// debug
// info
// warn
// error
// none
func LogLevelFromString(s string) (LogLevel, error) {
switch s {
case "trace":
return LogLevelTrace, nil
case "debug":
return LogLevelDebug, nil
case "info":
return LogLevelInfo, nil
case "warn":
return LogLevelWarn, nil
case "error":
return LogLevelError, nil
case "none":
return LogLevelNone, nil
default:
return 0, errors.New("invalid log level")
}
}
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)
}
case *string:
if len(*v) > 64 {
a = fmt.Sprintf("%s (truncated %d bytes)", (*v)[:64], len(*v)-64)
} else {
a = fmt.Sprintf("%s", *v)
}
case [][16]uint8: // array of raw UUID formats
out := make([]string, 0, len(v))
for _, va := range v {
b := uuid.UUID(va)
out = append(out, b.String())
}
a = out
default:
if v, ok := a.(fmt.Stringer); ok {
vstr := v.String()
if len(v.String()) > 64 {
a = fmt.Sprintf("%s (truncated %d bytes)", vstr[:64], len(vstr)-64)
} else {
a = vstr
}
}
}
logArgs = append(logArgs, a)
}
return logArgs
}