2017-02-20 13:41:13 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-05-14 12:34:07 +05:30
|
|
|
package models
|
2017-02-20 13:41:13 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-05-14 12:34:07 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
|
2017-02-20 13:41:13 +05:30
|
|
|
"github.com/go-xorm/core"
|
|
|
|
)
|
|
|
|
|
|
|
|
// XORMLogBridge a logger bridge from Logger to xorm
|
|
|
|
type XORMLogBridge struct {
|
|
|
|
showSQL bool
|
2019-05-14 12:34:07 +05:30
|
|
|
logger *log.Logger
|
2017-02-20 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
2019-05-14 12:34:07 +05:30
|
|
|
// NewXORMLogger inits a log bridge for xorm
|
|
|
|
func NewXORMLogger(showSQL bool) core.ILogger {
|
|
|
|
return &XORMLogBridge{
|
2019-04-02 13:18:31 +05:30
|
|
|
showSQL: showSQL,
|
2019-05-14 12:34:07 +05:30
|
|
|
logger: log.GetLogger("xorm"),
|
2017-02-22 17:14:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// Log a message with defined skip and at logging level
|
2019-05-14 12:34:07 +05:30
|
|
|
func (l *XORMLogBridge) Log(skip int, level log.Level, format string, v ...interface{}) error {
|
|
|
|
return l.logger.Log(skip+1, level, format, v...)
|
2017-02-20 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Debug show debug log
|
|
|
|
func (l *XORMLogBridge) Debug(v ...interface{}) {
|
2019-06-13 01:11:28 +05:30
|
|
|
_ = l.Log(2, log.DEBUG, fmt.Sprint(v...))
|
2017-02-20 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Debugf show debug log
|
|
|
|
func (l *XORMLogBridge) Debugf(format string, v ...interface{}) {
|
2019-06-13 01:11:28 +05:30
|
|
|
_ = l.Log(2, log.DEBUG, format, v...)
|
2017-02-20 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Error show error log
|
|
|
|
func (l *XORMLogBridge) Error(v ...interface{}) {
|
2019-06-13 01:11:28 +05:30
|
|
|
_ = l.Log(2, log.ERROR, fmt.Sprint(v...))
|
2017-02-20 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Errorf show error log
|
|
|
|
func (l *XORMLogBridge) Errorf(format string, v ...interface{}) {
|
2019-06-13 01:11:28 +05:30
|
|
|
_ = l.Log(2, log.ERROR, format, v...)
|
2017-02-20 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Info show information level log
|
|
|
|
func (l *XORMLogBridge) Info(v ...interface{}) {
|
2019-06-13 01:11:28 +05:30
|
|
|
_ = l.Log(2, log.INFO, fmt.Sprint(v...))
|
2017-02-20 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Infof show information level log
|
|
|
|
func (l *XORMLogBridge) Infof(format string, v ...interface{}) {
|
2019-06-13 01:11:28 +05:30
|
|
|
_ = l.Log(2, log.INFO, format, v...)
|
2017-02-20 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
2017-03-15 06:22:01 +05:30
|
|
|
// Warn show warning log
|
2017-02-20 13:41:13 +05:30
|
|
|
func (l *XORMLogBridge) Warn(v ...interface{}) {
|
2019-06-13 01:11:28 +05:30
|
|
|
_ = l.Log(2, log.WARN, fmt.Sprint(v...))
|
2017-02-20 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Warnf show warnning log
|
|
|
|
func (l *XORMLogBridge) Warnf(format string, v ...interface{}) {
|
2019-06-13 01:11:28 +05:30
|
|
|
_ = l.Log(2, log.WARN, format, v...)
|
2017-02-20 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// Level get logger level
|
|
|
|
func (l *XORMLogBridge) Level() core.LogLevel {
|
2019-05-14 12:34:07 +05:30
|
|
|
switch l.logger.GetLevel() {
|
|
|
|
case log.TRACE, log.DEBUG:
|
2019-04-02 13:18:31 +05:30
|
|
|
return core.LOG_DEBUG
|
2019-05-14 12:34:07 +05:30
|
|
|
case log.INFO:
|
2019-04-02 13:18:31 +05:30
|
|
|
return core.LOG_INFO
|
2019-05-14 12:34:07 +05:30
|
|
|
case log.WARN:
|
2019-04-02 13:18:31 +05:30
|
|
|
return core.LOG_WARNING
|
2019-05-14 12:34:07 +05:30
|
|
|
case log.ERROR, log.CRITICAL:
|
2019-04-02 13:18:31 +05:30
|
|
|
return core.LOG_ERR
|
|
|
|
}
|
|
|
|
return core.LOG_OFF
|
2017-02-20 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// SetLevel set the logger level
|
|
|
|
func (l *XORMLogBridge) SetLevel(lvl core.LogLevel) {
|
2017-02-20 13:41:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// ShowSQL set if record SQL
|
|
|
|
func (l *XORMLogBridge) ShowSQL(show ...bool) {
|
|
|
|
if len(show) > 0 {
|
|
|
|
l.showSQL = show[0]
|
|
|
|
} else {
|
|
|
|
l.showSQL = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsShowSQL if record SQL
|
|
|
|
func (l *XORMLogBridge) IsShowSQL() bool {
|
|
|
|
return l.showSQL
|
|
|
|
}
|