Skip to content

Commit

Permalink
Merge pull request #215 from pauliuspetronis/213-monolog-3
Browse files Browse the repository at this point in the history
Support for monolog/monolog ^3.0
  • Loading branch information
gordalina committed Oct 20, 2022
2 parents f977b94 + 93488aa commit 9fc69f4
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 31 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"hollodotme/fast-cgi-client": "^3.0",
"monolog/monolog": "^1.0|^2.0",
"monolog/monolog": "^1.0|^2.0|^3.0",
"php": ">=8.0.0",
"psr/log": "^1.0|^2.0|^3.0",
"symfony/console": "^4.0|^5.0|^6.0",
Expand Down
46 changes: 33 additions & 13 deletions src/Monolog/ConsoleFormatter.php
Expand Up @@ -12,7 +12,9 @@
namespace CacheTool\Monolog;

use Monolog\Formatter\LineFormatter;
use Monolog\Level;
use Monolog\Logger;
use Monolog\LogRecord;

/**
* Formats incoming records for console output by coloring them depending on log level.
Expand All @@ -21,27 +23,45 @@
*/
class ConsoleFormatter extends LineFormatter
{
const SIMPLE_FORMAT = "%start_tag%[%datetime%] %channel%.%level_name%:%end_tag% %message% %context% %extra%\n";
const SIMPLE_FORMAT = "%extra.start_tag%[%datetime%] %channel%.%level_name%:%extra.end_tag% %message% %context% %extra%\n";

/**
* {@inheritdoc}
*/
public function format(array $record): string
public function format(array|LogRecord $record): string
{
if ($record['level'] >= Logger::ERROR) {
$record['start_tag'] = '<error>';
$record['end_tag'] = '</error>';
} elseif ($record['level'] >= Logger::NOTICE) {
$record['start_tag'] = '<comment>';
$record['end_tag'] = '</comment>';
} elseif ($record['level'] >= Logger::INFO) {
$record['start_tag'] = '<info>';
$record['end_tag'] = '</info>';
if (Logger::API < 3) {
if ($record['level'] >= Logger::ERROR) {
$record = $this->addTags($record, 'error');
} elseif ($record['level'] >= Logger::NOTICE) {
$record = $this->addTags($record, 'comment');
} elseif ($record['level'] >= Logger::INFO) {
$record = $this->addTags($record, 'info');
} else {
$record = $this->addTags($record);
}

return parent::format($record);
}

if (Level::Error->includes($record->level)) {
$this->addTags($record, 'error');
} elseif (Level::Notice->includes($record->level)) {
$this->addTags($record, 'comment');
} elseif (Level::Info->includes($record->level)) {
$this->addTags($record, 'info');
} else {
$record['start_tag'] = '';
$record['end_tag'] = '';
$this->addTags($record);
}

return parent::format($record);
}

private function addTags(array|LogRecord $record, ?string $tag = null): array|LogRecord
{
$record['extra']['start_tag'] = $tag ? sprintf('<%s>', $tag) : '';
$record['extra']['end_tag'] = $tag ? sprintf('</%s>', $tag) : '';

return $record;
}
}
39 changes: 22 additions & 17 deletions src/Monolog/ConsoleHandler.php
Expand Up @@ -13,7 +13,9 @@

use Monolog\Formatter\FormatterInterface;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Level;
use Monolog\Logger;
use Monolog\LogRecord;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -36,15 +38,9 @@
*/
class ConsoleHandler extends AbstractProcessingHandler
{
/**
* @var OutputInterface|null
*/
private $output;
private OutputInterface|null $output;

/**
* @var array
*/
private $verbosityLevelMap = [
private array $verbosityLevelMap = [
OutputInterface::VERBOSITY_NORMAL => Logger::WARNING,
OutputInterface::VERBOSITY_VERBOSE => Logger::NOTICE,
OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::INFO,
Expand All @@ -54,11 +50,11 @@ class ConsoleHandler extends AbstractProcessingHandler
/**
* Constructor.
*
* @param OutputInterface|null $output The console output to use (the handler remains disabled when passing null
* until the output is set, e.g. by using console events)
* @param bool $bubble Whether the messages that are handled can bubble up the stack
* @param array $verbosityLevelMap Array that maps the OutputInterface verbosity to a minimum logging
* level (leave empty to use the default mapping)
* @param OutputInterface|null $output The console output to use (the handler remains disabled when passing null
* until the output is set, e.g. by using console events)
* @param bool $bubble Whether the messages that are handled can bubble up the stack
* @param array<int, int|Level> $verbosityLevelMap Array that maps the OutputInterface verbosity to a minimum logging
* level (leave empty to use the default mapping)
*/
public function __construct(OutputInterface $output = null, $bubble = true, array $verbosityLevelMap = [])
{
Expand All @@ -73,15 +69,15 @@ public function __construct(OutputInterface $output = null, $bubble = true, arra
/**
* {@inheritdoc}
*/
public function isHandling(array $record): bool
public function isHandling(array|LogRecord $record): bool
{
return $this->updateLevel() && parent::isHandling($record);
}

/**
* {@inheritdoc}
*/
public function handle(array $record): bool
public function handle(array|LogRecord $record): bool
{
// we have to update the logging level each time because the verbosity of the
// console output might have changed in the meantime (it is not immutable)
Expand Down Expand Up @@ -111,9 +107,9 @@ public function close(): void
/**
* {@inheritdoc}
*/
protected function write(array $record): void
protected function write(array|LogRecord $record): void
{
if ($record['level'] >= Logger::ERROR && $this->output instanceof ConsoleOutputInterface) {
if ($this->isError($record) && $this->output instanceof ConsoleOutputInterface) {
$this->output->getErrorOutput()->write((string) $record['formatted']);
} else {
$this->output->write((string) $record['formatted']);
Expand Down Expand Up @@ -147,4 +143,13 @@ private function updateLevel()

return true;
}

private function isError(array|LogRecord $record): bool
{
if (Logger::API < 3) {
return $record['level'] >= Logger::ERROR;
}

return Level::Error->includes($record->level);
}
}

0 comments on commit 9fc69f4

Please sign in to comment.