add headline and information about calling script, add logger getter

This commit is contained in:
Daniel Seifert 2019-09-19 16:03:06 +02:00
parent 35270ae271
commit 156429f7ea
3 changed files with 34 additions and 6 deletions

View File

@ -17,11 +17,22 @@ use Doctrine\DBAL\Configuration;
*/ */
class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database
{ {
public static function enableLogger() public static function enableLogger($message = null, $file = null, $line = null)
{ {
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC); $database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
$dbalConfig = $database->getConnection()->getConfiguration(); $dbalConfig = $database->getConnection()->getConfiguration();
$dbalConfig->setSQLLogger(new OxidSQLLogger()); $dbalConfig->setSQLLogger(new OxidSQLLogger($message, $file, $line));
}
/**
* @return OxidSQLLogger
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
*/
public static function getLogger()
{
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
$dbalConfig = $database->getConnection()->getConfiguration();
return $dbalConfig->getSQLLogger();
} }
public static function disableLogger() public static function disableLogger()

View File

@ -16,14 +16,22 @@ use Monolog;
*/ */
class OxidSQLLogger implements SQLLogger class OxidSQLLogger implements SQLLogger
{ {
public $message;
public $file;
public $line;
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function __construct() public function __construct($message = null, $file = null, $line = null)
{ {
if (!Monolog\Registry::hasLogger('sql')) { if (!Monolog\Registry::hasLogger('sql')) {
Monolog\Registry::addLogger((new LoggerFactory())->create('sql')); Monolog\Registry::addLogger((new LoggerFactory())->create('sql'));
} }
$this->message = $message;
$this->file = $file;
$this->line = $line;
} }
/** /**
@ -31,7 +39,16 @@ class OxidSQLLogger implements SQLLogger
*/ */
public function startQuery($sql, array $params = null, array $types = null) public function startQuery($sql, array $params = null, array $types = null)
{ {
Monolog\Registry::sql()->addDebug($sql, ['params' => $params, 'types' => $types]); Monolog\Registry::sql()->addDebug(
$this->message,
[
'query' => $sql,
'params' => $params,
'types' => $types,
'calling_file' => $this->file,
'calling_line' => $this->line,
]
);
} }
/** /**

View File

@ -5,8 +5,8 @@
* Time: 23:11 * Time: 23:11
*/ */
function StartSQLLog() { function StartSQLLog($message = null, $file = null, $line = null) {
\tm\oxid\sql\logger\OxidEsalesDatabase::enableLogger(); \tm\oxid\sql\logger\OxidEsalesDatabase::enableLogger($message, $file, $line);
} }
function StopSQLLog() { function StopSQLLog() {