36 Commits
1.0.0 ... 1.2.0

Author SHA1 Message Date
07fcca498d remove Query Status Monitor, complete implementation 2020-05-01 01:08:23 +02:00
c3aa93e543 disable linking smarty plugin 2020-05-01 00:29:56 +02:00
054ccd8419 Merge remote-tracking branch 'upstream/master' 2020-05-01 00:13:59 +02:00
8df59407d9 add SQL formatter 2020-04-30 20:45:41 +02:00
40012ba334 Suitable screenshot 2019-11-06 15:10:45 +01:00
b606986b86 Update CHANGELOG.md 2019-11-06 15:05:36 +01:00
e57e3fe934 Merge pull request #2 from TumTum/feature/sql-status
Feature/sql status
2019-11-06 15:03:02 +01:00
e0bd400107 Documentation to SQL Status Query Monitor 2019-11-06 15:00:55 +01:00
7e6687132c Status display of the DB how many queries were processed. (SHOW STATUS) 2019-11-06 14:44:58 +01:00
2410dc3203 Update README.md 2019-10-04 10:15:41 +02:00
e3623e4bd4 Update CHANGELOG.md 2019-10-01 16:17:07 +02:00
466bb61afc Merge pull request #1 from TumTum/feature/measures_time
Feature/measures time
2019-10-01 12:58:50 +02:00
5fd1cf0b81 Updated documentation 2019-10-01 12:55:33 +02:00
2488b19301 Activate Travis 2019-10-01 12:30:25 +02:00
9c3206ee73 New Featuer: "Measures the sql execution time" 2019-10-01 12:23:02 +02:00
5de9a138f8 Insert PHPSpec Framework 2019-10-01 12:22:43 +02:00
b58b674c3a add credits 2019-09-23 08:45:00 +02:00
7da3397db4 fix README images url 2019-09-23 08:38:19 +02:00
3daecc030a remove unused files 2019-09-21 13:21:11 +02:00
5b9328fd73 Merge branch 'dev_impovedFormat' 2019-09-21 13:13:59 +02:00
d21a3f892d allow newlines in cli output, add FirePHP handler to browser console log 2019-09-20 22:30:27 +02:00
bd16bf36cd adjust documentation 2019-09-20 16:05:36 +02:00
7705abd09e autogenerate log position informations,
does not need log positions parameters anymore when called
2019-09-20 10:40:01 +02:00
90c055d1cb prevent own method log traces 2019-09-20 10:37:44 +02:00
6079ae7cf6 add fallback in case of not set headline 2019-09-19 20:04:25 +02:00
d8c60ce2c2 change namespace 2019-09-19 19:52:54 +02:00
ceee2ca96c change composer.json in dev branch 2019-09-19 17:13:13 +02:00
e9310cda53 change composer.json in master branch 2019-09-19 17:11:07 +02:00
156429f7ea add headline and information about calling script, add logger getter 2019-09-19 16:03:06 +02:00
35270ae271 Version 1.0.0 2019-08-21 08:20:36 +02:00
8f633a1ed8 Update README.md 2019-08-21 08:15:58 +02:00
fe46283d43 Log output now in color on the shell console 2019-08-21 08:08:09 +02:00
a8eeb4d4e4 Update README.md 2019-08-21 00:43:33 +02:00
cb35b27522 Documentaion written more detailed 2019-08-21 00:36:20 +02:00
1d627808d0 Initial commit 2019-08-20 23:35:53 +02:00
06b33cd4d5 Initial commit 2019-08-20 23:33:18 +02:00
13 changed files with 410 additions and 45 deletions

View File

@ -6,8 +6,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
---
## [1.2.0] - 2020-04-30
### Added
- Format SQL query
- Measures the SQL execution time
---
## [1.1.0] - 2019-09-20
### Added
- add log message
- add position specifications of the calling source code
---
## [1.0.0] - 2019-07-21 ## [1.0.0] - 2019-07-21
available in tumtum/oxid-sql-logger package only
### Added ### Added
- Global function `StartSQLLog` and `StopSQLLog` - Global function `StartSQLLog` and `StopSQLLog`

