improve code
This commit is contained in:
parent
9b1a1b24ed
commit
a57605a729
@ -1,19 +1,21 @@
|
||||
<?php
|
||||
|
||||
use OxidEsales\Eshop\Core\DatabaseProvider;
|
||||
|
||||
require __DIR__ . '/../../../../source/bootstrap.php';
|
||||
|
||||
\D3StartSQLLog('Query for 100 items cheaper than 49,99');
|
||||
D3StartSQLLog( 'Query for 100 items cheaper than 49,99');
|
||||
|
||||
$db = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$db = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$list = $db->getAll('SELECT * '.PHP_EOL.'FROM oxarticles WHERE oxprice < ? LIMIT 100', [49.99]);
|
||||
|
||||
\D3StopSQLLog();
|
||||
D3StopSQLLog();
|
||||
|
||||
// or
|
||||
|
||||
\D3StartSQLLog();
|
||||
D3StartSQLLog();
|
||||
|
||||
$db = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$db = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$list = $db->getAll('SELECT * FROM oxarticles WHERE oxprice < ? LIMIT 100', [49.99]);
|
||||
|
||||
\D3StopSQLLog();
|
||||
D3StopSQLLog();
|
||||
|
@ -18,7 +18,8 @@ namespace D3\OxidSqlLogger\Extensions;
|
||||
use D3\OxidSqlLogger\Handler\d3FirePHPHandler;
|
||||
use D3\OxidSqlLogger\OxidSQLLogger;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use \FirePHP;
|
||||
use Exception;
|
||||
use FirePHP;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use Monolog\Logger;
|
||||
use OxidEsales\EshopCommunity\Core\Database\Adapter\Doctrine\Database;
|
||||
|
@ -16,15 +16,22 @@
|
||||
namespace D3\OxidSqlLogger\Handler;
|
||||
|
||||
use D3\OxidSqlLogger\Extensions\d3FirePHP;
|
||||
use Exception;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use OxidEsales\Eshop\Core\Registry;
|
||||
|
||||
class d3FirePHPHandler extends \Monolog\Handler\AbstractProcessingHandler
|
||||
class d3FirePHPHandler extends AbstractProcessingHandler
|
||||
{
|
||||
const ADD_TRACE = 'addTrace';
|
||||
|
||||
/**
|
||||
* @param array $record
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function write(array $record): void
|
||||
{
|
||||
$options = Registry::getConfig()->getConfigParam(d3FirePHPOptions);
|
||||
$options = Registry::getConfig()->getConfigParam('d3FirePHPOptions');
|
||||
$options = isset($options) && is_array($options) ? $options : [];
|
||||
|
||||
$fp = d3FirePHP::getInstance(true);
|
||||
@ -36,7 +43,7 @@ class d3FirePHPHandler extends \Monolog\Handler\AbstractProcessingHandler
|
||||
$fp->log( $record['formatted'], $record['message']);
|
||||
|
||||
if (in_array(self::ADD_TRACE, $options)) {
|
||||
$fp->trace( 'trace', 'trace' );
|
||||
$fp->trace( 'trace');
|
||||
$fp->groupEnd();
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ namespace D3\OxidSqlLogger;
|
||||
use Monolog;
|
||||
use OxidEsales\Eshop\Core\Registry;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
||||
use Traversable;
|
||||
|
||||
/**
|
||||
* Class Factory
|
||||
@ -77,7 +78,7 @@ class LoggerFactory
|
||||
{
|
||||
return function_exists('is_iterable') ?
|
||||
is_iterable($obj) :
|
||||
is_array($obj) || (is_object($obj) && ($obj instanceof \Traversable));
|
||||
is_array($obj) || ( $obj instanceof Traversable);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,7 +94,7 @@ class LoggerFactory
|
||||
$context = (new OutputFormatterStyle('yellow'))->apply('%context%');
|
||||
$newline = PHP_EOL . str_repeat(' ', 10);
|
||||
|
||||
$ttl_color = "$channel $level_name: $message {$newline} $context {$newline} %extra%" . PHP_EOL;
|
||||
$ttl_color = "$channel $level_name: $message $newline $context $newline %extra%" . PHP_EOL;
|
||||
|
||||
$streamHandler->setFormatter(
|
||||
new Monolog\Formatter\LineFormatter(
|
||||
|
@ -9,17 +9,20 @@ namespace D3\OxidSqlLogger;
|
||||
|
||||
use Doctrine\DBAL\Configuration;
|
||||
use Doctrine\DBAL\Logging\SQLLogger;
|
||||
use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database;
|
||||
use OxidEsales\Eshop\Core\DatabaseProvider;
|
||||
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
||||
|
||||
/**
|
||||
* Class OxidEsalesDatabase
|
||||
* Is a depenction injection Helper Class
|
||||
*/
|
||||
class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database
|
||||
class OxidEsalesDatabase extends Database
|
||||
{
|
||||
/**
|
||||
* @param null $message
|
||||
*
|
||||
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
|
||||
* @throws DatabaseConnectionException
|
||||
* @deprecated use non static d3EnableLogger method or D3StartSQLLog function
|
||||
*/
|
||||
public static function enableLogger($message = null)
|
||||
@ -29,21 +32,21 @@ class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrin
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
|
||||
* @throws DatabaseConnectionException
|
||||
*/
|
||||
public function d3EnableLogger($message)
|
||||
{
|
||||
$trace = debug_backtrace((PHP_VERSION_ID < 50306) ? 2 : DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
|
||||
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$database = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
/** @var Configuration $dbalConfig */
|
||||
$dbalConfig = $database->getConnection()->getConfiguration();
|
||||
$dbalConfig->setSQLLogger(
|
||||
new OxidSQLLogger(
|
||||
isset($trace[1]['file']) ? $trace[1]['file'] : null,
|
||||
isset($trace[1]['line']) ? $trace[1]['line'] : null,
|
||||
isset($trace[2]['class']) ? $trace[2]['class'] : null,
|
||||
isset($trace[2]['function']) ? $trace[2]['function'] : null,
|
||||
$trace[1]['file'] ?? null,
|
||||
$trace[1]['line'] ?? null,
|
||||
$trace[2]['class'] ?? null,
|
||||
$trace[2]['function'] ?? null,
|
||||
$message
|
||||
)
|
||||
);
|
||||
@ -51,7 +54,7 @@ class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrin
|
||||
|
||||
/**
|
||||
* @return SQLLogger|null
|
||||
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
|
||||
* @throws DatabaseConnectionException
|
||||
* @deprecated use non static d3GetLogger method
|
||||
*/
|
||||
public static function getLogger()
|
||||
@ -62,18 +65,18 @@ class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrin
|
||||
|
||||
/**
|
||||
* @return SQLLogger|null
|
||||
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
|
||||
* @throws DatabaseConnectionException
|
||||
*/
|
||||
public function d3GetLogger()
|
||||
{
|
||||
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$database = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
/** @var Configuration $dbalConfig */
|
||||
$dbalConfig = $database->getConnection()->getConfiguration();
|
||||
return $dbalConfig->getSQLLogger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
|
||||
* @throws DatabaseConnectionException
|
||||
* @deprecated use non static d3DisableLogger method or D3StopSQLLog function
|
||||
*/
|
||||
public static function disableLogger()
|
||||
@ -83,13 +86,13 @@ class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrin
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
|
||||
* @throws DatabaseConnectionException
|
||||
*/
|
||||
public function d3DisableLogger()
|
||||
{
|
||||
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$database = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
/** @var Configuration $dbalConfig */
|
||||
$dbalConfig = $database->getConnection()->getConfiguration();
|
||||
$dbalConfig->setSQLLogger(null);
|
||||
$dbalConfig->setSQLLogger();
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ use D3\ModCfg\Application\Model\d3database;
|
||||
use Doctrine\DBAL\Logging\SQLLogger;
|
||||
use Monolog;
|
||||
use NilPortugues\Sql\QueryFormatter\Formatter;
|
||||
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
||||
|
||||
/**
|
||||
* Class OxidSQLLogger
|
||||
@ -29,7 +30,11 @@ class OxidSQLLogger implements SQLLogger
|
||||
private $SQLQuery = null;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @param $file
|
||||
* @param $line
|
||||
* @param $class
|
||||
* @param $function
|
||||
* @param null $message
|
||||
*/
|
||||
public function __construct($file, $line, $class, $function, $message = null)
|
||||
{
|
||||
@ -45,7 +50,11 @@ class OxidSQLLogger implements SQLLogger
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @param string $sql
|
||||
* @param array|null $params
|
||||
* @param array|null $types
|
||||
*
|
||||
* @throws DatabaseConnectionException
|
||||
*/
|
||||
public function startQuery($sql, array $params = null, array $types = null)
|
||||
{
|
||||
@ -68,9 +77,9 @@ class OxidSQLLogger implements SQLLogger
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
|
||||
* @throws DatabaseConnectionException
|
||||
*/
|
||||
public function getPreparedStatementQuery(&$sql, $params = [])
|
||||
public function getPreparedStatementQuery(&$sql, array $params = [])
|
||||
{
|
||||
if (class_exists(d3database::class)
|
||||
&& method_exists(d3database::class, 'getPreparedStatementQuery')
|
||||
@ -92,7 +101,7 @@ class OxidSQLLogger implements SQLLogger
|
||||
$formatter = new Formatter();
|
||||
|
||||
Monolog\Registry::sql()->addDebug(
|
||||
'['.$this->SQLQuery->getReadableElapsedTime().'] ' . ( $this->message ? $this->message : $this->SQLQuery->getSql() ),
|
||||
'['.$this->SQLQuery->getReadableElapsedTime().'] ' . ( $this->message ?: $this->SQLQuery->getSql() ),
|
||||
[
|
||||
'query' => $formatter->format($this->SQLQuery->getSql()),
|
||||
'params' => $this->SQLQuery->getParams(),
|
||||
|
@ -16,7 +16,7 @@ class SQLQuery
|
||||
/**
|
||||
* @var float|null
|
||||
*/
|
||||
private $start_time = null;
|
||||
private $start_time;
|
||||
|
||||
/**
|
||||
* @var float|null
|
||||
@ -46,9 +46,6 @@ class SQLQuery
|
||||
|
||||
private $logStartingFunction;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->start_time = microtime(true);
|
||||
@ -224,6 +221,7 @@ class SQLQuery
|
||||
*
|
||||
* @param float $microtime
|
||||
* @param string $format The format to display (printf format)
|
||||
* @param int $round
|
||||
* @return string
|
||||
*/
|
||||
private function readableElapsedTime($microtime, $format = '%.3f%s', $round = 3)
|
||||
|
@ -1,27 +1,43 @@
|
||||
<?php
|
||||
|
||||
use D3\OxidSqlLogger\OxidEsalesDatabase;
|
||||
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
||||
|
||||
/**
|
||||
* @author Tobias Matthaiou <developer@tobimat.eu>
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*
|
||||
* @throws DatabaseConnectionException
|
||||
*/
|
||||
function D3StartSQLLog($message = null) {
|
||||
/** @var \D3\OxidSqlLogger\OxidEsalesDatabase $database */
|
||||
$database = oxNew(\D3\OxidSqlLogger\OxidEsalesDatabase::class);
|
||||
/** @var OxidEsalesDatabase $database */
|
||||
$database = oxNew( OxidEsalesDatabase::class);
|
||||
$database->d3EnableLogger($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws DatabaseConnectionException
|
||||
*/
|
||||
function D3StopSQLLog()
|
||||
{
|
||||
/** @var \D3\OxidSqlLogger\OxidEsalesDatabase $database */
|
||||
$database = oxNew(\D3\OxidSqlLogger\OxidEsalesDatabase::class);
|
||||
/** @var OxidEsalesDatabase $database */
|
||||
$database = oxNew( OxidEsalesDatabase::class);
|
||||
$database->d3DisableLogger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
*
|
||||
* @throws DatabaseConnectionException
|
||||
*/
|
||||
function D3AddSQLLogItem($message)
|
||||
{
|
||||
/** @var \D3\OxidSqlLogger\OxidEsalesDatabase $database */
|
||||
$database = oxNew(\D3\OxidSqlLogger\OxidEsalesDatabase::class);
|
||||
/** @var OxidEsalesDatabase $database */
|
||||
$database = oxNew( OxidEsalesDatabase::class);
|
||||
$database->d3GetLogger()->startQuery($message);
|
||||
$database->d3GetLogger()->stopQuery($message);
|
||||
$database->d3GetLogger()->stopQuery();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user