Comparer les révisions
55 Révisions
Auteur | SHA1 | Date |
---|---|---|
Daniel Seifert | fae28866eb | |
Daniel Seifert | 1aac154bcb | |
Daniel Seifert | e47817b95d | |
Daniel Seifert | 2b6ab8f809 | |
Daniel Seifert | c37768f2fa | |
Daniel Seifert | 6327bfb06d | |
Daniel Seifert | a57605a729 | |
Daniel Seifert | 9b1a1b24ed | |
Daniel Seifert | b76507025e | |
Daniel Seifert | f6f9ddc7e7 | |
Daniel Seifert | 8397868fdf | |
Daniel Seifert | e6e772a2ce | |
Daniel Seifert | d587fde2e8 | |
Daniel Seifert | c74789faef | |
Daniel Seifert | 001eafec6c | |
Daniel Seifert | 1a339d4644 | |
Daniel Seifert | 66a842db84 | |
Daniel Seifert | 806de37cc9 | |
Daniel Seifert | 491ceeac5a | |
Daniel Seifert | 07fcca498d | |
Daniel Seifert | c3aa93e543 | |
Daniel Seifert | 054ccd8419 | |
Daniel Seifert | 8df59407d9 | |
Tobias Matthaiou | 40012ba334 | |
Tobias Matthaiou | b606986b86 | |
Tobias Matthaiou | e57e3fe934 | |
Tobias Matthaiou | e0bd400107 | |
Tobias Matthaiou | 7e6687132c | |
Tobias Matthaiou | 2410dc3203 | |
Tobias Matthaiou | e3623e4bd4 | |
Tobias Matthaiou | 466bb61afc | |
Tobias Matthaiou | 5fd1cf0b81 | |
Tobias Matthaiou | 2488b19301 | |
Tobias Matthaiou | 9c3206ee73 | |
Tobias Matthaiou | 5de9a138f8 | |
Daniel Seifert | b58b674c3a | |
Daniel Seifert | 7da3397db4 | |
Daniel Seifert | 3daecc030a | |
Daniel Seifert | 5b9328fd73 | |
Daniel Seifert | d21a3f892d | |
Daniel Seifert | bd16bf36cd | |
Daniel Seifert | 7705abd09e | |
Daniel Seifert | 90c055d1cb | |
Daniel Seifert | 6079ae7cf6 | |
Daniel Seifert | d8c60ce2c2 | |
Daniel Seifert | ceee2ca96c | |
Daniel Seifert | e9310cda53 | |
Daniel Seifert | 156429f7ea | |
Tobias Matthaiou | 35270ae271 | |
Tobias Matthaiou | 8f633a1ed8 | |
Tobias Matthaiou | fe46283d43 | |
Tobias Matthaiou | a8eeb4d4e4 | |
Tobias Matthaiou | cb35b27522 | |
Tobias Matthaiou | 1d627808d0 | |
Tobias Matthaiou | 06b33cd4d5 |
Fichier binaire non affiché.
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
$finder = PhpCsFixer\Finder::create()
|
||||
->in(__DIR__)
|
||||
;
|
||||
|
||||
$config = new PhpCsFixer\Config();
|
||||
return $config->setRules([
|
||||
'@PHP80Migration' => true,
|
||||
'@PSR12' => true
|
||||
])
|
||||
->setFinder($finder)
|
||||
;
|
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:
|
||||
|
||||
![Example all sqls](https://raw.githubusercontent.com/TumTum/oxid-sql-logger/master/img/screenshot-a.jpg)
|
||||
![Example only one range](https://raw.githubusercontent.com/TumTum/oxid-sql-logger/master/img/screenshot-b.png)
|
||||
![Example all sqls](https://raw.githubusercontent.com/d3datadevelopment/oxid-sql-logger/master/img/screenshot-a.jpg)
|
||||
|
||||
CLI:
|
||||
|
||||
![Example CLI](https://raw.githubusercontent.com/TumTum/oxid-sql-logger/master/img/screenshot-cli.png)
|
||||
![Example CLI](https://raw.githubusercontent.com/d3datadevelopment/oxid-sql-logger/master/img/screenshot-cli.jpg)
|
||||
|
||||
## 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,21 @@
|
|||
{
|
||||
"name": "tumtum/oxid-sql-logger",
|
||||
"name": "d3/oxid-sql-logger",
|
||||
"description": "Returns all SQL queries in the browser.",
|
||||
"type": "library",
|
||||
"require": {
|
||||
"monolog/monolog": "^1"
|
||||
"php": "^8.0",
|
||||
"monolog/monolog": "^1",
|
||||
"nilportugues/sql-query-formatter": "^1.2.2",
|
||||
"firephp/firephp-core": "^0.5.3"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "~3.13.0",
|
||||
"phpstan/phpstan": "^1.10"
|
||||
},
|
||||
"license": "GPL-3.0",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"tm\\oxid\\sql\\logger\\": "./src"
|
||||
"D3\\OxidSqlLogger\\": "./src"
|
||||
},
|
||||
"files": [
|
||||
"./src/functions.php"
|
||||
|
@ -18,6 +25,16 @@
|
|||
{
|
||||
"name": "Tobias Matthaiou",
|
||||
"email": "developer@tobimat.eu"
|
||||
},
|
||||
{
|
||||
"name": "D3 Data Development (Inh. Thomas Dartsch)",
|
||||
"email": "info@shopmodule.com",
|
||||
"homepage": "https://www.d3data.de"
|
||||
}
|
||||
]
|
||||
],
|
||||
"scripts": {
|
||||
"php-cs-fixer": "./vendor/bin/php-cs-fixer fix --config=vendor/d3/oxid-sql-logger/.php-cs-fixer.php",
|
||||
|
||||
"phpstan": "./vendor/bin/phpstan --configuration=vendor/d3/oxid-sql-logger/phpstan.neon analyse"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,23 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
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();
|
||||
|
|
Fichier binaire non affiché.
Avant Largeur: | Hauteur: | Taille: 824 KiB Après Largeur: | Hauteur: | Taille: 166 KiB |
Fichier binaire non affiché.
Avant Largeur: | Hauteur: | Taille: 754 KiB |
Fichier binaire non affiché.
Après Largeur: | Hauteur: | Taille: 55 KiB |
Fichier binaire non affiché.
Avant Largeur: | Hauteur: | Taille: 50 KiB |
|
@ -0,0 +1,14 @@
|
|||
parameters:
|
||||
scanFiles:
|
||||
- ../../oxid-esales/oxideshop-ce/source/bootstrap.php
|
||||
- ../../oxid-esales/oxideshop-ce/source/oxfunctions.php
|
||||
- ../../oxid-esales/oxideshop-ce/source/overridablefunctions.php
|
||||
paths:
|
||||
- ./src
|
||||
- ./example
|
||||
level: 9
|
||||
phpVersion: 80300
|
||||
checkMissingIterableValueType: false
|
||||
treatPhpDocTypesAsCertain: false
|
||||
featureToggles:
|
||||
disableRuntimeReflectionProvider: true
|
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* https://www.d3data.de
|
||||
*
|
||||
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
* @link https://www.oxidmodule.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
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): FirePHP
|
||||
{
|
||||
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(): FirePHP
|
||||
{
|
||||
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 bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function trace($label): bool
|
||||
{
|
||||
return $this->fb($label, $label, FirePHP::TRACE, [
|
||||
'trace' => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS),
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* https://www.d3data.de
|
||||
*
|
||||
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
* @link https://www.oxidmodule.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace D3\OxidSqlLogger\Handler;
|
||||
|
||||
use D3\OxidSqlLogger\Extensions\d3FirePHP;
|
||||
use Exception;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use OxidEsales\Eshop\Core\Registry;
|
||||
|
||||
class d3FirePHPHandler extends AbstractProcessingHandler
|
||||
{
|
||||
public 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,25 +1,27 @@
|
|||
<?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;
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace D3\OxidSqlLogger;
|
||||
|
||||
use Monolog;
|
||||
use Monolog\Logger;
|
||||
use OxidEsales\Eshop\Core\Registry;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
||||
|
||||
/**
|
||||
* Class Factory
|
||||
* @package tm\oxid\sql\logger
|
||||
*/
|
||||
class LoggerFactory
|
||||
{
|
||||
/**
|
||||
* @param $name
|
||||
* @return Monolog\Logger
|
||||
* @param string $name
|
||||
*
|
||||
* @return Logger
|
||||
*/
|
||||
public function create($name)
|
||||
public function create(string $name): Monolog\Logger
|
||||
{
|
||||
return new Monolog\Logger($name, $this->getHandlers(), $this->getProcessors());
|
||||
}
|
||||
|
@ -27,21 +29,49 @@ class LoggerFactory
|
|||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getHandlers()
|
||||
private function getHandlers(): array
|
||||
{
|
||||
$handlers = [];
|
||||
if (PHP_SAPI == 'cli') {
|
||||
$handlers[] = $this->getStreamHandler();
|
||||
$configuredHandlers = Registry::getConfig()->getConfigParam('SqlLoggerCLIHandlers');
|
||||
|
||||
$handlers = (isset($configuredHandlers) && is_iterable($configuredHandlers)) ?
|
||||
$this->getInstancesFromHandlerList($configuredHandlers) :
|
||||
[
|
||||
$this->getStreamHandler(),
|
||||
];
|
||||
} else {
|
||||
$handlers[] = $this->getBrowserConsoleHandler();
|
||||
$configuredHandlers = Registry::getConfig()->getConfigParam('SqlLoggerGUIHandlers');
|
||||
|
||||
$handlers = (isset($configuredHandlers) && is_iterable($configuredHandlers)) ?
|
||||
$this->getInstancesFromHandlerList($configuredHandlers) :
|
||||
[
|
||||
$this->getBrowserConsoleHandler(),
|
||||
$this->getFirePHPHandler(),
|
||||
];
|
||||
}
|
||||
|
||||
return $handlers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable $classNames
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getInstancesFromHandlerList(iterable $classNames): array
|
||||
{
|
||||
return array_map(
|
||||
function ($className) {
|
||||
return new $className();
|
||||
},
|
||||
(array) $classNames
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Monolog\Handler\StreamHandler
|
||||
*/
|
||||
private function getStreamHandler()
|
||||
private function getStreamHandler(): Monolog\Handler\StreamHandler
|
||||
{
|
||||
$streamHandler = new Monolog\Handler\StreamHandler('php://stderr');
|
||||
|
||||
|
@ -51,9 +81,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;
|
||||
}
|
||||
|
@ -61,18 +98,33 @@ class LoggerFactory
|
|||
/**
|
||||
* @return Monolog\Handler\BrowserConsoleHandler
|
||||
*/
|
||||
private function getBrowserConsoleHandler()
|
||||
private function getBrowserConsoleHandler(): Monolog\Handler\BrowserConsoleHandler
|
||||
{
|
||||
return new Monolog\Handler\BrowserConsoleHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Monolog\Handler\FirePHPHandler
|
||||
*/
|
||||
private function getFirePHPHandler(): Monolog\Handler\FirePHPHandler
|
||||
{
|
||||
return new Monolog\Handler\FirePHPHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getProcessors()
|
||||
private function getProcessors(): array
|
||||
{
|
||||
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,76 @@
|
|||
<?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;
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace D3\OxidSqlLogger;
|
||||
|
||||
use Doctrine\DBAL\Configuration;
|
||||
use Doctrine\DBAL\Logging\SQLLogger;
|
||||
use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database;
|
||||
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
|
||||
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProvider;
|
||||
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
/**
|
||||
* 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 string|null $message
|
||||
*
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function d3EnableLogger(string $message = null): void
|
||||
{
|
||||
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$dbalConfig = $database->getConnection()->getConfiguration();
|
||||
$dbalConfig->setSQLLogger(new OxidSQLLogger());
|
||||
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
||||
|
||||
$this->d3GetConfiguration()->setSQLLogger(
|
||||
new OxidSQLLogger(
|
||||
$trace[1]['file'] ?? '',
|
||||
array_key_exists('line', $trace[1]) ? (int) $trace[1]['line'] : 0,
|
||||
$trace[2]['class'] ?? '',
|
||||
$trace[2]['function'] ?? '',
|
||||
$message
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public static function disableLogger()
|
||||
/**
|
||||
* @return SQLLogger|null
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function d3GetLogger(): ?SQLLogger
|
||||
{
|
||||
$database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
|
||||
$dbalConfig = $database->getConnection()->getConfiguration();
|
||||
$dbalConfig->setSQLLogger(null);
|
||||
return $this->d3GetConfiguration()->getSQLLogger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function d3DisableLogger(): void
|
||||
{
|
||||
$this->d3GetConfiguration()->setSQLLogger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Configuration
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
protected function d3GetConfiguration(): Configuration
|
||||
{
|
||||
/** @var ConnectionProvider $connectionProvider */
|
||||
$connectionProvider = ContainerFactory::getInstance()->getContainer()
|
||||
->get(ConnectionProviderInterface::class);
|
||||
return $connectionProvider->get()->getConfiguration();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,43 +1,113 @@
|
|||
<?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;
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace D3\OxidSqlLogger;
|
||||
|
||||
use D3\ModCfg\Application\Model\d3database;
|
||||
use Doctrine\DBAL\Logging\SQLLogger;
|
||||
use Monolog;
|
||||
use NilPortugues\Sql\QueryFormatter\Formatter;
|
||||
|
||||
/**
|
||||
* Class OxidSQLLogger
|
||||
* @package tm\oxid\sql\logger
|
||||
*/
|
||||
class OxidSQLLogger implements SQLLogger
|
||||
{
|
||||
public string $message;
|
||||
public string $logStartingFile;
|
||||
public int $logStartingLine;
|
||||
public string $logStartingClass;
|
||||
public string $logStartingFunction;
|
||||
|
||||
private SQLQuery|null $SQLQuery = null;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @param string $file
|
||||
* @param int $line
|
||||
* @param string $class
|
||||
* @param string $function
|
||||
* @param string|null $message
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct(string $file, int $line, string $class, string $function, string $message = null)
|
||||
{
|
||||
if (!Monolog\Registry::hasLogger('sql')) {
|
||||
Monolog\Registry::addLogger((new LoggerFactory())->create('sql'));
|
||||
}
|
||||
|
||||
$this->message = (string) $message;
|
||||
$this->logStartingFile = $file;
|
||||
$this->logStartingLine = $line;
|
||||
$this->logStartingClass = $class;
|
||||
$this->logStartingFunction = $function;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array|null $params
|
||||
* @param array|null $types
|
||||
*/
|
||||
public function startQuery($sql, ?array $params = null, ?array $types = null): void
|
||||
{
|
||||
if ($this->SQLQuery) {
|
||||
$this->SQLQuery->setCanceled();
|
||||
$this->stopQuery();
|
||||
}
|
||||
|
||||
$this->getPreparedStatementQuery($sql, $params ?? [], $types ?? []);
|
||||
|
||||
$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
|
||||
* @param array $types
|
||||
*/
|
||||
public function getPreparedStatementQuery(string &$sql, array $params = [], array $types = []): void
|
||||
{
|
||||
if (class_exists(d3database::class)
|
||||
&& method_exists(d3database::class, 'getPreparedStatementQuery')
|
||||
&& count($params)
|
||||
&& ($query = d3database::getInstance()->getPreparedStatementQuery($sql, $params, $types))
|
||||
&& strlen(trim($query))
|
||||
) {
|
||||
$sql = $query;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function startQuery($sql, array $params = null, array $types = null)
|
||||
public function stopQuery(): void
|
||||
{
|
||||
Monolog\Registry::sql()->addDebug($sql, ['params' => $params, 'types' => $types]);
|
||||
}
|
||||
if ($this->SQLQuery) {
|
||||
$formatter = new Formatter();
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function stopQuery()
|
||||
{
|
||||
// @phpstan-ignore-next-line
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,235 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @author Tobias Matthaiou <developer@tobimat.eu>
|
||||
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace D3\OxidSqlLogger;
|
||||
|
||||
class SQLQuery
|
||||
{
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private float $start_time;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private float $stop_time = 0.0;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $sql = '';
|
||||
|
||||
private ?array $parameters = null;
|
||||
|
||||
private ?array $parameterTypes = null;
|
||||
|
||||
private string $logStartingFile;
|
||||
|
||||
private int $logStartingLine;
|
||||
|
||||
private string $logStartingClass;
|
||||
|
||||
private string $logStartingFunction;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->start_time = microtime(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getSql(): string
|
||||
{
|
||||
return $this->sql;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @return static
|
||||
*/
|
||||
public function setSql(string $sql): static
|
||||
{
|
||||
$this->sql = $sql;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
public function getParams(): ?array
|
||||
{
|
||||
return $this->parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|null $params
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setParams(array $params = null): static
|
||||
{
|
||||
$this->parameters = $params;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|null
|
||||
*/
|
||||
public function getTypes(): ?array
|
||||
{
|
||||
return $this->parameterTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|null $types
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setTypes(array $types = null): static
|
||||
{
|
||||
$this->parameterTypes = $types;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingFile(): string
|
||||
{
|
||||
return $this->logStartingFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $file
|
||||
* @return static
|
||||
*/
|
||||
public function setLogStartingFile(string $file): static
|
||||
{
|
||||
$this->logStartingFile = $file;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLogStartingLine(): int
|
||||
{
|
||||
return $this->logStartingLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $line
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setLogStartingLine(int $line): static
|
||||
{
|
||||
$this->logStartingLine = $line;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingClass(): string
|
||||
{
|
||||
return $this->logStartingClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $classname
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setLogStartingClass(string $classname): static
|
||||
{
|
||||
$this->logStartingClass = $classname;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getLogStartingFunction(): string
|
||||
{
|
||||
return $this->logStartingFunction;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $functionname
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setLogStartingFunction(string $functionname): static
|
||||
{
|
||||
$this->logStartingFunction = $functionname;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Statement was cancelled prematurely, an error was thrown.
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function setCanceled(): static
|
||||
{
|
||||
$this->start_time = 0.0;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return elapsed time
|
||||
* @return float
|
||||
*/
|
||||
public function getElapsedTime(): float
|
||||
{
|
||||
if ($this->start_time === 0.0) {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
if ($this->stop_time === 0.0) {
|
||||
$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(): string
|
||||
{
|
||||
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
|
||||
*/
|
||||
protected function readableElapsedTime(float $microtime, string $format = '%.3f%s', int $round = 3): string
|
||||
{
|
||||
if ($microtime >= 1) {
|
||||
$unit = 's';
|
||||
$time = round($microtime, $round);
|
||||
} else {
|
||||
$unit = 'ms';
|
||||
$time = round($microtime*1000);
|
||||
|
||||
$format = (string) preg_replace('/(%.\d+f)/', '%d', $format);
|
||||
}
|
||||
|
||||
return sprintf($format, $time, $unit);
|
||||
}
|
||||
}
|
|
@ -1,14 +1,52 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* 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();
|
||||
declare(strict_types=1);
|
||||
|
||||
use D3\OxidSqlLogger\OxidEsalesDatabase;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
/**
|
||||
* @param string|null $message
|
||||
*
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
function D3StartSQLLog(string $message = null): void
|
||||
{
|
||||
/** @var OxidEsalesDatabase $database */
|
||||
$database = oxNew(OxidEsalesDatabase::class);
|
||||
$database->d3EnableLogger($message);
|
||||
}
|
||||
|
||||
function StopSQLLog() {
|
||||
\tm\oxid\sql\logger\OxidEsalesDatabase::disableLogger();
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
function D3StopSQLLog(): void
|
||||
{
|
||||
/** @var OxidEsalesDatabase $database */
|
||||
$database = oxNew(OxidEsalesDatabase::class);
|
||||
$database->d3DisableLogger();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $message
|
||||
*
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
function D3AddSQLLogItem(string $message): void
|
||||
{
|
||||
/** @var OxidEsalesDatabase $database */
|
||||
$database = oxNew(OxidEsalesDatabase::class);
|
||||
if ($logger = $database->d3GetLogger()) {
|
||||
$logger->startQuery($message);
|
||||
$logger->stopQuery();
|
||||
}
|
||||
}
|
||||
|
|
Chargement…
Référencer dans un nouveau ticket