本站源代码
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.

106 lines
2.4KB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/modules/log"
  8. "xorm.io/core"
  9. )
  10. // XORMLogBridge a logger bridge from Logger to xorm
  11. type XORMLogBridge struct {
  12. showSQL bool
  13. logger *log.Logger
  14. }
  15. // NewXORMLogger inits a log bridge for xorm
  16. func NewXORMLogger(showSQL bool) core.ILogger {
  17. return &XORMLogBridge{
  18. showSQL: showSQL,
  19. logger: log.GetLogger("xorm"),
  20. }
  21. }
  22. // Log a message with defined skip and at logging level
  23. func (l *XORMLogBridge) Log(skip int, level log.Level, format string, v ...interface{}) error {
  24. return l.logger.Log(skip+1, level, format, v...)
  25. }
  26. // Debug show debug log
  27. func (l *XORMLogBridge) Debug(v ...interface{}) {
  28. _ = l.Log(2, log.DEBUG, fmt.Sprint(v...))
  29. }
  30. // Debugf show debug log
  31. func (l *XORMLogBridge) Debugf(format string, v ...interface{}) {
  32. _ = l.Log(2, log.DEBUG, format, v...)
  33. }
  34. // Error show error log
  35. func (l *XORMLogBridge) Error(v ...interface{}) {
  36. _ = l.Log(2, log.ERROR, fmt.Sprint(v...))
  37. }
  38. // Errorf show error log
  39. func (l *XORMLogBridge) Errorf(format string, v ...interface{}) {
  40. _ = l.Log(2, log.ERROR, format, v...)
  41. }
  42. // Info show information level log
  43. func (l *XORMLogBridge) Info(v ...interface{}) {
  44. _ = l.Log(2, log.INFO, fmt.Sprint(v...))
  45. }
  46. // Infof show information level log
  47. func (l *XORMLogBridge) Infof(format string, v ...interface{}) {
  48. _ = l.Log(2, log.INFO, format, v...)
  49. }
  50. // Warn show warning log
  51. func (l *XORMLogBridge) Warn(v ...interface{}) {
  52. _ = l.Log(2, log.WARN, fmt.Sprint(v...))
  53. }
  54. // Warnf show warnning log
  55. func (l *XORMLogBridge) Warnf(format string, v ...interface{}) {
  56. _ = l.Log(2, log.WARN, format, v...)
  57. }
  58. // Level get logger level
  59. func (l *XORMLogBridge) Level() core.LogLevel {
  60. switch l.logger.GetLevel() {
  61. case log.TRACE, log.DEBUG:
  62. return core.LOG_DEBUG
  63. case log.INFO:
  64. return core.LOG_INFO
  65. case log.WARN:
  66. return core.LOG_WARNING
  67. case log.ERROR, log.CRITICAL:
  68. return core.LOG_ERR
  69. }
  70. return core.LOG_OFF
  71. }
  72. // SetLevel set the logger level
  73. func (l *XORMLogBridge) SetLevel(lvl core.LogLevel) {
  74. }
  75. // ShowSQL set if record SQL
  76. func (l *XORMLogBridge) ShowSQL(show ...bool) {
  77. if len(show) > 0 {
  78. l.showSQL = show[0]
  79. } else {
  80. l.showSQL = true
  81. }
  82. }
  83. // IsShowSQL if record SQL
  84. func (l *XORMLogBridge) IsShowSQL() bool {
  85. return l.showSQL
  86. }
上海开阖软件有限公司 沪ICP备12045867号-1