Refactor the logging system

* created a "Log" function for type "Connection"
* created a "log" function for type "Provider"
* replace logger calls to Log/log where possible

I also renamed PGSSQL to PGSQL, as this seemed to be a typo

Signed-off-by: Jo Vandeginste <Jo.Vandeginste@kuleuven.be>
This commit is contained in:
Jo Vandeginste
2019-09-06 11:23:06 +02:00
committed by drakkan
parent a7363a16be
commit abbb7f272b
11 changed files with 176 additions and 106 deletions

View File

@@ -5,8 +5,6 @@ import (
"fmt"
"runtime"
"time"
"github.com/drakkan/sftpgo/logger"
)
// MySQLProvider auth provider for MySQL/MariaDB database
@@ -17,6 +15,7 @@ type MySQLProvider struct {
func initializeMySQLProvider() error {
var err error
var connectionString string
provider = MySQLProvider{}
if len(config.ConnectionString) == 0 {
connectionString = fmt.Sprintf("%v:%v@tcp([%v]:%v)/%v?charset=utf8&interpolateParams=true&timeout=10s&tls=%v",
config.Username, config.Password, config.Host, config.Port, config.Name, getSSLMode())
@@ -26,13 +25,13 @@ func initializeMySQLProvider() error {
dbHandle, err := sql.Open("mysql", connectionString)
if err == nil {
numCPU := runtime.NumCPU()
logger.Debug(logSender, "", "mysql database handle created, connection string: %#v, pool size: %v", connectionString, numCPU)
provider.log(Debug, "mysql database handle created, connection string: %#v, pool size: %v", connectionString, numCPU)
dbHandle.SetMaxIdleConns(numCPU)
dbHandle.SetMaxOpenConns(numCPU)
dbHandle.SetConnMaxLifetime(1800 * time.Second)
provider = MySQLProvider{dbHandle: dbHandle}
} else {
logger.Warn(logSender, "", "error creating mysql database handler, connection string: %#v, error: %v", connectionString, err)
provider.log(Warn, "error creating mysql database handler, connection string: %#v, error: %v", connectionString, err)
}
return err
}
@@ -52,7 +51,7 @@ func (p MySQLProvider) getUserByID(ID int64) (User, error) {
func (p MySQLProvider) updateQuota(username string, filesAdd int, sizeAdd int64, reset bool) error {
tx, err := p.dbHandle.Begin()
if err != nil {
logger.Warn(logSender, "", "error starting transaction to update quota for user %v: %v", username, err)
p.log(Warn, "error starting transaction to update quota for user %v: %v", username, err)
return err
}
err = sqlCommonUpdateQuota(username, filesAdd, sizeAdd, reset, p.dbHandle)
@@ -62,7 +61,7 @@ func (p MySQLProvider) updateQuota(username string, filesAdd int, sizeAdd int64,
err = tx.Rollback()
}
if err != nil {
logger.Warn(logSender, "", "error closing transaction to update quota for user %v: %v", username, err)
p.log(Warn, "error closing transaction to update quota for user %v: %v", username, err)
}
return err
}
@@ -90,3 +89,11 @@ func (p MySQLProvider) deleteUser(user User) error {
func (p MySQLProvider) getUsers(limit int, offset int, order string, username string) ([]User, error) {
return sqlCommonGetUsers(limit, offset, order, username, p.dbHandle)
}
func (p MySQLProvider) log(level string, format string, v ...interface{}) {
sqlCommonLog(level, p.providerName(), format, v...)
}
func (p MySQLProvider) providerName() string {
return MySQLDataProviderName
}