Compare commits
52 Commits
Author | SHA1 | Date | |
---|---|---|---|
2b6ab8f809
|
|||
c37768f2fa
|
|||
6327bfb06d
|
|||
a57605a729
|
|||
9b1a1b24ed
|
|||
b76507025e
|
|||
f6f9ddc7e7
|
|||
8397868fdf
|
|||
e6e772a2ce
|
|||
d587fde2e8
|
|||
c74789faef
|
|||
001eafec6c
|
|||
1a339d4644
|
|||
66a842db84
|
|||
806de37cc9
|
|||
491ceeac5a
|
|||
07fcca498d
|
|||
c3aa93e543
|
|||
054ccd8419
|
|||
8df59407d9
|
|||
40012ba334 | |||
b606986b86 | |||
e57e3fe934 | |||
e0bd400107 | |||
7e6687132c | |||
2410dc3203 | |||
e3623e4bd4 | |||
466bb61afc | |||
5fd1cf0b81 | |||
2488b19301 | |||
9c3206ee73 | |||
5de9a138f8 | |||
b58b674c3a | |||
7da3397db4 | |||
3daecc030a | |||
5b9328fd73 | |||
d21a3f892d | |||
bd16bf36cd | |||
7705abd09e | |||
90c055d1cb | |||
6079ae7cf6 | |||
d8c60ce2c2 | |||
ceee2ca96c | |||
e9310cda53 | |||
156429f7ea | |||
35270ae271 | |||
8f633a1ed8 | |||
fe46283d43 | |||
a8eeb4d4e4 | |||
cb35b27522 | |||
1d627808d0 | |||
06b33cd4d5 |
BIN
.gitattributes
vendored
Normal file
BIN
.gitattributes
vendored
Normal file
Binary file not shown.
48
CHANGELOG.md
48
CHANGELOG.md
@ -4,11 +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/),
|
||||
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.0.0] - 2019-07-21
|
||||
## [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.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
|
||||
- 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
|
||||
- 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
|
||||
### Added
|
||||
- add log message
|
||||
- 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
|
||||
|
||||
available in tumtum/oxid-sql-logger package only
|
||||
|
||||
### Added
|
||||
- Global function `StartSQLLog` and `StopSQLLog`
|
||||
- Display SQL Querys on Browser or CLI
|
||||
- Display SQL Querys on Browser or CLI
|
43
README.md
43
README.md
@ -5,29 +5,58 @@ Returns all SQL queries into console of a Browser.
|
||||
|
||||
## Install
|
||||
|
||||
`composer require --dev tumtum/oxid-sql-logger`
|
||||
`composer require --dev d3/oxid-sql-logger`
|
||||
|
||||
## Usage
|
||||
|
||||
Just set the function `StartSQLLog()` somewhere and from that point on all SQLs will be logged.
|
||||
Just set the function `D3StartSQLLog()` somewhere and from that point on all SQLs will be logged.
|
||||
|
||||
```php
|
||||
\StartSQLLog();
|
||||
\D3StartSQLLog('specific log message');
|
||||
|
||||
$db = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$list = $db->getAll('SELECT * FROM oxarticles WHERE oxprice < ? LIMIT 100', [49.99]);
|
||||
|
||||
\StopSQLLog();
|
||||
\D3StopSQLLog();
|
||||
```
|
||||
|
||||
## Screenshots
|
||||
|
||||
Browser:
|
||||
|
||||

|
||||

|
||||

