-
Notifications
You must be signed in to change notification settings - Fork 27
/
logger.go
42 lines (35 loc) · 1007 Bytes
/
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
package godb
import (
"time"
"fmt"
)
const logPrefix = "SQL:"
// Logger is interface for custom logger
// Should implement `Println(v ...interface{})` method
type Logger interface {
Println(v ...interface{})
}
// SetLogger sets the logger for the given DB.
// By default there is no logger.
func (db *DB) SetLogger(logger Logger) {
db.logger = logger
}
// logPrintln is a wrapper for log.Logger.Println with the DB.logger
// as Logger.
func (db *DB) logPrintln(v ...interface{}) {
if db.logger != nil {
db.logger.Println(logPrefix, v)
}
}
// logExecution adds a log with a duration and SQL statement.
func (db *DB) logExecution(duration time.Duration, v ...interface{}) {
if db.logger != nil {
db.logger.Println(logPrefix, v, fmt.Sprintf("(Duration: %v)", duration))
}
}
// logExecution adds a log with a duration and SQL statement.
func (db *DB) logExecutionErr(err error, v ...interface{}) {
if db.logger != nil {
db.logger.Println(logPrefix, v, fmt.Sprintf("(ERROR: %v)", err))
}
}