diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-09-19 19:49:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-19 19:49:59 +0800 |
commit | a4bfef265d9e512830350635a0489c2cdcd6508f (patch) | |
tree | 1e3c2ec94276dfcb2f8ba73a2ac075ba39c4a34a /models/db/log.go | |
parent | 462306e263db5a809dbe2cdf62e99307aeff28de (diff) | |
download | gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.tar.gz gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.zip |
Move db related basic functions to models/db (#17075)
* Move db related basic functions to models/db
* Fix lint
* Fix lint
* Fix test
* Fix lint
* Fix lint
* revert unnecessary change
* Fix test
* Fix wrong replace string
* Use *Context
* Correct committer spelling and fix wrong replaced words
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models/db/log.go')
-rw-r--r-- | models/db/log.go | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/models/db/log.go b/models/db/log.go new file mode 100644 index 0000000000..f9febf440e --- /dev/null +++ b/models/db/log.go @@ -0,0 +1,107 @@ +// 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. + +package db + +import ( + "fmt" + + "code.gitea.io/gitea/modules/log" + + xormlog "xorm.io/xorm/log" +) + +// XORMLogBridge a logger bridge from Logger to xorm +type XORMLogBridge struct { + showSQL bool + logger log.Logger +} + +// NewXORMLogger inits a log bridge for xorm +func NewXORMLogger(showSQL bool) xormlog.Logger { + return &XORMLogBridge{ + showSQL: showSQL, + logger: log.GetLogger("xorm"), + } +} + +const stackLevel = 8 + +// Log a message with defined skip and at logging level +func (l *XORMLogBridge) Log(skip int, level log.Level, format string, v ...interface{}) error { + return l.logger.Log(skip+1, level, format, v...) +} + +// Debug show debug log +func (l *XORMLogBridge) Debug(v ...interface{}) { + _ = l.Log(stackLevel, log.DEBUG, fmt.Sprint(v...)) +} + +// Debugf show debug log +func (l *XORMLogBridge) Debugf(format string, v ...interface{}) { + _ = l.Log(stackLevel, log.DEBUG, format, v...) +} + +// Error show error log +func (l *XORMLogBridge) Error(v ...interface{}) { + _ = l.Log(stackLevel, log.ERROR, fmt.Sprint(v...)) +} + +// Errorf show error log +func (l *XORMLogBridge) Errorf(format string, v ...interface{}) { + _ = l.Log(stackLevel, log.ERROR, format, v...) +} + +// Info show information level log +func (l *XORMLogBridge) Info(v ...interface{}) { + _ = l.Log(stackLevel, log.INFO, fmt.Sprint(v...)) +} + +// Infof show information level log +func (l *XORMLogBridge) Infof(format string, v ...interface{}) { + _ = l.Log(stackLevel, log.INFO, format, v...) +} + +// Warn show warning log +func (l *XORMLogBridge) Warn(v ...interface{}) { + _ = l.Log(stackLevel, log.WARN, fmt.Sprint(v...)) +} + +// Warnf show warnning log +func (l *XORMLogBridge) Warnf(format string, v ...interface{}) { + _ = l.Log(stackLevel, log.WARN, format, v...) +} + +// Level get logger level +func (l *XORMLogBridge) Level() xormlog.LogLevel { + switch l.logger.GetLevel() { + case log.TRACE, log.DEBUG: + return xormlog.LOG_DEBUG + case log.INFO: + return xormlog.LOG_INFO + case log.WARN: + return xormlog.LOG_WARNING + case log.ERROR, log.CRITICAL: + return xormlog.LOG_ERR + } + return xormlog.LOG_OFF +} + +// SetLevel set the logger level +func (l *XORMLogBridge) SetLevel(lvl xormlog.LogLevel) { +} + +// 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 +} |