View File

@ -5,29 +5,31 @@ Returns all SQL queries into console of a Browser.
## Install ## Install
`composer require --dev tumtum/oxid-sql-logger` `composer require --dev d3/oxid-sql-logger`
## Usage ## 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 ```php
\StartSQLLog(); \D3StartSQLLog('specific log message');
$db = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC); $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]); $list = $db->getAll('SELECT * FROM oxarticles WHERE oxprice < ? LIMIT 100', [49.99]);
\StopSQLLog(); \D3StopSQLLog();
``` ```
## Screenshots ## Screenshots
Browser: Browser:
![Example all sqls](https://raw.githubusercontent.com/TumTum/oxid-sql-logger/master/img/screenshot-a.jpg) ![Example all sqls](https://raw.githubusercontent.com/d3datadevelopment/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: 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)
## Credits
Many thanks to [Tobias Matthaiou](https://github.com/TumTum/oxid-sql-logger) for his inspiration.

View File

@ -1,23 +1,26 @@
{ {
"name": "tumtum/oxid-sql-logger", "name": "d3/oxid-sql-logger",
"description": "Returns all SQL queries in the browser.", "description": "Returns all SQL queries in the browser.",
"type": "library", "type": "library",
"require": { "require": {
"monolog/monolog": "^1" "monolog/monolog": "^1",
"nilportugues/sql-query-formatter": "^1.2.2"
}, },
"license": "GPL-3.0", "license": "GPL-3.0",
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"tm\\oxid\\sql\\logger\\": "./src" "D3\\OxidSqlLogger\\": "./src"
}, }
"files": [
"./src/functions.php"
]
}, },
"authors": [ "authors": [
{ {
"name": "Tobias Matthaiou", "name": "Tobias Matthaiou",
"email": "developer@tobimat.eu" "email": "developer@tobimat.eu"
},
{
"name": "D3 Data Development (Inh. Thomas Dartsch)",
"email": "info@shopmodule.com",
"homepage": "http://www.d3data.de"
} }
] ]
} }

View File

@ -2,9 +2,18 @@
require __DIR__ . '/../../../../source/bootstrap.php'; 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); $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]); $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

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 50 KiB

View File

@ -1,17 +1,17 @@
<?php <?php
/** /**
* Autor: Tobias Matthaiou <developer@tobimat.eu> * @author Tobias Matthaiou <developer@tobimat.eu>
* Date: 2019-08-21 * @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* Time: 07:50
*/ */
namespace tm\oxid\sql\logger;
namespace D3\OxidSqlLogger;
use Monolog; use Monolog;
use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Symfony\Component\Console\Formatter\OutputFormatterStyle;
/** /**
* Class Factory * Class Factory
* @package tm\oxid\sql\logger
*/ */
class LoggerFactory class LoggerFactory
{ {
@ -34,6 +34,7 @@ class LoggerFactory
$handlers[] = $this->getStreamHandler(); $handlers[] = $this->getStreamHandler();
} else { } else {
$handlers[] = $this->getBrowserConsoleHandler(); $handlers[] = $this->getBrowserConsoleHandler();
$handlers[] = $this->getFirePHPHandler();
} }
return $handlers; return $handlers;
} }
@ -53,7 +54,14 @@ class LoggerFactory
$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; return $streamHandler;
} }
@ -66,13 +74,21 @@ class LoggerFactory
return new Monolog\Handler\BrowserConsoleHandler(); return new Monolog\Handler\BrowserConsoleHandler();
} }
/**
* @return Monolog\Handler\FirePHPHandler
*/
private function getFirePHPHandler()
{
return new Monolog\Handler\FirePHPHandler();
}
/** /**
* @return array * @return array
*/ */
private function getProcessors() private function getProcessors()
{ {
return [ 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(), new Monolog\Processor\PsrLogMessageProcessor(),
]; ];
} }

View File

