1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-25 08:59:31 -05:00

Move xorm logger bridge from log to models so that log module could be a standalone package (#6944)

* move xorm logger bridge from log to models so that log module could be a standalone package

* fix tests

* save logger on xorm log bridge
This commit is contained in:
Lunny Xiao 2019-05-14 15:04:07 +08:00 committed by Lauris BH
parent 0e057eb033
commit 8b36f01f45
3 changed files with 25 additions and 35 deletions

View file

@ -2,11 +2,13 @@
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package log package models
import ( import (
"fmt" "fmt"
"code.gitea.io/gitea/modules/log"
"github.com/go-xorm/core" "github.com/go-xorm/core"
) )
@ -14,80 +16,72 @@ import (
type XORMLogBridge struct { type XORMLogBridge struct {
showSQL bool showSQL bool
level core.LogLevel level core.LogLevel
logger *log.Logger
} }
var ( // NewXORMLogger inits a log bridge for xorm
// XORMLogger the logger for xorm func NewXORMLogger(showSQL bool) core.ILogger {
XORMLogger *XORMLogBridge return &XORMLogBridge{
)
// InitXORMLogger inits a log bridge for xorm
func InitXORMLogger(showSQL bool) {
XORMLogger = &XORMLogBridge{
showSQL: showSQL, showSQL: showSQL,
logger: log.GetLogger("xorm"),
} }
} }
// GetGiteaLevel returns the minimum Gitea logger level
func (l *XORMLogBridge) GetGiteaLevel() Level {
return GetLogger("xorm").GetLevel()
}
// Log a message with defined skip and at logging level // Log a message with defined skip and at logging level
func (l *XORMLogBridge) Log(skip int, level Level, format string, v ...interface{}) error { func (l *XORMLogBridge) Log(skip int, level log.Level, format string, v ...interface{}) error {
return GetLogger("xorm").Log(skip+1, level, format, v...) return l.logger.Log(skip+1, level, format, v...)
} }
// Debug show debug log // Debug show debug log
func (l *XORMLogBridge) Debug(v ...interface{}) { func (l *XORMLogBridge) Debug(v ...interface{}) {
l.Log(2, DEBUG, fmt.Sprint(v...)) l.Log(2, log.DEBUG, fmt.Sprint(v...))
} }
// Debugf show debug log // Debugf show debug log
func (l *XORMLogBridge) Debugf(format string, v ...interface{}) { func (l *XORMLogBridge) Debugf(format string, v ...interface{}) {
l.Log(2, DEBUG, format, v...) l.Log(2, log.DEBUG, format, v...)
} }
// Error show error log // Error show error log
func (l *XORMLogBridge) Error(v ...interface{}) { func (l *XORMLogBridge) Error(v ...interface{}) {
l.Log(2, ERROR, fmt.Sprint(v...)) l.Log(2, log.ERROR, fmt.Sprint(v...))
} }
// Errorf show error log // Errorf show error log
func (l *XORMLogBridge) Errorf(format string, v ...interface{}) { func (l *XORMLogBridge) Errorf(format string, v ...interface{}) {
l.Log(2, ERROR, format, v...) l.Log(2, log.ERROR, format, v...)
} }
// Info show information level log // Info show information level log
func (l *XORMLogBridge) Info(v ...interface{}) { func (l *XORMLogBridge) Info(v ...interface{}) {
l.Log(2, INFO, fmt.Sprint(v...)) l.Log(2, log.INFO, fmt.Sprint(v...))
} }
// Infof show information level log // Infof show information level log
func (l *XORMLogBridge) Infof(format string, v ...interface{}) { func (l *XORMLogBridge) Infof(format string, v ...interface{}) {
l.Log(2, INFO, format, v...) l.Log(2, log.INFO, format, v...)
} }
// Warn show warning log // Warn show warning log
func (l *XORMLogBridge) Warn(v ...interface{}) { func (l *XORMLogBridge) Warn(v ...interface{}) {
l.Log(2, WARN, fmt.Sprint(v...)) l.Log(2, log.WARN, fmt.Sprint(v...))
} }
// Warnf show warnning log // Warnf show warnning log
func (l *XORMLogBridge) Warnf(format string, v ...interface{}) { func (l *XORMLogBridge) Warnf(format string, v ...interface{}) {
l.Log(2, WARN, format, v...) l.Log(2, log.WARN, format, v...)
} }
// Level get logger level // Level get logger level
func (l *XORMLogBridge) Level() core.LogLevel { func (l *XORMLogBridge) Level() core.LogLevel {
switch l.GetGiteaLevel() { switch l.logger.GetLevel() {
case TRACE, DEBUG: case log.TRACE, log.DEBUG:
return core.LOG_DEBUG return core.LOG_DEBUG
case INFO: case log.INFO:
return core.LOG_INFO return core.LOG_INFO
case WARN: case log.WARN:
return core.LOG_WARNING return core.LOG_WARNING
case ERROR, CRITICAL: case log.ERROR, log.CRITICAL:
return core.LOG_ERR return core.LOG_ERR
} }
return core.LOG_OFF return core.LOG_OFF

View file

@ -15,7 +15,6 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
// Needed for the MySQL driver // Needed for the MySQL driver
@ -261,7 +260,7 @@ func NewTestEngine(x *xorm.Engine) (err error) {
} }
x.SetMapper(core.GonicMapper{}) x.SetMapper(core.GonicMapper{})
x.SetLogger(log.XORMLogger) x.SetLogger(NewXORMLogger(!setting.ProdMode))
x.ShowSQL(!setting.ProdMode) x.ShowSQL(!setting.ProdMode)
return x.StoreEngine("InnoDB").Sync2(tables...) return x.StoreEngine("InnoDB").Sync2(tables...)
} }
@ -276,7 +275,7 @@ func SetEngine() (err error) {
x.SetMapper(core.GonicMapper{}) x.SetMapper(core.GonicMapper{})
// WARNING: for serv command, MUST remove the output to os.stdout, // WARNING: for serv command, MUST remove the output to os.stdout,
// so use log file to instead print to stdout. // so use log file to instead print to stdout.
x.SetLogger(log.XORMLogger) x.SetLogger(NewXORMLogger(setting.LogSQL))
x.ShowSQL(setting.LogSQL) x.ShowSQL(setting.LogSQL)
return nil return nil
} }

View file

@ -295,8 +295,5 @@ func NewXORMLogService(disableConsole bool) {
Cfg.Section("log").Key("XORM").MustString(",") Cfg.Section("log").Key("XORM").MustString(",")
generateNamedLogger("xorm", options) generateNamedLogger("xorm", options)
log.InitXORMLogger(LogSQL)
} else {
log.InitXORMLogger(false)
} }
} }