xorm_logger.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package logger
  2. import (
  3. "github.com/sirupsen/logrus"
  4. "xorm.io/xorm/log"
  5. )
  6. type XORMLogger struct {
  7. logger *Logger
  8. level log.LogLevel
  9. showSQL bool
  10. }
  11. func NewXORMLogger(logger *Logger) log.Logger {
  12. lg := &XORMLogger{logger: logger.WithField("lib", "xorm"), showSQL: true}
  13. level := log.LOG_INFO
  14. if logger.Level == logrus.WarnLevel {
  15. level = log.LOG_WARNING
  16. } else if logger.Level == logrus.DebugLevel {
  17. level = log.LOG_DEBUG
  18. } else if logger.Level == logrus.ErrorLevel {
  19. level = log.LOG_ERR
  20. } else if logger.Level == logrus.FatalLevel {
  21. level = log.LOG_OFF
  22. } else if logger.Level == logrus.PanicLevel {
  23. level = log.LOG_UNKNOWN
  24. }
  25. lg.level = level
  26. return lg
  27. }
  28. func (s *XORMLogger) printSql(v ...interface{}) {
  29. var sql, params interface{}
  30. if len(v) > 0 {
  31. sql = v[0]
  32. } else {
  33. sql = ""
  34. }
  35. if len(v) > 1 {
  36. params = v[1]
  37. } else {
  38. params = nil
  39. }
  40. args := []interface{}{"sql", "", 0, sql, params, MaxInt64}
  41. s.logger.Print(args...)
  42. }
  43. // Error implement core.ILogger
  44. func (s *XORMLogger) Error(v ...interface{}) {
  45. if s.level <= log.LOG_ERR {
  46. s.printSql(v...)
  47. }
  48. return
  49. }
  50. // Errorf implement core.ILogger
  51. func (s *XORMLogger) Errorf(format string, v ...interface{}) {
  52. if s.level <= log.LOG_ERR {
  53. s.printSql(v...)
  54. }
  55. return
  56. }
  57. // Debug implement core.ILogger
  58. func (s *XORMLogger) Debug(v ...interface{}) {
  59. if s.level <= log.LOG_DEBUG {
  60. s.printSql(v...)
  61. }
  62. return
  63. }
  64. // Debugf implement core.ILogger
  65. func (s *XORMLogger) Debugf(format string, v ...interface{}) {
  66. if s.level <= log.LOG_DEBUG {
  67. s.printSql(v...)
  68. }
  69. return
  70. }
  71. // Info implement core.ILogger
  72. func (s *XORMLogger) Info(v ...interface{}) {
  73. if s.level <= log.LOG_INFO {
  74. s.printSql(v...)
  75. }
  76. return
  77. }
  78. // Infof implement core.ILogger
  79. func (s *XORMLogger) Infof(format string, v ...interface{}) {
  80. if s.level <= log.LOG_INFO {
  81. s.printSql(v...)
  82. }
  83. return
  84. }
  85. // Warn implement core.ILogger
  86. func (s *XORMLogger) Warn(v ...interface{}) {
  87. if s.level <= log.LOG_WARNING {
  88. s.printSql(v...)
  89. }
  90. return
  91. }
  92. // Warnf implement core.ILogger
  93. func (s *XORMLogger) Warnf(format string, v ...interface{}) {
  94. if s.level <= log.LOG_WARNING {
  95. s.printSql(v...)
  96. }
  97. return
  98. }
  99. // Level implement core.ILogger
  100. func (s *XORMLogger) Level() log.LogLevel {
  101. return s.level
  102. }
  103. // SetLevel implement core.ILogger
  104. func (s *XORMLogger) SetLevel(l log.LogLevel) {
  105. s.level = l
  106. return
  107. }
  108. // ShowSQL implement core.ILogger
  109. func (s *XORMLogger) ShowSQL(show ...bool) {
  110. if len(show) == 0 {
  111. s.showSQL = true
  112. return
  113. }
  114. s.showSQL = show[0]
  115. }
  116. // IsShowSQL implement core.ILogger
  117. func (s *XORMLogger) IsShowSQL() bool {
  118. return s.showSQL
  119. }