bug #34697 [MonologBridge] Fix compatibility of ServerLogHandler with Monolog 2 (jderusse)

This PR was merged into the 5.0 branch.

Discussion
----------

[MonologBridge] Fix compatibility of ServerLogHandler with Monolog 2

| Q             | A
| ------------- | ---
| Branch?       | 5.0
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | #34520
| License       | MIT
| Doc PR        | NA

This is an alternative to #34521 that keep compatibility with "processors" and "formaters".

Commits
-------

bdb10f7cd5 Fix compatibility with Monolog 2
This commit is contained in:
Nicolas Grekas 2019-11-29 17:39:52 +01:00
commit 5a307932d9

View File

@ -12,14 +12,43 @@
namespace Symfony\Bridge\Monolog\Handler;
use Monolog\Formatter\FormatterInterface;
use Monolog\Handler\AbstractHandler;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Handler\FormattableHandlerTrait;
use Monolog\Logger;
use Symfony\Bridge\Monolog\Formatter\VarDumperFormatter;
if (trait_exists(FormattableHandlerTrait::class)) {
class ServerLogHandler extends AbstractProcessingHandler
{
use ServerLogHandlerTrait;
/**
* {@inheritdoc}
*/
protected function getDefaultFormatter(): FormatterInterface
{
return new VarDumperFormatter();
}
}
} else {
class ServerLogHandler extends AbstractProcessingHandler
{
use ServerLogHandlerTrait;
/**
* {@inheritdoc}
*/
protected function getDefaultFormatter()
{
return new VarDumperFormatter();
}
}
}
/**
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
class ServerLogHandler extends AbstractHandler
trait ServerLogHandlerTrait
{
private $host;
private $context;
@ -56,6 +85,11 @@ class ServerLogHandler extends AbstractHandler
restore_error_handler();
}
return parent::handle($record);
}
protected function write(array $record): void
{
$recordFormatted = $this->formatRecord($record);
set_error_handler(self::class.'::nullErrorHandler');
@ -72,16 +106,12 @@ class ServerLogHandler extends AbstractHandler
} finally {
restore_error_handler();
}
return false === $this->bubble;
}
/**
* {@inheritdoc}
*
* @return FormatterInterface
*/
protected function getDefaultFormatter()
protected function getDefaultFormatter(): FormatterInterface
{
return new VarDumperFormatter();
}
@ -103,13 +133,7 @@ class ServerLogHandler extends AbstractHandler
private function formatRecord(array $record): string
{
if ($this->processors) {
foreach ($this->processors as $processor) {
$record = $processor($record);
}
}
$recordFormatted = $this->getFormatter()->format($record);
$recordFormatted = $record['formatted'];
foreach (['log_uuid', 'uuid', 'uid'] as $key) {
if (isset($record['extra'][$key])) {