Merge pull request #7005 from nupplaphil/issue/dba_logger_fix

DBA & Logger fix
This commit is contained in:
Hypolite Petovan 2019-04-13 17:02:15 -04:00 committed by GitHub
commit edc4cfdcd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 65 additions and 19 deletions

View file

@ -11,6 +11,7 @@ use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Database\DBStructure; use Friendica\Database\DBStructure;
use Friendica\Object\Image; use Friendica\Object\Image;
use Friendica\Util\Logger\VoidLogger;
use Friendica\Util\Network; use Friendica\Util\Network;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use Friendica\Util\Strings; use Friendica\Util\Strings;
@ -601,7 +602,7 @@ class Installer
$dbpass = $configCache->get('database', 'password'); $dbpass = $configCache->get('database', 'password');
$dbdata = $configCache->get('database', 'database'); $dbdata = $configCache->get('database', 'database');
if (!DBA::connect($basePath, $configCache, $profiler, $dbhost, $dbuser, $dbpass, $dbdata)) { if (!DBA::connect($basePath, $configCache, $profiler, new VoidLogger(), $dbhost, $dbuser, $dbpass, $dbdata)) {
$this->addCheck(L10n::t('Could not connect to database.'), false, true, ''); $this->addCheck(L10n::t('Could not connect to database.'), false, true, '');
return false; return false;

View file

@ -3,7 +3,6 @@
namespace Friendica\Database; namespace Friendica\Database;
use Friendica\Core\Config\Cache\IConfigCache; use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Core\Logger;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
@ -13,6 +12,7 @@ use mysqli_stmt;
use PDO; use PDO;
use PDOException; use PDOException;
use PDOStatement; use PDOStatement;
use Psr\Log\LoggerInterface;
/** /**
* @class MySQL database class * @class MySQL database class
@ -40,6 +40,10 @@ class DBA
* @var Profiler * @var Profiler
*/ */
private static $profiler; private static $profiler;
/**
* @var LoggerInterface
*/
private static $logger;
/** /**
* @var string * @var string
*/ */
@ -59,7 +63,7 @@ class DBA
private static $db_name = ''; private static $db_name = '';
private static $db_charset = ''; private static $db_charset = '';
public static function connect($basePath, IConfigCache $configCache, Profiler $profiler, $serveraddr, $user, $pass, $db, $charset = null) public static function connect($basePath, IConfigCache $configCache, Profiler $profiler, LoggerInterface $logger, $serveraddr, $user, $pass, $db, $charset = null)
{ {
if (!is_null(self::$connection) && self::connected()) { if (!is_null(self::$connection) && self::connected()) {
return true; return true;
@ -69,6 +73,7 @@ class DBA
self::$basePath = $basePath; self::$basePath = $basePath;
self::$configCache = $configCache; self::$configCache = $configCache;
self::$profiler = $profiler; self::$profiler = $profiler;
self::$logger = $logger;
self::$db_serveraddr = $serveraddr; self::$db_serveraddr = $serveraddr;
self::$db_user = $user; self::$db_user = $user;
self::$db_pass = $pass; self::$db_pass = $pass;
@ -143,6 +148,21 @@ class DBA
return self::$connected; return self::$connected;
} }
/**
* Sets the logger for DBA
*
* @note this is necessary because if we want to load the logger configuration
* from the DB, but there's an error, we would print out an exception.
* So the logger gets updated after the logger configuration can be retrieved
* from the database
*
* @param LoggerInterface $logger
*/
public static function setLogger(LoggerInterface $logger)
{
self::$logger = $logger;
}
/** /**
* Disconnects the current database connection * Disconnects the current database connection
*/ */
@ -169,7 +189,7 @@ class DBA
public static function reconnect() { public static function reconnect() {
self::disconnect(); self::disconnect();
$ret = self::connect(self::$basePath, self::$configCache, self::$profiler, self::$db_serveraddr, self::$db_user, self::$db_pass, self::$db_name, self::$db_charset); $ret = self::connect(self::$basePath, self::$configCache, self::$profiler, self::$logger, self::$db_serveraddr, self::$db_user, self::$db_pass, self::$db_name, self::$db_charset);
return $ret; return $ret;
} }
@ -425,7 +445,7 @@ class DBA
if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) { if ((substr_count($sql, '?') != count($args)) && (count($args) > 0)) {
// Question: Should we continue or stop the query here? // Question: Should we continue or stop the query here?
Logger::warning('Query parameters mismatch.', ['query' => $sql, 'args' => $args, 'callstack' => System::callstack()]); self::$logger->warning('Query parameters mismatch.', ['query' => $sql, 'args' => $args, 'callstack' => System::callstack()]);
} }
$sql = self::cleanQuery($sql); $sql = self::cleanQuery($sql);
@ -565,22 +585,35 @@ class DBA
$error = self::$error; $error = self::$error;
$errorno = self::$errorno; $errorno = self::$errorno;
Logger::log('DB Error '.self::$errorno.': '.self::$error."\n". self::$logger->error('DB Error', [
System::callstack(8)."\n".self::replaceParameters($sql, $args)); 'code' => self::$errorno,
'error' => self::$error,
'callstack' => System::callstack(8),
'params' => self::replaceParameters($sql, $args),
]);
// On a lost connection we try to reconnect - but only once. // On a lost connection we try to reconnect - but only once.
if ($errorno == 2006) { if ($errorno == 2006) {
if (self::$in_retrial || !self::reconnect()) { if (self::$in_retrial || !self::reconnect()) {
// It doesn't make sense to continue when the database connection was lost // It doesn't make sense to continue when the database connection was lost
if (self::$in_retrial) { if (self::$in_retrial) {
Logger::log('Giving up retrial because of database error '.$errorno.': '.$error); self::$logger->notice('Giving up retrial because of database error', [
'code' => self::$errorno,
'error' => self::$error,
]);
} else { } else {
Logger::log("Couldn't reconnect after database error ".$errorno.': '.$error); self::$logger->notice('Couldn\'t reconnect after database error', [
'code' => self::$errorno,
'error' => self::$error,
]);
} }
exit(1); exit(1);
} else { } else {
// We try it again // We try it again
Logger::log('Reconnected after database error '.$errorno.': '.$error); self::$logger->notice('Reconnected after database error', [
'code' => self::$errorno,
'error' => self::$error,
]);
self::$in_retrial = true; self::$in_retrial = true;
$ret = self::p($sql, $args); $ret = self::p($sql, $args);
self::$in_retrial = false; self::$in_retrial = false;
@ -649,13 +682,20 @@ class DBA
$error = self::$error; $error = self::$error;
$errorno = self::$errorno; $errorno = self::$errorno;
Logger::log('DB Error '.self::$errorno.': '.self::$error."\n". self::$logger->error('DB Error', [
System::callstack(8)."\n".self::replaceParameters($sql, $params)); 'code' => self::$errorno,
'error' => self::$error,
'callstack' => System::callstack(8),
'params' => self::replaceParameters($sql, $params),
]);
// On a lost connection we simply quit. // On a lost connection we simply quit.
// A reconnect like in self::p could be dangerous with modifications // A reconnect like in self::p could be dangerous with modifications
if ($errorno == 2006) { if ($errorno == 2006) {
Logger::log('Giving up because of database error '.$errorno.': '.$error); self::$logger->notice('Giving up because of database error', [
'code' => self::$errorno,
'error' => self::$error,
]);
exit(1); exit(1);
} }
@ -850,7 +890,7 @@ class DBA
public static function insert($table, $param, $on_duplicate_update = false) { public static function insert($table, $param, $on_duplicate_update = false) {
if (empty($table) || empty($param)) { if (empty($table) || empty($param)) {
Logger::log('Table and fields have to be set'); self::$logger->info('Table and fields have to be set');
return false; return false;
} }
@ -1068,7 +1108,7 @@ class DBA
public static function delete($table, array $conditions, array $options = [], array &$callstack = []) public static function delete($table, array $conditions, array $options = [], array &$callstack = [])
{ {
if (empty($table) || empty($conditions)) { if (empty($table) || empty($conditions)) {
Logger::log('Table and conditions have to be set'); self::$logger->info('Table and conditions have to be set');
return false; return false;
} }
@ -1154,7 +1194,7 @@ class DBA
if ((count($command['conditions']) > 1) || is_int($first_key)) { if ((count($command['conditions']) > 1) || is_int($first_key)) {
$sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string; $sql = "DELETE FROM `" . $command['table'] . "`" . $condition_string;
Logger::log(self::replaceParameters($sql, $conditions), Logger::DATA); self::$logger->debug(self::replaceParameters($sql, $conditions));
if (!self::e($sql, $conditions)) { if (!self::e($sql, $conditions)) {
if ($do_transaction) { if ($do_transaction) {
@ -1184,7 +1224,7 @@ class DBA
$sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" . $sql = "DELETE FROM `" . $table . "` WHERE `" . $field . "` IN (" .
substr(str_repeat("?, ", count($field_values)), 0, -2) . ");"; substr(str_repeat("?, ", count($field_values)), 0, -2) . ");";
Logger::log(self::replaceParameters($sql, $field_values), Logger::DATA); self::$logger->debug(self::replaceParameters($sql, $field_values));
if (!self::e($sql, $field_values)) { if (!self::e($sql, $field_values)) {
if ($do_transaction) { if ($do_transaction) {
@ -1233,7 +1273,7 @@ class DBA
public static function update($table, $fields, $condition, $old_fields = []) { public static function update($table, $fields, $condition, $old_fields = []) {
if (empty($table) || empty($fields) || empty($condition)) { if (empty($table) || empty($fields) || empty($condition)) {
Logger::log('Table, fields and condition have to be set'); self::$logger->info('Table, fields and condition have to be set');
return false; return false;
} }

View file

@ -4,6 +4,7 @@ namespace Friendica\Factory;
use Friendica\Core\Config\Cache; use Friendica\Core\Config\Cache;
use Friendica\Database; use Friendica\Database;
use Friendica\Util\Logger\VoidLogger;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
class DBFactory class DBFactory
@ -51,7 +52,7 @@ class DBFactory
$db_data = $server['MYSQL_DATABASE']; $db_data = $server['MYSQL_DATABASE'];
} }
if (Database\DBA::connect($basePath, $configCache, $profiler, $db_host, $db_user, $db_pass, $db_data, $charset)) { if (Database\DBA::connect($basePath, $configCache, $profiler, new VoidLogger(), $db_host, $db_user, $db_pass, $db_data, $charset)) {
// Loads DB_UPDATE_VERSION constant // Loads DB_UPDATE_VERSION constant
Database\DBStructure::definition($basePath, false); Database\DBStructure::definition($basePath, false);
} }

View file

@ -3,6 +3,7 @@
namespace Friendica\Factory; namespace Friendica\Factory;
use Friendica\App; use Friendica\App;
use Friendica\Database\DBA;
use Friendica\Factory; use Friendica\Factory;
use Friendica\Util\BasePath; use Friendica\Util\BasePath;
use Friendica\Util\BaseURL; use Friendica\Util\BaseURL;
@ -34,6 +35,7 @@ class DependencyFactory
// needed to call PConfig::init() // needed to call PConfig::init()
Factory\ConfigFactory::createPConfig($configCache); Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create($channel, $config, $profiler); $logger = Factory\LoggerFactory::create($channel, $config, $profiler);
DBA::setLogger($logger);
Factory\LoggerFactory::createDev($channel, $config, $profiler); Factory\LoggerFactory::createDev($channel, $config, $profiler);
$baseURL = new BaseURL($config, $_SERVER); $baseURL = new BaseURL($config, $_SERVER);

View file

@ -10,6 +10,7 @@ use Friendica\Database\DBA;
use Friendica\Factory; use Friendica\Factory;
use Friendica\Util\BasePath; use Friendica\Util\BasePath;
use Friendica\Util\Config\ConfigFileLoader; use Friendica\Util\Config\ConfigFileLoader;
use Friendica\Util\Logger\VoidLogger;
use Friendica\Util\Profiler; use Friendica\Util\Profiler;
use PHPUnit\DbUnit\DataSet\YamlDataSet; use PHPUnit\DbUnit\DataSet\YamlDataSet;
use PHPUnit\DbUnit\TestCaseTrait; use PHPUnit\DbUnit\TestCaseTrait;
@ -52,6 +53,7 @@ abstract class DatabaseTest extends MockedTest
$basePath, $basePath,
$config, $config,
$profiler, $profiler,
new VoidLogger(),
getenv('MYSQL_HOST'), getenv('MYSQL_HOST'),
getenv('MYSQL_USERNAME'), getenv('MYSQL_USERNAME'),
getenv('MYSQL_PASSWORD'), getenv('MYSQL_PASSWORD'),