http urls monitor.

logger.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (c) 2012-present The upper.io/db authors. All rights reserved.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining
  4. // a copy of this software and associated documentation files (the
  5. // "Software"), to deal in the Software without restriction, including
  6. // without limitation the rights to use, copy, modify, merge, publish,
  7. // distribute, sublicense, and/or sell copies of the Software, and to
  8. // permit persons to whom the Software is furnished to do so, subject to
  9. // the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be
  12. // included in all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. package db
  22. import (
  23. "context"
  24. "fmt"
  25. "log"
  26. "regexp"
  27. "strings"
  28. "time"
  29. )
  30. const (
  31. fmtLogSessID = `Session ID: %05d`
  32. fmtLogTxID = `Transaction ID: %05d`
  33. fmtLogQuery = `Query: %s`
  34. fmtLogArgs = `Arguments: %#v`
  35. fmtLogRowsAffected = `Rows affected: %d`
  36. fmtLogLastInsertID = `Last insert ID: %d`
  37. fmtLogError = `Error: %v`
  38. fmtLogTimeTaken = `Time taken: %0.5fs`
  39. fmtLogContext = `Context: %v`
  40. )
  41. var (
  42. reInvisibleChars = regexp.MustCompile(`[\s\r\n\t]+`)
  43. reColumnCompareExclude = regexp.MustCompile(`[^a-zA-Z0-9]`)
  44. )
  45. // QueryStatus represents the status of a query after being executed.
  46. type QueryStatus struct {
  47. SessID uint64
  48. TxID uint64
  49. RowsAffected *int64
  50. LastInsertID *int64
  51. Query string
  52. Args []interface{}
  53. Err error
  54. Start time.Time
  55. End time.Time
  56. Context context.Context
  57. }
  58. // String returns a formatted log message.
  59. func (q *QueryStatus) String() string {
  60. lines := make([]string, 0, 8)
  61. if q.SessID > 0 {
  62. lines = append(lines, fmt.Sprintf(fmtLogSessID, q.SessID))
  63. }
  64. if q.TxID > 0 {
  65. lines = append(lines, fmt.Sprintf(fmtLogTxID, q.TxID))
  66. }
  67. if query := q.Query; query != "" {
  68. query = reInvisibleChars.ReplaceAllString(query, ` `)
  69. query = strings.TrimSpace(query)
  70. lines = append(lines, fmt.Sprintf(fmtLogQuery, query))
  71. }
  72. if len(q.Args) > 0 {
  73. lines = append(lines, fmt.Sprintf(fmtLogArgs, q.Args))
  74. }
  75. if q.RowsAffected != nil {
  76. lines = append(lines, fmt.Sprintf(fmtLogRowsAffected, *q.RowsAffected))
  77. }
  78. if q.LastInsertID != nil {
  79. lines = append(lines, fmt.Sprintf(fmtLogLastInsertID, *q.LastInsertID))
  80. }
  81. if q.Err != nil {
  82. lines = append(lines, fmt.Sprintf(fmtLogError, q.Err))
  83. }
  84. lines = append(lines, fmt.Sprintf(fmtLogTimeTaken, float64(q.End.UnixNano()-q.Start.UnixNano())/float64(1e9)))
  85. if q.Context != nil {
  86. lines = append(lines, fmt.Sprintf(fmtLogContext, q.Context))
  87. }
  88. return strings.Join(lines, "\n")
  89. }
  90. // EnvEnableDebug can be used by adapters to determine if the user has enabled
  91. // debugging.
  92. //
  93. // If the user sets the `UPPERIO_DB_DEBUG` environment variable to a
  94. // non-empty value, all generated statements will be printed at runtime to
  95. // the standard logger.
  96. //
  97. // Example:
  98. //
  99. // UPPERIO_DB_DEBUG=1 go test
  100. //
  101. // UPPERIO_DB_DEBUG=1 ./go-program
  102. const (
  103. EnvEnableDebug = `UPPERIO_DB_DEBUG`
  104. )
  105. // Logger represents a logging collector. You can pass a logging collector to
  106. // db.DefaultSettings.SetLogger(myCollector) to make it collect db.QueryStatus messages
  107. // after executing a query.
  108. type Logger interface {
  109. Log(*QueryStatus)
  110. }
  111. type defaultLogger struct {
  112. }
  113. func (lg *defaultLogger) Log(m *QueryStatus) {
  114. log.Printf("\n\t%s\n\n", strings.Replace(m.String(), "\n", "\n\t", -1))
  115. }
  116. var _ = Logger(&defaultLogger{})
  117. func init() {
  118. if envEnabled(EnvEnableDebug) {
  119. DefaultSettings.SetLogging(true)
  120. }
  121. }