123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package logger
- import (
- "github.com/sirupsen/logrus"
- "xorm.io/xorm/log"
- )
- type XORMLogger struct {
- logger *Logger
- level log.LogLevel
- showSQL bool
- }
- func NewXORMLogger(logger *Logger) log.Logger {
- lg := &XORMLogger{logger: logger.WithField("lib", "xorm"), showSQL: true}
- level := log.LOG_INFO
- if logger.Level == logrus.WarnLevel {
- level = log.LOG_WARNING
- } else if logger.Level == logrus.DebugLevel {
- level = log.LOG_DEBUG
- } else if logger.Level == logrus.ErrorLevel {
- level = log.LOG_ERR
- } else if logger.Level == logrus.FatalLevel {
- level = log.LOG_OFF
- } else if logger.Level == logrus.PanicLevel {
- level = log.LOG_UNKNOWN
- }
- lg.level = level
- return lg
- }
- func (s *XORMLogger) printSql(v ...interface{}) {
- var sql, params interface{}
- if len(v) > 0 {
- sql = v[0]
- } else {
- sql = ""
- }
- if len(v) > 1 {
- params = v[1]
- } else {
- params = nil
- }
- args := []interface{}{"sql", "", 0, sql, params, MaxInt64}
- s.logger.Print(args...)
- }
- // Error implement core.ILogger
- func (s *XORMLogger) Error(v ...interface{}) {
- if s.level <= log.LOG_ERR {
- s.printSql(v...)
- }
- return
- }
- // Errorf implement core.ILogger
- func (s *XORMLogger) Errorf(format string, v ...interface{}) {
- if s.level <= log.LOG_ERR {
- s.printSql(v...)
- }
- return
- }
- // Debug implement core.ILogger
- func (s *XORMLogger) Debug(v ...interface{}) {
- if s.level <= log.LOG_DEBUG {
- s.printSql(v...)
- }
- return
- }
- // Debugf implement core.ILogger
- func (s *XORMLogger) Debugf(format string, v ...interface{}) {
- if s.level <= log.LOG_DEBUG {
- s.printSql(v...)
- }
- return
- }
- // Info implement core.ILogger
- func (s *XORMLogger) Info(v ...interface{}) {
- if s.level <= log.LOG_INFO {
- s.printSql(v...)
- }
- return
- }
- // Infof implement core.ILogger
- func (s *XORMLogger) Infof(format string, v ...interface{}) {
- if s.level <= log.LOG_INFO {
- s.printSql(v...)
- }
- return
- }
- // Warn implement core.ILogger
- func (s *XORMLogger) Warn(v ...interface{}) {
- if s.level <= log.LOG_WARNING {
- s.printSql(v...)
- }
- return
- }
- // Warnf implement core.ILogger
- func (s *XORMLogger) Warnf(format string, v ...interface{}) {
- if s.level <= log.LOG_WARNING {
- s.printSql(v...)
- }
- return
- }
- // Level implement core.ILogger
- func (s *XORMLogger) Level() log.LogLevel {
- return s.level
- }
- // SetLevel implement core.ILogger
- func (s *XORMLogger) SetLevel(l log.LogLevel) {
- s.level = l
- return
- }
- // ShowSQL implement core.ILogger
- func (s *XORMLogger) ShowSQL(show ...bool) {
- if len(show) == 0 {
- s.showSQL = true
- return
- }
- s.showSQL = show[0]
- }
- // IsShowSQL implement core.ILogger
- func (s *XORMLogger) IsShowSQL() bool {
- return s.showSQL
- }
|