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