Browse Source

Log mysql errors too

Vova Tkach 6 years ago
parent
commit
406b47a416
3 changed files with 15 additions and 11 deletions
  1. 7 3
      engine/sqlw/log.go
  2. 4 4
      engine/sqlw/sqlw.go
  3. 4 4
      engine/sqlw/txw.go

+ 7 - 3
engine/sqlw/log.go

@@ -10,7 +10,7 @@ import (
 	"golang-fave/consts"
 )
 
-func log(query string, s time.Time, transaction bool) {
+func log(query string, s time.Time, e error, transaction bool) {
 	color := "0;33"
 	if transaction {
 		color = "1;33"
@@ -22,9 +22,13 @@ func log(query string, s time.Time, transaction bool) {
 	if reg, err := regexp.Compile("[\\s\\t]+;$"); err == nil {
 		msg = reg.ReplaceAllString(msg, ";")
 	}
+	eStr := " (nil) "
+	if e != nil {
+		eStr = " (" + e.Error() + ") "
+	}
 	if consts.IS_WIN {
-		fmt.Fprintln(os.Stdout, "[SQL] "+msg+fmt.Sprintf(" %.3f ms", time.Now().Sub(s).Seconds()))
+		fmt.Fprintln(os.Stdout, "[SQL] "+msg+eStr+fmt.Sprintf(" %.3f ms", time.Now().Sub(s).Seconds()))
 	} else {
-		fmt.Fprintln(os.Stdout, "\033["+color+"m[SQL] "+msg+fmt.Sprintf(" %.3f ms", time.Now().Sub(s).Seconds())+"\033[0m")
+		fmt.Fprintln(os.Stdout, "\033["+color+"m[SQL] "+msg+eStr+fmt.Sprintf(" %.3f ms", time.Now().Sub(s).Seconds())+"\033[0m")
 	}
 }

+ 4 - 4
engine/sqlw/sqlw.go

@@ -50,7 +50,7 @@ func (this *DB) QueryRow(query string, args ...interface{}) *sql.Row {
 	if consts.ParamDebug {
 		s := time.Now()
 		r := this.db.QueryRow(query, args...)
-		log(query, s, false)
+		log(query, s, nil, false)
 		return r
 	}
 	return this.db.QueryRow(query, args...)
@@ -63,7 +63,7 @@ func (this *DB) Begin() (*Tx, error) {
 	}
 	if consts.ParamDebug {
 		s := time.Now()
-		log("[TX] TRANSACTION START", s, true)
+		log("[TX] TRANSACTION START", s, err, true)
 		return &Tx{tx, s}, err
 	}
 	return &Tx{tx, time.Now()}, err
@@ -73,7 +73,7 @@ func (this *DB) Query(query string, args ...interface{}) (*sql.Rows, error) {
 	if consts.ParamDebug {
 		s := time.Now()
 		r, e := this.db.Query(query, args...)
-		log(query, s, false)
+		log(query, s, e, false)
 		return r, e
 	}
 	return this.db.Query(query, args...)
@@ -83,7 +83,7 @@ func (this *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
 	if consts.ParamDebug {
 		s := time.Now()
 		r, e := this.db.Exec(query, args...)
-		log(query, s, false)
+		log(query, s, e, false)
 		return r, e
 	}
 	return this.db.Exec(query, args...)

+ 4 - 4
engine/sqlw/txw.go

@@ -17,7 +17,7 @@ type Tx struct {
 func (this *Tx) Rollback() error {
 	if consts.ParamDebug {
 		err := this.tx.Rollback()
-		log("[TX] TRANSACTION END (Rollback)", this.s, true)
+		log("[TX] TRANSACTION END (Rollback)", this.s, nil, true)
 		return err
 	}
 	return this.tx.Rollback()
@@ -26,7 +26,7 @@ func (this *Tx) Rollback() error {
 func (this *Tx) Commit() error {
 	if consts.ParamDebug {
 		err := this.tx.Commit()
-		log("[TX] TRANSACTION END (Commit)", this.s, true)
+		log("[TX] TRANSACTION END (Commit)", this.s, err, true)
 		return err
 	}
 	return this.tx.Commit()
@@ -36,7 +36,7 @@ func (this *Tx) Exec(query string, args ...interface{}) (sql.Result, error) {
 	if consts.ParamDebug {
 		s := time.Now()
 		r, e := this.tx.Exec(query, args...)
-		log("[TX] "+query, s, true)
+		log("[TX] "+query, s, e, true)
 		return r, e
 	}
 	return this.tx.Exec(query, args...)
@@ -46,7 +46,7 @@ func (this *Tx) QueryRow(query string, args ...interface{}) *sql.Row {
 	if consts.ParamDebug {
 		s := time.Now()
 		r := this.tx.QueryRow(query, args...)
-		log("[TX] "+query, s, true)
+		log("[TX] "+query, s, nil, true)
 		return r
 	}
 	return this.tx.QueryRow(query, args...)