diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b808e9..c00f429 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,8 +6,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.1.0] - 2019-09-20 + +### Added + +- add log message +- add position specifications of the calling source code + ## [1.0.0] - 2019-07-21 +available in tumtum/oxid-sql-logger package only + ### Added - Global function `StartSQLLog` and `StopSQLLog` diff --git a/README.md b/README.md index 7201f53..8370e35 100644 --- a/README.md +++ b/README.md @@ -5,19 +5,19 @@ 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 @@ -25,9 +25,8 @@ $list = $db->getAll('SELECT * FROM oxarticles WHERE oxprice < ? LIMIT 100', [49. 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) CLI: -![Example CLI](https://raw.githubusercontent.com/TumTum/oxid-sql-logger/master/img/screenshot-cli.png) +![Example CLI](https://raw.githubusercontent.com/TumTum/oxid-sql-logger/master/img/screenshot-cli.jpg) diff --git a/example/simple.php b/example/simple.php index 113492d..5d02ac9 100644 --- a/example/simple.php +++ b/example/simple.php @@ -2,9 +2,18 @@ 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); +$list = $db->getAll('SELECT * '.PHP_EOL.'FROM oxarticles WHERE oxprice < ? LIMIT 100', [49.99]); + +\D3StopSQLLog(); + +// or + +\D3StartSQLLog(); $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(); diff --git a/img/screenshot-a.jpg b/img/screenshot-a.jpg index da7c347..7d5390c 100644 Binary files a/img/screenshot-a.jpg and b/img/screenshot-a.jpg differ diff --git a/img/screenshot-cli.jpg b/img/screenshot-cli.jpg new file mode 100644 index 0000000..a59597a Binary files /dev/null and b/img/screenshot-cli.jpg differ diff --git a/src/LoggerFactory.php b/src/LoggerFactory.php index 5f156ea..d1643cc 100644 --- a/src/LoggerFactory.php +++ b/src/LoggerFactory.php @@ -1,17 +1,17 @@ - * Date: 2019-08-21 - * Time: 07:50 + * @author Tobias Matthaiou + * @author D3 Data Development - Daniel Seifert */ -namespace tm\oxid\sql\logger; + +namespace D3\OxidSqlLogger; use Monolog; use Symfony\Component\Console\Formatter\OutputFormatterStyle; /** * Class Factory - * @package tm\oxid\sql\logger */ class LoggerFactory { @@ -34,6 +34,7 @@ class LoggerFactory $handlers[] = $this->getStreamHandler(); } else { $handlers[] = $this->getBrowserConsoleHandler(); + $handlers[] = $this->getFirePHPHandler(); } return $handlers; } @@ -53,7 +54,14 @@ class LoggerFactory $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 +74,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(), ]; } diff --git a/src/OxidEsalesDatabase.php b/src/OxidEsalesDatabase.php index b3d85d8..13015f7 100644 --- a/src/OxidEsalesDatabase.php +++ b/src/OxidEsalesDatabase.php @@ -1,27 +1,46 @@ - * Date: 2019-08-20 - * Time: 21:56 + * @author Tobias Matthaiou + * @author D3 Data Development - Daniel Seifert */ -namespace tm\oxid\sql\logger; +namespace D3\OxidSqlLogger; use Doctrine\DBAL\Configuration; /** * Class OxidEsalesDatabase * Is a depenction injection Helper Class - * - * @package tm\oxid\sql\logger */ class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database { - public static function enableLogger() + public static function enableLogger($message = null) + { + $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); + $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, + $message + ) + ); + } + + /** + * @return OxidSQLLogger + * @throws \OxidEsales\Eshop\Core\Exception\DatabaseConnectionException + */ + public static function getLogger() { $database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC); $dbalConfig = $database->getConnection()->getConfiguration(); - $dbalConfig->setSQLLogger(new OxidSQLLogger()); + return $dbalConfig->getSQLLogger(); } public static function disableLogger() diff --git a/src/OxidSQLLogger.php b/src/OxidSQLLogger.php index 7fbe8be..3f3a7aa 100644 --- a/src/OxidSQLLogger.php +++ b/src/OxidSQLLogger.php @@ -1,29 +1,40 @@ - * Date: 2019-08-20 - * Time: 21:33 + * @author Tobias Matthaiou + * @author D3 Data Development - Daniel Seifert */ -namespace tm\oxid\sql\logger; +namespace D3\OxidSqlLogger; use Doctrine\DBAL\Logging\SQLLogger; use Monolog; /** * Class OxidSQLLogger - * @package tm\oxid\sql\logger */ class OxidSQLLogger implements SQLLogger { + public $message; + public $logStartingFile; + public $logStartingLine; + public $logStartingClass; + public $logStartingFunction; + /** * @inheritDoc */ - public function __construct() + 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; } /** @@ -31,7 +42,19 @@ class OxidSQLLogger implements SQLLogger */ public function startQuery($sql, array $params = null, array $types = null) { - Monolog\Registry::sql()->addDebug($sql, ['params' => $params, 'types' => $types]); + Monolog\Registry::sql()->addDebug( + $this->message ? $this->message : $sql, + [ + 'query' => $sql, + 'params' => $params, + 'types' => $types, + 'logStartingFile' => $this->logStartingFile, + 'logStartingLine' => $this->logStartingLine, + 'logStartingClass' => $this->logStartingClass, + 'logStartingFunction' => $this->logStartingFunction, + + ] + ); } /** diff --git a/src/functions.php b/src/functions.php index 5c2dec9..0571331 100644 --- a/src/functions.php +++ b/src/functions.php @@ -1,14 +1,14 @@ - * Date: 2019-08-20 - * Time: 23:11 + * @author Tobias Matthaiou + * @author D3 Data Development - Daniel Seifert */ -function StartSQLLog() { - \tm\oxid\sql\logger\OxidEsalesDatabase::enableLogger(); +function D3StartSQLLog($message = null) { + \D3\OxidSqlLogger\OxidEsalesDatabase::enableLogger($message); } -function StopSQLLog() { - \tm\oxid\sql\logger\OxidEsalesDatabase::disableLogger(); +function D3StopSQLLog() { + \D3\OxidSqlLogger\OxidEsalesDatabase::disableLogger(); }