-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
99 lines (94 loc) · 2.21 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
package surf
import (
"fmt"
"gopkg.in/guregu/null.v3"
"io"
"strconv"
"strings"
"time"
)
var (
loggingEnabled = false
loggingWriter io.Writer
)
// SetLogging adjusts the configuration for logging. You can enable
// and disable the logging here. By default, logging is disabled.
//
// Most calls to this function will be called like SetLogging(true, os.Stdout)
func SetLogging(enabled bool, writer io.Writer) {
loggingEnabled = enabled
loggingWriter = writer
}
// printQuery prints a query if the user has enabled logging
func PrintSqlQuery(query string, args ...interface{}) {
if loggingEnabled {
for i, arg := range args {
query = strings.Replace(query, "$"+strconv.Itoa(i+1), pointerToLogString(arg), 1)
}
fmt.Fprint(loggingWriter, query)
}
}
// pointerToLogString converts a value pointer to the string
// that should be logged for it
func pointerToLogString(pointer interface{}) string {
switch v := pointer.(type) {
case *string:
return "'" + *v + "'"
case *float32:
return strconv.FormatFloat(float64(*v), 'f', -1, 32)
case *float64:
return strconv.FormatFloat(*v, 'f', -1, 64)
case *bool:
return strconv.FormatBool(*v)
case *int:
return strconv.Itoa(*v)
case *int8:
return strconv.FormatInt(int64(*v), 10)
case *int16:
return strconv.FormatInt(int64(*v), 10)
case *int32:
return strconv.FormatInt(int64(*v), 10)
case *int64:
return strconv.FormatInt(*v, 10)
case *uint:
return strconv.FormatUint(uint64(*v), 10)
case *uint8:
return strconv.FormatUint(uint64(*v), 10)
case *uint16:
return strconv.FormatUint(uint64(*v), 10)
case *uint32:
return strconv.FormatUint(uint64(*v), 10)
case *uint64:
return strconv.FormatUint(*v, 10)
case *time.Time:
return "'" + (*v).Format(time.RFC3339) + "'"
case *null.Int:
if v.Valid {
return strconv.FormatInt(v.Int64, 10)
}
break
case *null.String:
if v.Valid {
return "'" + v.String + "'"
}
break
case *null.Bool:
if v.Valid {
return strconv.FormatBool(v.Bool)
}
break
case *null.Float:
if v.Valid {
return strconv.FormatFloat(v.Float64, 'f', -1, 64)
}
break
case *null.Time:
if v.Valid {
return "'" + v.Time.Format(time.RFC3339) + "'"
}
break
default:
return fmt.Sprintf("%v", v)
}
return "null"
}