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.

143 lines
3.0 KiB

  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 log
  5. import (
  6. "fmt"
  7. "github.com/go-xorm/core"
  8. )
  9. // XORMLogBridge a logger bridge from Logger to xorm
  10. type XORMLogBridge struct {
  11. loggers []*Logger
  12. showSQL bool
  13. level core.LogLevel
  14. }
  15. var (
  16. // XORMLogger the logger for xorm
  17. XORMLogger *XORMLogBridge
  18. )
  19. // DiscardXORMLogger inits a blank logger for xorm
  20. func DiscardXORMLogger() {
  21. XORMLogger = &XORMLogBridge{
  22. showSQL: false,
  23. }
  24. }
  25. // NewXORMLogger generate logger for xorm FIXME: configable
  26. func NewXORMLogger(bufferlen int64, mode, config string) {
  27. logger := newLogger(bufferlen)
  28. logger.SetLogger(mode, config)
  29. if XORMLogger == nil {
  30. XORMLogger = &XORMLogBridge{
  31. showSQL: true,
  32. }
  33. }
  34. XORMLogger.loggers = append(XORMLogger.loggers, logger)
  35. }
  36. func (l *XORMLogBridge) writerMsg(skip, level int, msg string) error {
  37. for _, logger := range l.loggers {
  38. if err := logger.writerMsg(skip, level, msg); err != nil {
  39. return err
  40. }
  41. }
  42. return nil
  43. }
  44. // Debug show debug log
  45. func (l *XORMLogBridge) Debug(v ...interface{}) {
  46. if l.level <= core.LOG_DEBUG {
  47. msg := fmt.Sprint(v...)
  48. l.writerMsg(0, DEBUG, "[D]"+msg)
  49. }
  50. }
  51. // Debugf show debug log
  52. func (l *XORMLogBridge) Debugf(format string, v ...interface{}) {
  53. if l.level <= core.LOG_DEBUG {
  54. for _, logger := range l.loggers {
  55. logger.Debug(format, v...)
  56. }
  57. }
  58. }
  59. // Error show error log
  60. func (l *XORMLogBridge) Error(v ...interface{}) {
  61. if l.level <= core.LOG_ERR {
  62. msg := fmt.Sprint(v...)
  63. l.writerMsg(0, ERROR, "[E]"+msg)
  64. }
  65. }
  66. // Errorf show error log
  67. func (l *XORMLogBridge) Errorf(format string, v ...interface{}) {
  68. if l.level <= core.LOG_ERR {
  69. for _, logger := range l.loggers {
  70. logger.Error(0, format, v...)
  71. }
  72. }
  73. }
  74. // Info show information level log
  75. func (l *XORMLogBridge) Info(v ...interface{}) {
  76. if l.level <= core.LOG_INFO {
  77. msg := fmt.Sprint(v...)
  78. l.writerMsg(0, INFO, "[I]"+msg)
  79. }
  80. }
  81. // Infof show information level log
  82. func (l *XORMLogBridge) Infof(format string, v ...interface{}) {
  83. if l.level <= core.LOG_INFO {
  84. for _, logger := range l.loggers {
  85. logger.Info(format, v...)
  86. }
  87. }
  88. }
  89. // Warn show warning log
  90. func (l *XORMLogBridge) Warn(v ...interface{}) {
  91. if l.level <= core.LOG_WARNING {
  92. msg := fmt.Sprint(v...)
  93. l.writerMsg(0, WARN, "[W] "+msg)
  94. }
  95. }
  96. // Warnf show warnning log
  97. func (l *XORMLogBridge) Warnf(format string, v ...interface{}) {
  98. if l.level <= core.LOG_WARNING {
  99. for _, logger := range l.loggers {
  100. logger.Warn(format, v...)
  101. }
  102. }
  103. }
  104. // Level get logger level
  105. func (l *XORMLogBridge) Level() core.LogLevel {
  106. return l.level
  107. }
  108. // SetLevel set logger level
  109. func (l *XORMLogBridge) SetLevel(level core.LogLevel) {
  110. l.level = level
  111. }
  112. // ShowSQL set if record SQL
  113. func (l *XORMLogBridge) ShowSQL(show ...bool) {
  114. if len(show) > 0 {
  115. l.showSQL = show[0]
  116. } else {
  117. l.showSQL = true
  118. }
  119. }
  120. // IsShowSQL if record SQL
  121. func (l *XORMLogBridge) IsShowSQL() bool {
  122. return l.showSQL
  123. }