Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[MonologBridge] Add support for Monolog 3 #46153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 5 src/Symfony/Bridge/Monolog/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.1
---

* Add support for Monolog 3

6.0
---

Expand Down
57 changes: 57 additions & 0 deletions 57 src/Symfony/Bridge/Monolog/Formatter/CompatibilityFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Monolog\Formatter;

use Monolog\Logger;
use Monolog\LogRecord;

if (Logger::API >= 3) {
/**
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*
* @internal
*/
trait CompatibilityFormatter
{
abstract private function doFormat(array|LogRecord $record): mixed;

/**
* {@inheritdoc}
*/
public function format(LogRecord $record): mixed
{
return $this->doFormat($record);
}
}
} else {
/**
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*
* @internal
*/
trait CompatibilityFormatter
{
abstract private function doFormat(array|LogRecord $record): mixed;

/**
* {@inheritdoc}
*/
public function format(array $record): mixed
{
return $this->doFormat($record);
}
}
}
13 changes: 9 additions & 4 deletions 13 src/Symfony/Bridge/Monolog/Formatter/ConsoleFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Monolog\Formatter\FormatterInterface;
use Monolog\Logger;
use Monolog\LogRecord;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Cloner\Stub;
Expand All @@ -24,9 +25,13 @@
*
* @author Tobias Schultze <http://tobion.de>
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*
* @final since Symfony 6.1
*/
class ConsoleFormatter implements FormatterInterface
{
use CompatibilityFormatter;

public const SIMPLE_FORMAT = "%datetime% %start_tag%%level_name%%end_tag% <comment>[%channel%]</> %message%%context%%extra%\n";
public const SIMPLE_DATE = 'H:i:s';

Expand Down Expand Up @@ -98,11 +103,11 @@ public function formatBatch(array $records): mixed
return $records;
}

/**
* {@inheritdoc}
*/
public function format(array $record): mixed
private function doFormat(array|LogRecord $record): mixed
{
if ($record instanceof LogRecord) {
$record = $record->toArray();
}
$record = $this->replacePlaceHolder($record);

if (!$this->options['ignore_empty_context_and_extra'] || !empty($record['context'])) {
Expand Down
14 changes: 10 additions & 4 deletions 14 src/Symfony/Bridge/Monolog/Formatter/VarDumperFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,31 @@
namespace Symfony\Bridge\Monolog\Formatter;

use Monolog\Formatter\FormatterInterface;
use Monolog\LogRecord;
use Symfony\Component\VarDumper\Cloner\VarCloner;

/**
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*
* @final since Symfony 6.1
*/
class VarDumperFormatter implements FormatterInterface
{
use CompatibilityFormatter;

private VarCloner $cloner;

public function __construct(VarCloner $cloner = null)
{
$this->cloner = $cloner ?? new VarCloner();
}

/**
* {@inheritdoc}
*/
public function format(array $record): mixed
private function doFormat(array|LogRecord $record): mixed
{
if ($record instanceof LogRecord) {
$record = $record->toArray();
}

$record['context'] = $this->cloner->cloneVar($record['context']);
$record['extra'] = $this->cloner->cloneVar($record['extra']);

Expand Down
57 changes: 57 additions & 0 deletions 57 src/Symfony/Bridge/Monolog/Handler/CompatibilityHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Monolog\Handler;

use Monolog\Logger;
use Monolog\LogRecord;

if (Logger::API >= 3) {
/**
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*
* @internal
*/
trait CompatibilityHandler
{
abstract private function doHandle(array|LogRecord $record): bool;

/**
* {@inheritdoc}
*/
public function handle(LogRecord $record): bool
{
return $this->doHandle($record);
}
}
} else {
/**
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*
* @internal
*/
trait CompatibilityHandler
{
abstract private function doHandle(array|LogRecord $record): bool;

/**
* {@inheritdoc}
*/
public function handle(array $record): bool
{
return $this->doHandle($record);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Monolog\Handler;

use Monolog\Logger;
use Monolog\LogRecord;

if (Logger::API >= 3) {
/**
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*
* @internal
*/
trait CompatibilityProcessingHandler
{
abstract private function doWrite(array|LogRecord $record): void;

/**
* {@inheritdoc}
*/
protected function write(LogRecord $record): void
{
$this->doWrite($record);
}
}
} else {
/**
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*
* @internal
*/
trait CompatibilityProcessingHandler
{
abstract private function doWrite(array|LogRecord $record): void;

/**
* {@inheritdoc}
*/
protected function write(array $record): void
{
$this->doWrite($record);
}
}
}
58 changes: 52 additions & 6 deletions 58 src/Symfony/Bridge/Monolog/Handler/ConsoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Monolog\LogRecord;
use Symfony\Bridge\Monolog\Formatter\ConsoleFormatter;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
Expand All @@ -24,6 +25,48 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\VarDumper\Dumper\CliDumper;

if (Logger::API >= 3) {
/**
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*
* @internal
*/
trait CompatibilityIsHandlingHandler
{
abstract private function doIsHandling(array|LogRecord $record): bool;

/**
* {@inheritdoc}
*/
public function isHandling(LogRecord $record): bool
{
return $this->doIsHandling($record);
}
}
} else {
/**
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*
* @internal
*/
trait CompatibilityIsHandlingHandler
{
abstract private function doIsHandling(array|LogRecord $record): bool;

/**
* {@inheritdoc}
*/
public function isHandling(array $record): bool
{
return $this->doIsHandling($record);
}
}
}

/**
* Writes logs to the console output depending on its verbosity setting.
*
Expand All @@ -40,9 +83,15 @@
* This mapping can be customized with the $verbosityLevelMap constructor parameter.
*
* @author Tobias Schultze <http://tobion.de>
*
* @final since Symfony 6.1
*/
class ConsoleHandler extends AbstractProcessingHandler implements EventSubscriberInterface
{
use CompatibilityHandler;
use CompatibilityIsHandlingHandler;
use CompatibilityProcessingHandler;

private ?OutputInterface $output;
private array $verbosityLevelMap = [
OutputInterface::VERBOSITY_QUIET => Logger::ERROR,
Expand Down Expand Up @@ -75,15 +124,15 @@ public function __construct(OutputInterface $output = null, bool $bubble = true,
/**
* {@inheritdoc}
*/
public function isHandling(array $record): bool
private function doIsHandling(array|LogRecord $record): bool
{
return $this->updateLevel() && parent::isHandling($record);
}

/**
* {@inheritdoc}
*/
public function handle(array $record): bool
private function doHandle(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 @@ -141,10 +190,7 @@ public static function getSubscribedEvents(): array
];
}

/**
* {@inheritdoc}
*/
protected function write(array $record): void
private function doWrite(array|LogRecord $record): void
{
// at this point we've determined for sure that we want to output the record, so use the output's own verbosity
$this->output->write((string) $record['formatted'], false, $this->output->getVerbosity());
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.