-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
75 lines (59 loc) · 1.62 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
package kredis
import (
"fmt"
"strings"
"time"
"github.com/fatih/color"
"github.com/redis/go-redis/extra/rediscmd/v9"
"github.com/redis/go-redis/v9"
)
type logging interface {
Info(redis.Cmder, time.Duration)
Warn(string, error)
}
type stdLogger struct{}
var debugLogger logging
// Enable logging of Redis commands and errors that are ignored by functions
// with failsafes. This is useful for development and tests. Logging is
// disabled by default.
func EnableDebugLogging() {
debugLogger = stdLogger{}
}
// Turn off debug logging.
func DisableDebugLogging() {
debugLogger = nil
}
func SetDebugLogger(userLogger logging) {
debugLogger = userLogger
}
func (log stdLogger) Info(cmd redis.Cmder, dur time.Duration) {
name, key, args := cmdLogParts(cmd)
if dur == 0 {
fmt.Printf("Kredis (tx) %s %s %s\n", name, key, args)
} else {
msec := float64(dur.Microseconds()) / 1000.0
fmt.Printf("Kredis (%.1fms) %s %s %s\n", msec, name, key, args)
}
}
func (log stdLogger) Warn(fnName string, err error) {
fmt.Printf("Kredis error in %s: %s", fnName, err.Error())
}
var cmdColor = color.New(color.FgYellow).SprintFunc()
var keyColor = color.New(color.FgCyan).SprintFunc()
var argsColor = color.New(color.FgGreen).SprintFunc()
func cmdLogParts(cmd redis.Cmder) (string, string, string) {
var name string
var key string
var args []string
cmdStr := rediscmd.CmdString(cmd)
for idx, str := range strings.Split(cmdStr, " ") {
if idx == 0 {
name = cmdColor(strings.ToUpper(str))
} else if idx == 1 {
key = keyColor(str)
} else {
args = append(args, argsColor(str))
}
}
return name, key, strings.Join(args, " ")
}