|
||||
|
||||
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
|
||||
|
||||
Many thanks to [Tobias Matthaiou](https://github.com/TumTum/oxid-sql-logger) for his inspiration.
|
||||
|
@ -1,14 +1,16 @@
|
||||
{
|
||||
"name": "tumtum/oxid-sql-logger",
|
||||
"name": "d3/oxid-sql-logger",
|
||||
"description": "Returns all SQL queries in the browser.",
|
||||
"type": "library",
|
||||
"require": {
|
||||
"monolog/monolog": "^1"
|
||||
"monolog/monolog": "^1",
|
||||
"nilportugues/sql-query-formatter": "^1.2.2",
|
||||
"firephp/firephp-core": "^0.5.3"
|
||||
},
|
||||
"license": "GPL-3.0",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"tm\\oxid\\sql\\logger\\": "./src"
|
||||
"D3\\OxidSqlLogger\\": "./src"
|
||||
},
|
||||
"files": [
|
||||
"./src/functions.php"
|
||||
@ -18,6 +20,11 @@
|
||||
{
|
||||
"name": "Tobias Matthaiou",
|
||||
"email": "developer@tobimat.eu"
|
||||
},
|
||||
{
|
||||
"name": "D3 Data Development (Inh. Thomas Dartsch)",
|
||||
"email": "info@shopmodule.com",
|
||||
"homepage": "https://www.d3data.de"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -1,10 +1,21 @@
|
||||
<?php
|
||||
|
||||
use OxidEsales\Eshop\Core\DatabaseProvider;
|
||||
|
||||
require __DIR__ . '/../../../../source/bootstrap.php';
|
||||
|
||||
\StartSQLLog();
|
||||
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();
|
||||
|
||||
// or
|
||||
|
||||
D3StartSQLLog();
|
||||
|
||||
$db = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$list = $db->getAll('SELECT * FROM oxarticles WHERE oxprice < ? LIMIT 100', [49.99]);
|
||||
|
||||
\StopSQLLog();
|
||||
D3StopSQLLog();
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 824 KiB After Width: | Height: | Size: 166 KiB |
Binary file not shown.
Before Width: | Height: | Size: 754 KiB |
BIN
img/screenshot-cli.jpg
Normal file
BIN
img/screenshot-cli.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 55 KiB |
Binary file not shown.
Before Width: | Height: | Size: 50 KiB |
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();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,17 +1,19 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Autor: Tobias Matthaiou <developer@tobimat.eu>
|
||||
* Date: 2019-08-21
|
||||
* Time: 07:50
|
||||
* @author Tobias Matthaiou <developer@tobimat.eu>
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
*/
|
||||
namespace tm\oxid\sql\logger;
|
||||
|
||||
namespace D3\OxidSqlLogger;
|
||||
|
||||
use Monolog;
|
||||
use OxidEsales\Eshop\Core\Registry;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
||||
use Traversable;
|
||||
|
||||
/**
|
||||
* Class Factory
|
||||
* @package tm\oxid\sql\logger
|
||||
*/
|
||||
class LoggerFactory
|
||||
{
|
||||
@ -29,15 +31,56 @@ class LoggerFactory
|
||||
*/
|
||||
private function getHandlers()
|
||||
{
|
||||
$handlers = [];
|
||||
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 {
|
||||
$handlers[] = $this->getBrowserConsoleHandler();
|
||||
$configuredHandlers = Registry::getConfig()->getConfigParam('SqlLoggerGUIHandlers');
|
||||
|
||||
$handlers = (isset($configuredHandlers) && $this->is_iterable($configuredHandlers)) ?
|
||||
$this->getInstancesFromHandlerList($configuredHandlers) :
|
||||
[
|
||||
$this->getBrowserConsoleHandler(),
|
||||
$this->getFirePHPHandler()
|
||||
];
|
||||
}
|
||||
|
||||
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
|
||||
*/
|
||||
@ -51,9 +94,16 @@ 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($ttl_color));
|
||||
$streamHandler->setFormatter(
|
||||
new Monolog\Formatter\LineFormatter(
|
||||
$ttl_color,
|
||||
null,
|
||||
true,
|
||||
true
|
||||
)
|
||||
);
|
||||
|
||||
return $streamHandler;
|
||||
}
|
||||
@ -66,13 +116,21 @@ class LoggerFactory
|
||||
return new Monolog\Handler\BrowserConsoleHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Monolog\Handler\FirePHPHandler
|
||||
*/
|
||||
private function getFirePHPHandler()
|
||||
{
|
||||
return new Monolog\Handler\FirePHPHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getProcessors()
|
||||
{
|
||||
return [
|
||||
new Monolog\Processor\IntrospectionProcessor(Monolog\Logger::DEBUG, ['tm\\oxid\\sql\\logger', 'Doctrine\\DBAL\\Connection', 'OxidEsales\\EshopCommunity\\Core\\Database\\Adapter\\Doctrine\\Database']),
|
||||
new Monolog\Processor\IntrospectionProcessor(Monolog\Logger::DEBUG, ['D3\\OxidSqlLogger', 'Doctrine\\DBAL\\Connection', 'OxidEsales\\EshopCommunity\\Core\\Database\\Adapter\\Doctrine\\Database']),
|
||||
new Monolog\Processor\PsrLogMessageProcessor(),
|
||||
];
|
||||
}
|
||||
|
@ -1,33 +1,98 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Autor: Tobias Matthaiou <developer@tobimat.eu>
|
||||
* Date: 2019-08-20
|
||||
* Time: 21:56
|
||||
* @author Tobias Matthaiou <developer@tobimat.eu>
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
*/
|
||||
|
||||
namespace tm\oxid\sql\logger;
|
||||
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
|
||||
*
|
||||
* @package tm\oxid\sql\logger
|
||||
*/
|
||||
class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database
|
||||
class OxidEsalesDatabase extends Database
|
||||
{
|
||||
public static function enableLogger()
|
||||
/**
|
||||
* @param null $message
|
||||
*
|
||||
* @throws DatabaseConnectionException
|
||||
* @deprecated use non static d3EnableLogger method or D3StartSQLLog function
|
||||
*/
|
||||
public static function enableLogger($message = null)
|
||||
{
|
||||
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$dbalConfig = $database->getConnection()->getConfiguration();
|
||||
$dbalConfig->setSQLLogger(new OxidSQLLogger());
|
||||
$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);
|
||||
|
||||
$database = DatabaseProvider::getDb( DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
/** @var Configuration $dbalConfig */
|
||||
$dbalConfig = $database->getConnection()->getConfiguration();
|
||||
$dbalConfig->setSQLLogger(
|
||||
new OxidSQLLogger(
|
||||
$trace[1]['file'] ?? null,
|
||||
$trace[1]['line'] ?? null,
|
||||
$trace[2]['class'] ?? null,
|
||||
$trace[2]['function'] ?? null,
|
||||
$message
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SQLLogger|null
|
||||
* @throws DatabaseConnectionException
|
||||
* @deprecated use non static d3GetLogger method
|
||||
*/
|
||||
public static function getLogger()
|
||||
{
|
||||
$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();
|
||||
return $dbalConfig->getSQLLogger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws DatabaseConnectionException
|
||||
* @deprecated use non static d3DisableLogger method or D3StopSQLLog function
|
||||
*/
|
||||
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->setSQLLogger(null);
|
||||
$dbalConfig->setSQLLogger();
|
||||
}
|
||||
}
|
||||
|
@ -1,37 +1,95 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Autor: Tobias Matthaiou <developer@tobimat.eu>
|
||||
* Date: 2019-08-20
|
||||
* Time: 21:33
|
||||
* @author Tobias Matthaiou <developer@tobimat.eu>
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
*/
|
||||
|
||||
namespace tm\oxid\sql\logger;
|
||||
namespace D3\OxidSqlLogger;
|
||||
|
||||
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
|
||||
* @package tm\oxid\sql\logger
|
||||
*/
|
||||
class OxidSQLLogger implements SQLLogger
|
||||
{
|
||||
public $message;
|
||||
public $logStartingFile;
|
||||
public $logStartingLine;
|
||||
public $logStartingClass;
|
||||
public $logStartingFunction;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @var SQLQuery
|
||||
*/
|
||||
public function __construct()
|
||||
private $SQLQuery = null;
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
* @param $line
|
||||
* @param $class
|
||||
* @param $function
|
||||
* @param null $message
|
||||
*/
|
||||
public function __construct($file, $line, $class, $function, $message = null)
|
||||
{
|
||||
if (!Monolog\Registry::hasLogger('sql')) {
|
||||
Monolog\Registry::addLogger((new LoggerFactory())->create('sql'));
|
||||
}
|
||||
|
||||
$this->message = $message;
|
||||
$this->logStartingFile = $file;
|
||||
$this->logStartingLine = $line;
|
||||
$this->logStartingClass = $class;
|
||||
$this->logStartingFunction = $function;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @param string $sql
|
||||
* @param array|null $params
|
||||
* @param array|null $types
|
||||
*
|
||||
* @throws DatabaseConnectionException
|
||||
*/
|
||||
public function startQuery($sql, array $params = null, array $types = null)
|
||||
{
|
||||
Monolog\Registry::sql()->addDebug($sql, ['params' => $params, 'types' => $types]);
|
||||
if ($this->SQLQuery) {
|
||||
$this->SQLQuery->setCanceled();
|
||||
$this->stopQuery();
|
||||
}
|
||||
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -39,5 +97,24 @@ class OxidSQLLogger implements SQLLogger
|
||||
*/
|
||||
public function stopQuery()
|
||||
{
|
||||
if ($this->SQLQuery) {
|
||||
$formatter = new Formatter();
|
||||
|
||||
Monolog\Registry::sql()->addDebug(
|
||||
'['.$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;
|
||||
}
|
||||
}
|
||||
|
245
src/SQLQuery.php
Normal file
245
src/SQLQuery.php
Normal file
@ -0,0 +1,245 @@
|
||||
<?php
|
||||
/**
|
||||
* Autor: Tobias Matthaiou <developer@tobimat.eu>
|
||||
* Date: 2019-08-20
|
||||
* Time: 21:56
|
||||
*/
|
||||
|
||||
namespace D3\OxidSqlLogger;
|
||||
|
||||
/**
|
||||
* Class SQLQuery
|
||||
* @package tm\oxid\sql\logger
|
||||
*/
|
||||
class SQLQuery
|
||||
{
|
||||
/**
|
||||
* @var float|null
|
||||
*/
|
||||
private $start_time;
|
||||
|
||||
/**
|
||||
* @var float|null
|
||||
*/
|
||||
private $stop_time = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $sql = '';
|
||||
|
||||
/**
|
||||
* @var null
|
||||
*/
|
||||
private $params = null;
|
||||
|
||||
/**
|
||||
* @var null
|
||||
*/
|
||||
private $types = null;
|
||||
|
||||
private $logStartingFile;
|
||||
|
||||
private $logStartingLine;
|
||||
|
||||
private $logStartingClass;
|
||||
|
||||
private $logStartingFunction;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->start_time = microtime(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSql()
|
||||
{
|
||||
return $this->sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function setSql($sql)
|
||||
{
|
||||
$this->sql = $sql;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
*/
|
||||
public function getParams()
|
||||
{
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $params
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function setParams($params)
|
||||
{
|
||||
$this->params = $params;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
*/
|
||||
public function getTypes()
|
||||
{
|
||||
return $this->types;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $types
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function setTypes($types)
|
||||
{
|
||||
$this->types = $types;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingFile()
|
||||
{
|
||||
return $this->logStartingFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $file
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function setLogStartingFile($file)
|
||||
{
|
||||
$this->logStartingFile = $file;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingLine()
|
||||
{
|
||||
return $this->logStartingLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $line
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function setLogStartingLine($line)
|
||||
{
|
||||
$this->logStartingLine = $line;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingClass()
|
||||
{
|
||||
return $this->logStartingClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $classname
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function setLogStartingClass($classname)
|
||||
{
|
||||
$this->logStartingClass = $classname;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingFunction()
|
||||
{
|
||||
return $this->logStartingFunction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $functionname
|
||||
* @return SQLQuery
|
||||
*/
|
||||
public function setLogStartingFunction($functionname)
|
||||
{
|
||||
$this->logStartingFunction = $functionname;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Statement was cancelled prematurely, an error was thrown.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setCanceled()
|
||||
{
|
||||
$this->start_time = null;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns elapsed time
|
||||
* @return float|string
|
||||
*/
|
||||
public function getElapsedTime()
|
||||
{
|
||||
if ($this->start_time === null) {
|
||||
return 'Statement canceled';
|
||||
}
|
||||
|
||||
if ($this->stop_time === null) {
|
||||
$end_time = microtime(true);
|
||||
$this->stop_time = $end_time - $this->start_time;
|
||||
}
|
||||
|
||||
return (float)$this->stop_time;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a human readable elapsed time
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getReadableElapsedTime()
|
||||
{
|
||||
return $this->readableElapsedTime($this->getElapsedTime());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a human readable elapsed time
|
||||
*
|
||||
* @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)
|
||||
{
|
||||
if (is_string($microtime)) {
|
||||
return $microtime;
|
||||
}
|
||||
|
||||
if ($microtime >= 1) {
|
||||
$unit = 's';
|
||||
$time = round($microtime, $round);
|
||||
} else {
|
||||
$unit = 'ms';
|
||||
$time = round($microtime*1000);
|
||||
|
||||
$format = preg_replace('/(%.[\d]+f)/', '%d', $format);
|
||||
}
|
||||
|
||||
return sprintf($format, $time, $unit);
|
||||
}
|
||||
}
|
@ -1,14 +1,43 @@
|
||||
<?php
|
||||
|
||||
use D3\OxidSqlLogger\OxidEsalesDatabase;
|
||||
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
||||
|
||||
/**
|
||||
* Autor: Tobias Matthaiou <developer@tobimat.eu>
|
||||
* Date: 2019-08-20
|
||||
* Time: 23:11
|
||||
* @author Tobias Matthaiou <developer@tobimat.eu>
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
*/
|
||||
|
||||
function StartSQLLog() {
|
||||
\tm\oxid\sql\logger\OxidEsalesDatabase::enableLogger();
|
||||
/**
|
||||
* @param string $message
|
||||
*
|
||||
* @throws DatabaseConnectionException
|
||||
*/
|
||||
function D3StartSQLLog($message = null) {
|
||||
/** @var OxidEsalesDatabase $database */
|
||||
$database = oxNew( OxidEsalesDatabase::class);
|
||||
$database->d3EnableLogger($message);
|
||||
}
|
||||
|
||||
function StopSQLLog() {
|
||||
\tm\oxid\sql\logger\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