orm_log.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package orm
  15. import (
  16. "database/sql"
  17. "fmt"
  18. "io"
  19. "log"
  20. "strings"
  21. "time"
  22. )
  23. // Log implement the log.Logger
  24. type Log struct {
  25. *log.Logger
  26. }
  27. // NewLog set io.Writer to create a Logger.
  28. func NewLog(out io.Writer) *Log {
  29. d := new(Log)
  30. d.Logger = log.New(out, "[ORM]", log.LstdFlags)
  31. return d
  32. }
  33. func debugLogQueies(alias *alias, operaton, query string, t time.Time, err error, args ...interface{}) {
  34. sub := time.Now().Sub(t) / 1e5
  35. elsp := float64(int(sub)) / 10.0
  36. flag := " OK"
  37. if err != nil {
  38. flag = "FAIL"
  39. }
  40. con := fmt.Sprintf(" -[Queries/%s] - [%s / %11s / %7.1fms] - [%s]", alias.Name, flag, operaton, elsp, query)
  41. cons := make([]string, 0, len(args))
  42. for _, arg := range args {
  43. cons = append(cons, fmt.Sprintf("%v", arg))
  44. }
  45. if len(cons) > 0 {
  46. con += fmt.Sprintf(" - `%s`", strings.Join(cons, "`, `"))
  47. }
  48. if err != nil {
  49. con += " - " + err.Error()
  50. }
  51. DebugLog.Println(con)
  52. }
  53. // statement query logger struct.
  54. // if dev mode, use stmtQueryLog, or use stmtQuerier.
  55. type stmtQueryLog struct {
  56. alias *alias
  57. query string
  58. stmt stmtQuerier
  59. }
  60. var _ stmtQuerier = new(stmtQueryLog)
  61. func (d *stmtQueryLog) Close() error {
  62. a := time.Now()
  63. err := d.stmt.Close()
  64. debugLogQueies(d.alias, "st.Close", d.query, a, err)
  65. return err
  66. }
  67. func (d *stmtQueryLog) Exec(args ...interface{}) (sql.Result, error) {
  68. a := time.Now()
  69. res, err := d.stmt.Exec(args...)
  70. debugLogQueies(d.alias, "st.Exec", d.query, a, err, args...)
  71. return res, err
  72. }
  73. func (d *stmtQueryLog) Query(args ...interface{}) (*sql.Rows, error) {
  74. a := time.Now()
  75. res, err := d.stmt.Query(args...)
  76. debugLogQueies(d.alias, "st.Query", d.query, a, err, args...)
  77. return res, err
  78. }
  79. func (d *stmtQueryLog) QueryRow(args ...interface{}) *sql.Row {
  80. a := time.Now()
  81. res := d.stmt.QueryRow(args...)
  82. debugLogQueies(d.alias, "st.QueryRow", d.query, a, nil, args...)
  83. return res
  84. }
  85. func newStmtQueryLog(alias *alias, stmt stmtQuerier, query string) stmtQuerier {
  86. d := new(stmtQueryLog)
  87. d.stmt = stmt
  88. d.alias = alias
  89. d.query = query
  90. return d
  91. }
  92. // database query logger struct.
  93. // if dev mode, use dbQueryLog, or use dbQuerier.
  94. type dbQueryLog struct {
  95. alias *alias
  96. db dbQuerier
  97. tx txer
  98. txe txEnder
  99. }
  100. var _ dbQuerier = new(dbQueryLog)
  101. var _ txer = new(dbQueryLog)
  102. var _ txEnder = new(dbQueryLog)
  103. func (d *dbQueryLog) Prepare(query string) (*sql.Stmt, error) {
  104. a := time.Now()
  105. stmt, err := d.db.Prepare(query)
  106. debugLogQueies(d.alias, "db.Prepare", query, a, err)
  107. return stmt, err
  108. }
  109. func (d *dbQueryLog) Exec(query string, args ...interface{}) (sql.Result, error) {
  110. a := time.Now()
  111. res, err := d.db.Exec(query, args...)
  112. debugLogQueies(d.alias, "db.Exec", query, a, err, args...)
  113. return res, err
  114. }
  115. func (d *dbQueryLog) Query(query string, args ...interface{}) (*sql.Rows, error) {
  116. a := time.Now()
  117. res, err := d.db.Query(query, args...)
  118. debugLogQueies(d.alias, "db.Query", query, a, err, args...)
  119. return res, err
  120. }
  121. func (d *dbQueryLog) QueryRow(query string, args ...interface{}) *sql.Row {
  122. a := time.Now()
  123. res := d.db.QueryRow(query, args...)
  124. debugLogQueies(d.alias, "db.QueryRow", query, a, nil, args...)
  125. return res
  126. }
  127. func (d *dbQueryLog) Begin() (*sql.Tx, error) {
  128. a := time.Now()
  129. tx, err := d.db.(txer).Begin()
  130. debugLogQueies(d.alias, "db.Begin", "START TRANSACTION", a, err)
  131. return tx, err
  132. }
  133. func (d *dbQueryLog) Commit() error {
  134. a := time.Now()
  135. err := d.db.(txEnder).Commit()
  136. debugLogQueies(d.alias, "tx.Commit", "COMMIT", a, err)
  137. return err
  138. }
  139. func (d *dbQueryLog) Rollback() error {
  140. a := time.Now()
  141. err := d.db.(txEnder).Rollback()
  142. debugLogQueies(d.alias, "tx.Rollback", "ROLLBACK", a, err)
  143. return err
  144. }
  145. func (d *dbQueryLog) SetDB(db dbQuerier) {
  146. d.db = db
  147. }
  148. func newDbQueryLog(alias *alias, db dbQuerier) dbQuerier {
  149. d := new(dbQueryLog)
  150. d.alias = alias
  151. d.db = db
  152. return d
  153. }