2
0

Add *Conn.SetLogLevel

Allow changing log level after connection is established. Because
log level and loggers can be set independently, it is now possible
to have a log level above none when there is a nil logger. This
means all log statements need to check for nil logger and an
appropriate log level. This check has been factored out into
*Conn.shouldLog.
This commit is contained in:
Jack Christensen
2016-02-13 10:13:10 -06:00
parent cffae7ff5d
commit 0f7bf19387
4 changed files with 120 additions and 36 deletions
+4 -4
View File
@@ -52,7 +52,7 @@ type Rows struct {
sql string
args []interface{}
log func(lvl int, msg string, ctx ...interface{})
logLevel *int
shouldLog func(lvl int) bool
unlockConn bool
}
@@ -78,11 +78,11 @@ func (rows *Rows) close() {
rows.closed = true
if rows.err == nil {
if *rows.logLevel >= LogLevelInfo {
if rows.shouldLog(LogLevelInfo) {
endTime := time.Now()
rows.log(LogLevelInfo, "Query", "sql", rows.sql, "args", logQueryArgs(rows.args), "time", endTime.Sub(rows.startTime), "rowCount", rows.rowCount)
}
} else if *rows.logLevel >= LogLevelError {
} else if rows.shouldLog(LogLevelError) {
rows.log(LogLevelError, "Query", "sql", rows.sql, "args", logQueryArgs(rows.args))
}
}
@@ -474,7 +474,7 @@ func (rows *Rows) Values() ([]interface{}, error) {
// from Query and handle it in *Rows.
func (c *Conn) Query(sql string, args ...interface{}) (*Rows, error) {
c.lastActivityTime = time.Now()
rows := &Rows{conn: c, startTime: c.lastActivityTime, sql: sql, args: args, log: c.log, logLevel: &c.logLevel}
rows := &Rows{conn: c, startTime: c.lastActivityTime, sql: sql, args: args, log: c.log, shouldLog: c.shouldLog}
if err := c.lock(); err != nil {
rows.abort(err)