-
Notifications
You must be signed in to change notification settings - Fork 39
/
logger.go
92 lines (72 loc) · 1.87 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
package gosql
import (
"fmt"
"log"
"os"
"regexp"
"strings"
"time"
)
const (
fmtLogQuery = `Query: %s`
fmtLogArgs = `Args: %#v`
fmtLogError = `Error: %v`
fmtLogTimeTaken = `Time: %0.5fs`
)
var (
reInvisibleChars = regexp.MustCompile(`[\s\r\n\t]+`)
)
// QueryStatus represents the status of a query after being executed.
type QueryStatus struct {
Query string
Args interface{}
Start time.Time
End time.Time
Err error
}
// String returns a formatted log message.
func (q *QueryStatus) String() string {
lines := make([]string, 0, 8)
if query := q.Query; query != "" {
query = reInvisibleChars.ReplaceAllString(query, ` `)
query = strings.TrimSpace(query)
lines = append(lines, fmt.Sprintf(fmtLogQuery, query))
}
if args, ok := q.Args.([]interface{}); ok && len(args) == 0 {
q.Args = nil
}
if q.Args != nil {
lines = append(lines, fmt.Sprintf(fmtLogArgs, q.Args))
}
if q.Err != nil {
lines = append(lines, fmt.Sprintf(fmtLogError, q.Err))
}
lines = append(lines, fmt.Sprintf(fmtLogTimeTaken, float64(q.End.UnixNano()-q.Start.UnixNano())/float64(1e9)))
return strings.Join(lines, "\n")
}
// Logger represents a logging collector. You can pass a logging collector to
// gosql.SetLogger(myCollector) to make it collect QueryStatus messages
// after executing a query.
type Logger interface {
Printf(format string, v ...interface{})
}
type defaultLogger struct {
logging bool
log Logger
}
func (d *defaultLogger) Log(m *QueryStatus, show bool) {
if d.logging || show {
d.log.Printf("\n\t%s\n\n", strings.Replace(m.String(), "\n", "\n\t", -1))
}
}
func (d *defaultLogger) SetLogging(logging bool) {
d.logging = logging
}
var logger = &defaultLogger{log: log.New(os.Stderr, "", log.LstdFlags)}
func SetLogger(l Logger) {
logger.log = l
}
//SetLogging set default logger
func SetLogging(logging bool) {
logger.logging = logging
}