add configurable FirePHP handler

This commit is contained in:
Daniel Seifert 2020-05-13 18:53:56 +02:00
parent 491ceeac5a
commit 806de37cc9
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
4 changed files with 167 additions and 5 deletions

View File

@ -4,7 +4,8 @@
"type": "library",
"require": {
"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",
"autoload": {

View File

@ -0,0 +1,79 @@
<?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 \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)
));
}
}

View File

@ -0,0 +1,43 @@
<?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 OxidEsales\Eshop\Core\Registry;
class d3FirePHPHandler extends \Monolog\Handler\AbstractProcessingHandler
{
const ADD_TRACE = 'addTrace';
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', 'trace' );
$fp->groupEnd();
}
}
}

View File

@ -8,6 +8,7 @@
namespace D3\OxidSqlLogger;
use Monolog;
use OxidEsales\Eshop\Core\Registry;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
/**
@ -29,16 +30,54 @@ 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();
$handlers[] = $this->getFirePHPHandler();
$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) || (is_object($obj) && ($obj instanceof \Traversable));
}
/**
* @return Monolog\Handler\StreamHandler
*/