@ -1,27 +1,46 @@
<?php <?php
/** /**
* Autor: Tobias Matthaiou <developer@tobimat.eu> * @author Tobias Matthaiou <developer@tobimat.eu>
* Date: 2019-08-20 * @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* Time: 21:56
*/ */
namespace tm\oxid\sql\logger; namespace D3\OxidSqlLogger;
use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Configuration;
/** /**
* Class OxidEsalesDatabase * Class OxidEsalesDatabase
* Is a depenction injection Helper Class * Is a depenction injection Helper Class
*
* @package tm\oxid\sql\logger
*/ */
class OxidEsalesDatabase extends \OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database 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); $database = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
$dbalConfig = $database->getConnection()->getConfiguration(); $dbalConfig = $database->getConnection()->getConfiguration();
$dbalConfig->setSQLLogger(new OxidSQLLogger()); return $dbalConfig->getSQLLogger();
} }
public static function disableLogger() public static function disableLogger()

View File

@ -1,29 +1,46 @@
<?php <?php
/** /**
* Autor: Tobias Matthaiou <developer@tobimat.eu> * @author Tobias Matthaiou <developer@tobimat.eu>
* Date: 2019-08-20 * @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* Time: 21:33
*/ */
namespace tm\oxid\sql\logger; namespace D3\OxidSqlLogger;
use Doctrine\DBAL\Logging\SQLLogger; use Doctrine\DBAL\Logging\SQLLogger;
use Monolog; use Monolog;
use NilPortugues\Sql\QueryFormatter\Formatter;
/** /**
* Class OxidSQLLogger * Class OxidSQLLogger
* @package tm\oxid\sql\logger
*/ */
class OxidSQLLogger implements SQLLogger class OxidSQLLogger implements SQLLogger
{ {
public $message;
public $logStartingFile;
public $logStartingLine;
public $logStartingClass;
public $logStartingFunction;
/**
* @var SQLQuery
*/
private $SQLQuery = null;
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function __construct() public function __construct($file, $line, $class, $function, $message = null)
{ {
if (!Monolog\Registry::hasLogger('sql')) { if (!Monolog\Registry::hasLogger('sql')) {
Monolog\Registry::addLogger((new LoggerFactory())->create('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 +48,18 @@ class OxidSQLLogger implements SQLLogger
*/ */
public function startQuery($sql, array $params = null, array $types = null) 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->SQLQuery = (new SQLQuery()) ->setSql($sql)
->setParams($params)
->setTypes($types)
->setLogStartingFile($this->logStartingFile)
->setLogStartingLine($this->logStartingLine)
->setLogStartingClass($this->logStartingClass)
->setLogStartingFunction($this->logStartingFunction);
} }
/** /**
@ -39,5 +67,24 @@ class OxidSQLLogger implements SQLLogger
*/ */
public function stopQuery() public function stopQuery()
{ {
if ($this->SQLQuery) {
$formatter = new Formatter();
Monolog\Registry::sql()->addDebug(
'['.$this->SQLQuery->getReadableElapsedTime().'] ' . ( $this->message ? $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;
} }
} }

247
src/SQLQuery.php Normal file
View File

@ -0,0 +1,247 @@
<?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 = null;
/**
* @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;
/**
* @inheritDoc
*/
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)
* @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);
}
}

View File

@ -1,14 +1,14 @@
<?php <?php
/** /**
* Autor: Tobias Matthaiou <developer@tobimat.eu> * @author Tobias Matthaiou <developer@tobimat.eu>
* Date: 2019-08-20 * @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* Time: 23:11
*/ */
function StartSQLLog() { function D3StartSQLLog($message = null) {
\tm\oxid\sql\logger\OxidEsalesDatabase::enableLogger(); \D3\OxidSqlLogger\OxidEsalesDatabase::enableLogger($message);
} }
function StopSQLLog() { function D3StopSQLLog() {
\tm\oxid\sql\logger\OxidEsalesDatabase::disableLogger(); \D3\OxidSqlLogger\OxidEsalesDatabase::disableLogger();
} }