Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
2b6ab8f809
|
|||
c37768f2fa
|
|||
6327bfb06d
|
|||
a57605a729
|
|||
9b1a1b24ed
|
|||
b76507025e
|
|||
f6f9ddc7e7
|
|||
8397868fdf
|
|||
e6e772a2ce
|
|||
d587fde2e8
|
|||
c74789faef
|
|||
001eafec6c
|
|||
1a339d4644
|
|||
66a842db84
|
|||
806de37cc9
|
|||
491ceeac5a
|
BIN
.gitattributes
vendored
Normal file
BIN
.gitattributes
vendored
Normal file
Binary file not shown.
46
CHANGELOG.md
46
CHANGELOG.md
@ -4,33 +4,53 @@ All notable changes to this project will be documented in this file.
|
|||||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased](https://git.d3data.de/D3Public/oxid-sql-logger/compare/1.3.3...master)
|
||||||
|
|
||||||
---
|
## [1.3.3](https://git.d3data.de/D3Public/oxid-sql-logger/compare/1.3.2...1.3.3) - 2022-09-05
|
||||||
|
### Fixed
|
||||||
|
- fix argument error if no parameter list was set in stopDebug
|
||||||
|
|
||||||
## [1.2.0] - 2020-04-30
|
## [1.3.2](https://git.d3data.de/D3Public/oxid-sql-logger/compare/1.3.1...1.3.2) - 2022-07-18
|
||||||
|
### Fixed
|
||||||
|
- fix some code style issues
|
||||||
|
|
||||||
|
## [1.3.1](https://git.d3data.de/D3Public/oxid-sql-logger/compare/1.3.0...1.3.1) - 2021-04-29
|
||||||
|
### Fixed
|
||||||
|
- prevent the use of not countable parameters in prepared statemant rendering
|
||||||
|
|
||||||
|
## [1.3.0](https://git.d3data.de/D3Public/oxid-sql-logger/compare/1.2.2...1.3.0) - 2021-03-02
|
||||||
|
### Fixed
|
||||||
|
- fix missing static methods
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
- add configurable FirePHP handler
|
||||||
|
|
||||||
|
## [1.2.2](https://git.d3data.de/D3Public/oxid-sql-logger/compare/1.2.1...1.2.2) - 2021-01-23
|
||||||
|
### Changed
|
||||||
|
- change static methods to non-static methods for better testable code
|
||||||
|
- change line endings for easy patching
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- add prepared statement generator
|
||||||
|
|
||||||
|
## [1.2.1](https://git.d3data.de/D3Public/oxid-sql-logger/compare/1.2.0...1.2.1) - 2020-05-01
|
||||||
|
### Fixed
|
||||||
|
- add missing autoload file item in composer.json
|
||||||
|
|
||||||
|
## [1.2.0](https://git.d3data.de/D3Public/oxid-sql-logger/compare/1.1.0...1.2.0) - 2020-04-30
|
||||||
|
### Added
|
||||||
- Format SQL query
|
- Format SQL query
|
||||||
- Measures the SQL execution time
|
- Measures the SQL execution time
|
||||||
|
|
||||||
---
|
## [1.1.0](https://git.d3data.de/D3Public/oxid-sql-logger/compare/1.0.0...1.1.0) - 2019-09-20
|
||||||
|
|
||||||
## [1.1.0] - 2019-09-20
|
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- add log message
|
- add log message
|
||||||
- add position specifications of the calling source code
|
- add position specifications of the calling source code
|
||||||
|
|
||||||
---
|
## [1.0.0](https://git.d3data.de/D3Public/oxid-sql-logger/releases/tag/1.0.0) - 2019-07-21
|
||||||
|
|
||||||
## [1.0.0] - 2019-07-21
|
|
||||||
|
|
||||||
available in tumtum/oxid-sql-logger package only
|
available in tumtum/oxid-sql-logger package only
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- Global function `StartSQLLog` and `StopSQLLog`
|
- Global function `StartSQLLog` and `StopSQLLog`
|
||||||
- Display SQL Querys on Browser or CLI
|
- Display SQL Querys on Browser or CLI
|
27
README.md
27
README.md
@ -30,6 +30,33 @@ CLI:
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
Add the sections to the config.inc.php of the shop if needed:
|
||||||
|
|
||||||
|
```php
|
||||||
|
$this->SqlLoggerGUIHandlers = [
|
||||||
|
\Monolog\Handler\BrowserConsoleHandler::class,
|
||||||
|
\D3\OxidSqlLogger\Handler\d3FirePHPHandler::class
|
||||||
|
];
|
||||||
|
```
|
||||||
|
|
||||||
|
Standard handlers are BrowserConsoleHandler and d3FirePHPHandler.
|
||||||
|
|
||||||
|
```php
|
||||||
|
$this->SqlLoggerCLIHandlers = [
|
||||||
|
...
|
||||||
|
];
|
||||||
|
```
|
||||||
|
|
||||||
|
Standard handler is StreamHandler.
|
||||||
|
|
||||||
|
```php
|
||||||
|
$this->d3FirePHPOptions = [
|
||||||
|
\D3\OxidSqlLogger\Handler\d3FirePHPHandler::ADD_TRACE
|
||||||
|
];
|
||||||
|
```
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
Many thanks to [Tobias Matthaiou](https://github.com/TumTum/oxid-sql-logger) for his inspiration.
|
Many thanks to [Tobias Matthaiou](https://github.com/TumTum/oxid-sql-logger) for his inspiration.
|
||||||
|
@ -4,13 +4,17 @@
|
|||||||
"type": "library",
|
"type": "library",
|
||||||
"require": {
|
"require": {
|
||||||
"monolog/monolog": "^1",
|
"monolog/monolog": "^1",
|
||||||
"nilportugues/sql-query-formatter": "^1.2.2"
|
"nilportugues/sql-query-formatter": "^1.2.2",
|
||||||
|
"firephp/firephp-core": "^0.5.3"
|
||||||
},
|
},
|
||||||
"license": "GPL-3.0",
|
"license": "GPL-3.0",
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"D3\\OxidSqlLogger\\": "./src"
|
"D3\\OxidSqlLogger\\": "./src"
|
||||||
}
|
},
|
||||||
|
"files": [
|
||||||
|
"./src/functions.php"
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"authors": [
|
"authors": [
|
||||||
{
|
{
|
||||||
@ -20,7 +24,7 @@
|
|||||||
{
|
{
|
||||||
"name": "D3 Data Development (Inh. Thomas Dartsch)",
|
"name": "D3 Data Development (Inh. Thomas Dartsch)",
|
||||||
"email": "info@shopmodule.com",
|
"email": "info@shopmodule.com",
|
||||||
"homepage": "http://www.d3data.de"
|
"homepage": "https://www.d3data.de"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use OxidEsales\Eshop\Core\DatabaseProvider;
|
||||||
|
|
||||||
require __DIR__ . '/../../../../source/bootstrap.php';
|
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]);
|
$list = $db->getAll('SELECT * '.PHP_EOL.'FROM oxarticles WHERE oxprice < ? LIMIT 100', [49.99]);
|
||||||
|
|
||||||
\D3StopSQLLog();
|
D3StopSQLLog();
|
||||||
|
|
||||||
// or
|
// 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]);
|
$list = $db->getAll('SELECT * FROM oxarticles WHERE oxprice < ? LIMIT 100', [49.99]);
|
||||||
|
|
||||||
\D3StopSQLLog();
|
D3StopSQLLog();
|
||||||
|
80
src/Extensions/d3FirePHP.php
Normal file
80
src/Extensions/d3FirePHP.php
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This Software is the property of Data Development and is protected
|
||||||
|
* by copyright law - it is NOT Freeware.
|
||||||
|
* Any unauthorized use of this software without a valid license
|
||||||
|
* is a violation of the license agreement and will be prosecuted by
|
||||||
|
* civil and criminal law.
|
||||||
|
* http://www.shopmodule.com
|
||||||
|
*
|
||||||
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
||||||
|
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||||
|
* @link http://www.oxidmodule.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace D3\OxidSqlLogger\Extensions;
|
||||||
|
|
||||||
|
use D3\OxidSqlLogger\Handler\d3FirePHPHandler;
|
||||||
|
use D3\OxidSqlLogger\OxidSQLLogger;
|
||||||
|
use Doctrine\DBAL\Connection;
|
||||||
|
use Exception;
|
||||||
|
use FirePHP;
|
||||||
|
use Monolog\Handler\AbstractProcessingHandler;
|
||||||
|
use Monolog\Logger;
|
||||||
|
use OxidEsales\EshopCommunity\Core\Database\Adapter\Doctrine\Database;
|
||||||
|
|
||||||
|
class d3FirePHP extends FirePHP
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Gets singleton instance of FirePHP
|
||||||
|
*
|
||||||
|
* @param boolean $autoCreate
|
||||||
|
* @return FirePHP
|
||||||
|
*/
|
||||||
|
public static function getInstance($autoCreate = false)
|
||||||
|
{
|
||||||
|
if ($autoCreate === true && !self::$instance) {
|
||||||
|
self::init();
|
||||||
|
}
|
||||||
|
return self::$instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates FirePHP object and stores it for singleton access
|
||||||
|
*
|
||||||
|
* @return FirePHP
|
||||||
|
*/
|
||||||
|
public static function init()
|
||||||
|
{
|
||||||
|
return self::setInstance(new self());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->ignoreClassInTraces(d3FirePHP::class);
|
||||||
|
$this->ignoreClassInTraces(d3FirePHPHandler::class);
|
||||||
|
$this->ignoreClassInTraces(Logger::class);
|
||||||
|
$this->ignoreClassInTraces(AbstractProcessingHandler::class);
|
||||||
|
$this->ignoreClassInTraces(OxidSQLLogger::class);
|
||||||
|
$this->ignoreClassInTraces(Connection::class);
|
||||||
|
$this->ignoreClassInTraces(Database::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log a trace in the firebug console
|
||||||
|
*
|
||||||
|
* @see FirePHP::TRACE
|
||||||
|
* @param string $label
|
||||||
|
* @return true
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function trace($label)
|
||||||
|
{
|
||||||
|
return $this->fb($label, $label, FirePHP::TRACE, array(
|
||||||
|
'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
50
src/Handler/d3FirePHPHandler.php
Normal file
50
src/Handler/d3FirePHPHandler.php
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This Software is the property of Data Development and is protected
|
||||||
|
* by copyright law - it is NOT Freeware.
|
||||||
|
* Any unauthorized use of this software without a valid license
|
||||||
|
* is a violation of the license agreement and will be prosecuted by
|
||||||
|
* civil and criminal law.
|
||||||
|
* http://www.shopmodule.com
|
||||||
|
*
|
||||||
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
||||||
|
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||||
|
* @link http://www.oxidmodule.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace D3\OxidSqlLogger\Handler;
|
||||||
|
|
||||||
|
use D3\OxidSqlLogger\Extensions\d3FirePHP;
|
||||||
|
use Exception;
|
||||||
|
use Monolog\Handler\AbstractProcessingHandler;
|
||||||
|
use OxidEsales\Eshop\Core\Registry;
|
||||||
|
|
||||||
|
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 = isset($options) && is_array($options) ? $options : [];
|
||||||
|
|
||||||
|
$fp = d3FirePHP::getInstance(true);
|
||||||
|
|
||||||
|
if (in_array(self::ADD_TRACE, $options)) {
|
||||||
|
$fp->group( $record['message'], [ 'Collapsed' => true ] );
|
||||||
|
}
|
||||||
|
|
||||||
|
$fp->log( $record['formatted'], $record['message']);
|
||||||
|
|
||||||
|
if (in_array(self::ADD_TRACE, $options)) {
|
||||||
|
$fp->trace( 'trace');
|
||||||
|
$fp->groupEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -8,7 +8,9 @@
|
|||||||
namespace D3\OxidSqlLogger;
|
namespace D3\OxidSqlLogger;
|
||||||
|
|
||||||
use Monolog;
|
use Monolog;
|
||||||
|
use OxidEsales\Eshop\Core\Registry;
|
||||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
||||||
|
use Traversable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Factory
|
* Class Factory
|
||||||
@ -29,16 +31,56 @@ class LoggerFactory
|
|||||||
*/
|
*/
|
||||||
private function getHandlers()
|
private function getHandlers()
|
||||||
{
|
{
|
||||||
$handlers = [];
|
|
||||||
if (PHP_SAPI == 'cli') {
|
if (PHP_SAPI == 'cli') {
|
||||||
$handlers[] = $this->getStreamHandler();
|
$configuredHandlers = Registry::getConfig()->getConfigParam('SqlLoggerCLIHandlers');
|
||||||
|
|
||||||
|
$handlers = (isset($configuredHandlers) && $this->is_iterable($configuredHandlers)) ?
|
||||||
|
$this->getInstancesFromHandlerList($configuredHandlers) :
|
||||||
|
[
|
||||||
|
$this->getStreamHandler()
|
||||||
|
];
|
||||||
} else {
|
} else {
|
||||||
$handlers[] = $this->getBrowserConsoleHandler();
|
$configuredHandlers = Registry::getConfig()->getConfigParam('SqlLoggerGUIHandlers');
|
||||||
$handlers[] = $this->getFirePHPHandler();
|
|
||||||
|
$handlers = (isset($configuredHandlers) && $this->is_iterable($configuredHandlers)) ?
|
||||||
|
$this->getInstancesFromHandlerList($configuredHandlers) :
|
||||||
|
[
|
||||||
|
$this->getBrowserConsoleHandler(),
|
||||||
|
$this->getFirePHPHandler()
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $handlers;
|
return $handlers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $classNames
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getInstancesFromHandlerList(array $classNames)
|
||||||
|
{
|
||||||
|
return array_map(
|
||||||
|
function($className){
|
||||||
|
return new $className();
|
||||||
|
},
|
||||||
|
$classNames
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* polyfill for is_iterable() - available from PHP 7.1
|
||||||
|
* @param $obj
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function is_iterable($obj)
|
||||||
|
{
|
||||||
|
return function_exists('is_iterable') ?
|
||||||
|
is_iterable($obj) :
|
||||||
|
is_array($obj) || ( $obj instanceof Traversable);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Monolog\Handler\StreamHandler
|
* @return Monolog\Handler\StreamHandler
|
||||||
*/
|
*/
|
||||||
@ -52,7 +94,7 @@ class LoggerFactory
|
|||||||
$context = (new OutputFormatterStyle('yellow'))->apply('%context%');
|
$context = (new OutputFormatterStyle('yellow'))->apply('%context%');
|
||||||
$newline = PHP_EOL . str_repeat(' ', 10);
|
$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(
|
$streamHandler->setFormatter(
|
||||||
new Monolog\Formatter\LineFormatter(
|
new Monolog\Formatter\LineFormatter(
|
||||||
|
@ -8,45 +8,91 @@
|
|||||||
namespace D3\OxidSqlLogger;
|
namespace D3\OxidSqlLogger;
|
||||||
|
|
||||||
use Doctrine\DBAL\Configuration;
|
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
|
* Class OxidEsalesDatabase
|
||||||
* Is a depenction injection Helper Class
|
* Is a depenction injection Helper Class
|
||||||
*/
|
*/
|
||||||
class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database
|
class OxidEsalesDatabase extends Database
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @param null $message
|
||||||
|
*
|
||||||
|
* @throws DatabaseConnectionException
|
||||||
|
* @deprecated use non static d3EnableLogger method or D3StartSQLLog function
|
||||||
|
*/
|
||||||
public static function enableLogger($message = null)
|
public static function enableLogger($message = null)
|
||||||
|
{
|
||||||
|
$database = oxNew(OxidEsalesDatabase::class);
|
||||||
|
$database->d3EnableLogger($message);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws DatabaseConnectionException
|
||||||
|
*/
|
||||||
|
public function d3EnableLogger($message)
|
||||||
{
|
{
|
||||||
$trace = debug_backtrace((PHP_VERSION_ID < 50306) ? 2 : DEBUG_BACKTRACE_IGNORE_ARGS);
|
$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 = $database->getConnection()->getConfiguration();
|
||||||
$dbalConfig->setSQLLogger(
|
$dbalConfig->setSQLLogger(
|
||||||
new OxidSQLLogger(
|
new OxidSQLLogger(
|
||||||
isset($trace[1]['file']) ? $trace[1]['file'] : null,
|
$trace[1]['file'] ?? null,
|
||||||
isset($trace[1]['line']) ? $trace[1]['line'] : null,
|
$trace[1]['line'] ?? null,
|
||||||
isset($trace[2]['class']) ? $trace[2]['class'] : null,
|
$trace[2]['class'] ?? null,
|
||||||
isset($trace[2]['function']) ? $trace[2]['function'] : null,
|
$trace[2]['function'] ?? null,
|
||||||
$message
|
$message
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return OxidSQLLogger
|
* @return SQLLogger|null
|
||||||
* @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException
|
* @throws DatabaseConnectionException
|
||||||
|
* @deprecated use non static d3GetLogger method
|
||||||
*/
|
*/
|
||||||
public static function getLogger()
|
public static function getLogger()
|
||||||
{
|
{
|
||||||
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
|
$database = oxNew(OxidEsalesDatabase::class);
|
||||||
|
return $database->d3GetLogger();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return SQLLogger|null
|
||||||
|
* @throws DatabaseConnectionException
|
||||||
|
*/
|
||||||
|
public function d3GetLogger()
|
||||||
|
{
|
||||||
|
$database = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
|
||||||
|
/** @var Configuration $dbalConfig */
|
||||||
$dbalConfig = $database->getConnection()->getConfiguration();
|
$dbalConfig = $database->getConnection()->getConfiguration();
|
||||||
return $dbalConfig->getSQLLogger();
|
return $dbalConfig->getSQLLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws DatabaseConnectionException
|
||||||
|
* @deprecated use non static d3DisableLogger method or D3StopSQLLog function
|
||||||
|
*/
|
||||||
public static function disableLogger()
|
public static function disableLogger()
|
||||||
{
|
{
|
||||||
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
|
$database = oxNew(OxidEsalesDatabase::class);
|
||||||
|
$database->d3DisableLogger();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws DatabaseConnectionException
|
||||||
|
*/
|
||||||
|
public function d3DisableLogger()
|
||||||
|
{
|
||||||
|
$database = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
|
||||||
|
/** @var Configuration $dbalConfig */
|
||||||
$dbalConfig = $database->getConnection()->getConfiguration();
|
$dbalConfig = $database->getConnection()->getConfiguration();
|
||||||
$dbalConfig->setSQLLogger(null);
|
$dbalConfig->setSQLLogger();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,11 @@
|
|||||||
|
|
||||||
namespace D3\OxidSqlLogger;
|
namespace D3\OxidSqlLogger;
|
||||||
|
|
||||||
|
use D3\ModCfg\Application\Model\d3database;
|
||||||
use Doctrine\DBAL\Logging\SQLLogger;
|
use Doctrine\DBAL\Logging\SQLLogger;
|
||||||
use Monolog;
|
use Monolog;
|
||||||
use NilPortugues\Sql\QueryFormatter\Formatter;
|
use NilPortugues\Sql\QueryFormatter\Formatter;
|
||||||
|
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class OxidSQLLogger
|
* Class OxidSQLLogger
|
||||||
@ -28,7 +30,11 @@ class OxidSQLLogger implements SQLLogger
|
|||||||
private $SQLQuery = null;
|
private $SQLQuery = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @param $file
|
||||||
|
* @param $line
|
||||||
|
* @param $class
|
||||||
|
* @param $function
|
||||||
|
* @param null $message
|
||||||
*/
|
*/
|
||||||
public function __construct($file, $line, $class, $function, $message = null)
|
public function __construct($file, $line, $class, $function, $message = null)
|
||||||
{
|
{
|
||||||
@ -44,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)
|
public function startQuery($sql, array $params = null, array $types = null)
|
||||||
{
|
{
|
||||||
@ -53,6 +63,8 @@ class OxidSQLLogger implements SQLLogger
|
|||||||
$this->stopQuery();
|
$this->stopQuery();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->getPreparedStatementQuery($sql, $params ?? []);
|
||||||
|
|
||||||
$this->SQLQuery = (new SQLQuery()) ->setSql($sql)
|
$this->SQLQuery = (new SQLQuery()) ->setSql($sql)
|
||||||
->setParams($params)
|
->setParams($params)
|
||||||
->setTypes($types)
|
->setTypes($types)
|
||||||
@ -62,6 +74,24 @@ class OxidSQLLogger implements SQLLogger
|
|||||||
->setLogStartingFunction($this->logStartingFunction);
|
->setLogStartingFunction($this->logStartingFunction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sql
|
||||||
|
* @param array $params
|
||||||
|
* @throws DatabaseConnectionException
|
||||||
|
*/
|
||||||
|
public function getPreparedStatementQuery(&$sql, array $params = [])
|
||||||
|
{
|
||||||
|
if (class_exists(d3database::class)
|
||||||
|
&& method_exists(d3database::class, 'getPreparedStatementQuery')
|
||||||
|
&& is_array($params)
|
||||||
|
&& count($params)
|
||||||
|
&& ($query = d3database::getInstance()->getPreparedStatementQuery($sql, $params))
|
||||||
|
&& strlen(trim($query))
|
||||||
|
) {
|
||||||
|
$sql = $query;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
@ -71,7 +101,7 @@ class OxidSQLLogger implements SQLLogger
|
|||||||
$formatter = new Formatter();
|
$formatter = new Formatter();
|
||||||
|
|
||||||
Monolog\Registry::sql()->addDebug(
|
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()),
|
'query' => $formatter->format($this->SQLQuery->getSql()),
|
||||||
'params' => $this->SQLQuery->getParams(),
|
'params' => $this->SQLQuery->getParams(),
|
||||||
|
@ -16,7 +16,7 @@ class SQLQuery
|
|||||||
/**
|
/**
|
||||||
* @var float|null
|
* @var float|null
|
||||||
*/
|
*/
|
||||||
private $start_time = null;
|
private $start_time;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var float|null
|
* @var float|null
|
||||||
@ -46,9 +46,6 @@ class SQLQuery
|
|||||||
|
|
||||||
private $logStartingFunction;
|
private $logStartingFunction;
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->start_time = microtime(true);
|
$this->start_time = microtime(true);
|
||||||
@ -224,6 +221,7 @@ class SQLQuery
|
|||||||
*
|
*
|
||||||
* @param float $microtime
|
* @param float $microtime
|
||||||
* @param string $format The format to display (printf format)
|
* @param string $format The format to display (printf format)
|
||||||
|
* @param int $round
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function readableElapsedTime($microtime, $format = '%.3f%s', $round = 3)
|
private function readableElapsedTime($microtime, $format = '%.3f%s', $round = 3)
|
||||||
|
@ -1,14 +1,43 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use D3\OxidSqlLogger\OxidEsalesDatabase;
|
||||||
|
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Tobias Matthaiou <developer@tobimat.eu>
|
* @author Tobias Matthaiou <developer@tobimat.eu>
|
||||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $message
|
||||||
|
*
|
||||||
|
* @throws DatabaseConnectionException
|
||||||
|
*/
|
||||||
function D3StartSQLLog($message = null) {
|
function D3StartSQLLog($message = null) {
|
||||||
\D3\OxidSqlLogger\OxidEsalesDatabase::enableLogger($message);
|
/** @var OxidEsalesDatabase $database */
|
||||||
|
$database = oxNew( OxidEsalesDatabase::class);
|
||||||
|
$database->d3EnableLogger($message);
|
||||||
}
|
}
|
||||||
|
|
||||||
function D3StopSQLLog() {
|
/**
|
||||||
\D3\OxidSqlLogger\OxidEsalesDatabase::disableLogger();
|
* @throws DatabaseConnectionException
|
||||||
|
*/
|
||||||
|
function D3StopSQLLog()
|
||||||
|
{
|
||||||
|
/** @var OxidEsalesDatabase $database */
|
||||||
|
$database = oxNew( OxidEsalesDatabase::class);
|
||||||
|
$database->d3DisableLogger();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $message
|
||||||
|
*
|
||||||
|
* @throws DatabaseConnectionException
|
||||||
|
*/
|
||||||
|
function D3AddSQLLogItem($message)
|
||||||
|
{
|
||||||
|
/** @var OxidEsalesDatabase $database */
|
||||||
|
$database = oxNew( OxidEsalesDatabase::class);
|
||||||
|
$database->d3GetLogger()->startQuery($message);
|
||||||
|
$database->d3GetLogger()->stopQuery();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user