8
0
Fork 0
oxid-sql-logger/src/OxidSQLLogger.php

121 Zeilen
3.7 KiB
PHP

2019-08-20 23:34:33 +02:00
<?php
2019-09-19 19:52:54 +02:00
2019-08-20 23:34:33 +02:00
/**
2019-09-19 19:52:54 +02:00
* @author Tobias Matthaiou <developer@tobimat.eu>
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
2019-08-20 23:34:33 +02:00
*/
2019-09-19 19:52:54 +02:00
namespace D3\OxidSqlLogger;
2019-08-20 23:34:33 +02:00
2021-01-23 21:53:18 +01:00
use D3\ModCfg\Application\Model\d3database;
2019-08-20 23:34:33 +02:00
use Doctrine\DBAL\Logging\SQLLogger;
use Monolog;
use NilPortugues\Sql\QueryFormatter\Formatter;
2022-08-18 14:18:43 +02:00
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
2019-08-20 23:34:33 +02:00
/**
* Class OxidSQLLogger
*/
class OxidSQLLogger implements SQLLogger
{
public $message;
public $logStartingFile;
public $logStartingLine;
public $logStartingClass;
public $logStartingFunction;
/**
* @var SQLQuery
*/
private $SQLQuery = null;
2019-08-20 23:34:33 +02:00
/**
2022-08-18 14:18:43 +02:00
* @param $file
* @param $line
* @param $class
* @param $function
* @param null $message
2019-08-20 23:34:33 +02:00
*/
public function __construct($file, $line, $class, $function, $message = null)
2019-08-20 23:34:33 +02:00
{
if (!Monolog\Registry::hasLogger('sql')) {
Monolog\Registry::addLogger((new LoggerFactory())->create('sql'));
2019-08-20 23:34:33 +02:00
}
$this->message = $message;
$this->logStartingFile = $file;
$this->logStartingLine = $line;
$this->logStartingClass = $class;
$this->logStartingFunction = $function;
2019-08-20 23:34:33 +02:00
}
/**
2022-08-18 14:18:43 +02:00
* @param string $sql
* @param array|null $params
* @param array|null $types
*
* @throws DatabaseConnectionException
2019-08-20 23:34:33 +02:00
*/
public function startQuery($sql, array $params = null, array $types = null)
{
if ($this->SQLQuery) {
$this->SQLQuery->setCanceled();
$this->stopQuery();
}
2021-01-23 21:53:18 +01:00
$this->getPreparedStatementQuery($sql, $params);
$this->SQLQuery = (new SQLQuery()) ->setSql($sql)
->setParams($params)
->setTypes($types)
->setLogStartingFile($this->logStartingFile)
->setLogStartingLine($this->logStartingLine)
->setLogStartingClass($this->logStartingClass)
->setLogStartingFunction($this->logStartingFunction);
2019-08-20 23:34:33 +02:00
}
/**
2021-01-23 21:53:18 +01:00
* @param string $sql
* @param array $params
2022-08-18 14:18:43 +02:00
* @throws DatabaseConnectionException
2021-01-23 21:53:18 +01:00
*/
2022-08-18 14:18:43 +02:00
public function getPreparedStatementQuery(&$sql, array $params = [])
2021-01-23 21:53:18 +01:00
{
if (class_exists(d3database::class)
&& method_exists(d3database::class, 'getPreparedStatementQuery')
&& is_array($params)
2021-01-23 21:53:18 +01:00
&& count($params)
&& ($query = d3database::getInstance()->getPreparedStatementQuery($sql, $params))
&& strlen(trim($query))
) {
$sql = $query;
}
}
/**
2019-08-20 23:34:33 +02:00
* @inheritDoc
*/
public function stopQuery()
{
if ($this->SQLQuery) {
$formatter = new Formatter();
Monolog\Registry::sql()->addDebug(
2022-08-18 14:18:43 +02:00
'['.$this->SQLQuery->getReadableElapsedTime().'] ' . ( $this->message ?: $this->SQLQuery->getSql() ),
[
'query' => $formatter->format($this->SQLQuery->getSql()),
'params' => $this->SQLQuery->getParams(),
'time' => $this->SQLQuery->getElapsedTime(),
'types' => $this->SQLQuery->getTypes(),
'logStartingFile' => $this->SQLQuery->getLogStartingFile(),
'logStartingLine' => $this->SQLQuery->getLogStartingLine(),
'logStartingClass' => $this->SQLQuery->getLogStartingClass(),
'logStartingFunction' => $this->SQLQuery->getLogStartingFunction(),
]
);
}
$this->SQLQuery = null;
2019-08-20 23:34:33 +02:00
}
}