You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
4.4 KiB

  1. // Copyright 2015 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "fmt"
  7. "io"
  8. "log"
  9. "github.com/go-xorm/core"
  10. )
  11. // default log options
  12. const (
  13. DEFAULT_LOG_PREFIX = "[xorm]"
  14. DEFAULT_LOG_FLAG = log.Ldate | log.Lmicroseconds
  15. DEFAULT_LOG_LEVEL = core.LOG_DEBUG
  16. )
  17. var _ core.ILogger = DiscardLogger{}
  18. // DiscardLogger don't log implementation for core.ILogger
  19. type DiscardLogger struct{}
  20. // Debug empty implementation
  21. func (DiscardLogger) Debug(v ...interface{}) {}
  22. // Debugf empty implementation
  23. func (DiscardLogger) Debugf(format string, v ...interface{}) {}
  24. // Error empty implementation
  25. func (DiscardLogger) Error(v ...interface{}) {}
  26. // Errorf empty implementation
  27. func (DiscardLogger) Errorf(format string, v ...interface{}) {}
  28. // Info empty implementation
  29. func (DiscardLogger) Info(v ...interface{}) {}
  30. // Infof empty implementation
  31. func (DiscardLogger) Infof(format string, v ...interface{}) {}
  32. // Warn empty implementation
  33. func (DiscardLogger) Warn(v ...interface{}) {}
  34. // Warnf empty implementation
  35. func (DiscardLogger) Warnf(format string, v ...interface{}) {}
  36. // Level empty implementation
  37. func (DiscardLogger) Level() core.LogLevel {
  38. return core.LOG_UNKNOWN
  39. }
  40. // SetLevel empty implementation
  41. func (DiscardLogger) SetLevel(l core.LogLevel) {}
  42. // ShowSQL empty implementation
  43. func (DiscardLogger) ShowSQL(show ...bool) {}
  44. // IsShowSQL empty implementation
  45. func (DiscardLogger) IsShowSQL() bool {
  46. return false
  47. }
  48. // SimpleLogger is the default implment of core.ILogger
  49. type SimpleLogger struct {
  50. DEBUG *log.Logger
  51. ERR *log.Logger
  52. INFO *log.Logger
  53. WARN *log.Logger
  54. level core.LogLevel
  55. showSQL bool
  56. }
  57. var _ core.ILogger = &SimpleLogger{}
  58. // NewSimpleLogger use a special io.Writer as logger output
  59. func NewSimpleLogger(out io.Writer) *SimpleLogger {
  60. return NewSimpleLogger2(out, DEFAULT_LOG_PREFIX, DEFAULT_LOG_FLAG)
  61. }
  62. // NewSimpleLogger2 let you customrize your logger prefix and flag
  63. func NewSimpleLogger2(out io.Writer, prefix string, flag int) *SimpleLogger {
  64. return NewSimpleLogger3(out, prefix, flag, DEFAULT_LOG_LEVEL)
  65. }
  66. // NewSimpleLogger3 let you customrize your logger prefix and flag and logLevel
  67. func NewSimpleLogger3(out io.Writer, prefix string, flag int, l core.LogLevel) *SimpleLogger {
  68. return &SimpleLogger{
  69. DEBUG: log.New(out, fmt.Sprintf("%s [debug] ", prefix), flag),
  70. ERR: log.New(out, fmt.Sprintf("%s [error] ", prefix), flag),
  71. INFO: log.New(out, fmt.Sprintf("%s [info] ", prefix), flag),
  72. WARN: log.New(out, fmt.Sprintf("%s [warn] ", prefix), flag),
  73. level: l,
  74. }
  75. }
  76. // Error implement core.ILogger
  77. func (s *SimpleLogger) Error(v ...interface{}) {
  78. if s.level <= core.LOG_ERR {
  79. s.ERR.Output(2, fmt.Sprint(v...))
  80. }
  81. return
  82. }
  83. // Errorf implement core.ILogger
  84. func (s *SimpleLogger) Errorf(format string, v ...interface{}) {
  85. if s.level <= core.LOG_ERR {
  86. s.ERR.Output(2, fmt.Sprintf(format, v...))
  87. }
  88. return
  89. }
  90. // Debug implement core.ILogger
  91. func (s *SimpleLogger) Debug(v ...interface{}) {
  92. if s.level <= core.LOG_DEBUG {
  93. s.DEBUG.Output(2, fmt.Sprint(v...))
  94. }
  95. return
  96. }
  97. // Debugf implement core.ILogger
  98. func (s *SimpleLogger) Debugf(format string, v ...interface{}) {
  99. if s.level <= core.LOG_DEBUG {
  100. s.DEBUG.Output(2, fmt.Sprintf(format, v...))
  101. }
  102. return
  103. }
  104. // Info implement core.ILogger
  105. func (s *SimpleLogger) Info(v ...interface{}) {
  106. if s.level <= core.LOG_INFO {
  107. s.INFO.Output(2, fmt.Sprint(v...))
  108. }
  109. return
  110. }
  111. // Infof implement core.ILogger
  112. func (s *SimpleLogger) Infof(format string, v ...interface{}) {
  113. if s.level <= core.LOG_INFO {
  114. s.INFO.Output(2, fmt.Sprintf(format, v...))
  115. }
  116. return
  117. }
  118. // Warn implement core.ILogger
  119. func (s *SimpleLogger) Warn(v ...interface{}) {
  120. if s.level <= core.LOG_WARNING {
  121. s.WARN.Output(2, fmt.Sprint(v...))
  122. }
  123. return
  124. }
  125. // Warnf implement core.ILogger
  126. func (s *SimpleLogger) Warnf(format string, v ...interface{}) {
  127. if s.level <= core.LOG_WARNING {
  128. s.WARN.Output(2, fmt.Sprintf(format, v...))
  129. }
  130. return
  131. }
  132. // Level implement core.ILogger
  133. func (s *SimpleLogger) Level() core.LogLevel {
  134. return s.level
  135. }
  136. // SetLevel implement core.ILogger
  137. func (s *SimpleLogger) SetLevel(l core.LogLevel) {
  138. s.level = l
  139. return
  140. }
  141. // ShowSQL implement core.ILogger
  142. func (s *SimpleLogger) ShowSQL(show ...bool) {
  143. if len(show) == 0 {
  144. s.showSQL = true
  145. return
  146. }
  147. s.showSQL = show[0]
  148. }
  149. // IsShowSQL implement core.ILogger
  150. func (s *SimpleLogger) IsShowSQL() bool {
  151. return s.showSQL
  152. }