Log output now in color on the shell console
This commit is contained in:
parent
a8eeb4d4e4
commit
fe46283d43
79
src/LoggerFactory.php
Normal file
79
src/LoggerFactory.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* Autor: Tobias Matthaiou <developer@tobimat.eu>
|
||||
* Date: 2019-08-21
|
||||
* Time: 07:50
|
||||
*/
|
||||
namespace tm\oxid\sql\logger;
|
||||
|
||||
use Monolog;
|
||||
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
|
||||
|
||||
/**
|
||||
* Class Factory
|
||||
* @package tm\oxid\sql\logger
|
||||
*/
|
||||
class LoggerFactory
|
||||
{
|
||||
/**
|
||||
* @param $name
|
||||
* @return Monolog\Logger
|
||||
*/
|
||||
public function create($name)
|
||||
{
|
||||
return new Monolog\Logger($name, $this->getHandlers(), $this->getProcessors());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function getHandlers()
|
||||
{
|
||||
$handlers = [];
|
||||
if (PHP_SAPI == 'cli') {
|
||||
$handlers[] = $this->getStreamHandler();
|
||||
} else {
|
||||
$handlers[] = $this->getBrowserConsoleHandler();
|
||||
}
|
||||
return $handlers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Monolog\Handler\StreamHandler
|
||||
*/
|
||||
private function getStreamHandler()
|
||||
{
|
||||
$streamHandler = new Monolog\Handler\StreamHandler('php://stderr');
|
||||
|
||||
$channel = (new OutputFormatterStyle(null, null, ['bold']))->apply('%channel%');
|
||||
$level_name = (new OutputFormatterStyle('blue'))->apply('%level_name%');
|
||||
$message = (new OutputFormatterStyle('green'))->apply('%message%');
|
||||
$context = (new OutputFormatterStyle('yellow'))->apply('%context%');
|
||||
$newline = PHP_EOL . str_repeat(' ', 10);
|
||||
|
||||
$ttl_color = "$channel $level_name: $message {$newline} $context {$newline} %extra%" . PHP_EOL;
|
||||
|
||||
$streamHandler->setFormatter(new Monolog\Formatter\LineFormatter($ttl_color));
|
||||
|
||||
return $streamHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Monolog\Handler\BrowserConsoleHandler
|
||||
*/
|
||||
private function getBrowserConsoleHandler()
|
||||
{
|
||||
return new Monolog\Handler\BrowserConsoleHandler();
|
||||
}
|
||||
|
||||
/**
|
||||
* @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\PsrLogMessageProcessor(),
|
||||
];
|
||||
}
|
||||
}
|
@ -22,18 +22,7 @@ class OxidSQLLogger implements SQLLogger
|
||||
public function __construct()
|
||||
{
|
||||
if (!Monolog\Registry::hasLogger('sql')) {
|
||||
Monolog\Registry::addLogger(
|
||||
new Monolog\Logger(
|
||||
'sql',
|
||||
[
|
||||
new Monolog\Handler\BrowserConsoleHandler(),
|
||||
],
|
||||
[
|
||||
new Monolog\Processor\IntrospectionProcessor(Monolog\Logger::DEBUG, ['tm\\oxid\\sql\\logger', 'Doctrine\\DBAL\\Connection', 'OxidEsales\\EshopCommunity\\Core\\Database\\Adapter\\Doctrine\\Database']),
|
||||
new Monolog\Processor\PsrLogMessageProcessor(),
|
||||
]
|
||||
)
|
||||
);
|
||||
Monolog\Registry::addLogger((new LoggerFactory())->create('sql'));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user