Skip to content
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 UPGRADE-7.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ Mime
* Remove `Email::attachPart()` method, use `Email::addPart()` instead
* Require explicit argument when calling `Message::setBody()`

MonologBridge
-------------

* Drop support for monolog < 3.0

PropertyAccess
--------------

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
"guzzlehttp/promises": "^1.4",
"league/html-to-markdown": "^5.0",
"masterminds/html5": "^2.7.2",
"monolog/monolog": "^1.25.1|^2",
"monolog/monolog": "^3.0",
"nyholm/psr7": "^1.0",
"pda/pheanstalk": "^4.0",
"php-http/discovery": "^1.15",
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/Monolog/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.0
---

* Drop support for monolog < 3.0

6.4
---

Expand Down
17 changes: 15 additions & 2 deletions src/Symfony/Bridge/Monolog/Command/ServerLogCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

use Monolog\Formatter\FormatterInterface;
use Monolog\Handler\HandlerInterface;
use Monolog\Logger;
use Monolog\Level;
use Monolog\LogRecord;
use Symfony\Bridge\Monolog\Formatter\ConsoleFormatter;
use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
use Symfony\Component\Console\Attribute\AsCommand;
Expand Down Expand Up @@ -86,7 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$this->handler = new ConsoleHandler($output, true, [
OutputInterface::VERBOSITY_NORMAL => Logger::DEBUG,
OutputInterface::VERBOSITY_NORMAL => Level::Debug,
]);

$this->handler->setFormatter(new ConsoleFormatter([
Expand Down Expand Up @@ -153,6 +154,18 @@ private function displayLog(OutputInterface $output, int $clientId, array $recor
$logBlock = sprintf('<bg=%s> </>', self::BG_COLOR[$clientId % 8]);
$output->write($logBlock);

$record = new LogRecord(
$record['datetime'],
$record['channel'],
Level::fromValue($record['level']),
$record['message'],
// We wrap context and extra, because they have been already dumped.
// So they are instance of Symfony\Component\VarDumper\Cloner\Data
// But LogRecord expects array
['data' => $record['context']],
['data' => $record['extra']],
);

$this->handler->handle($record);
}
}
51 changes: 0 additions & 51 deletions src/Symfony/Bridge/Monolog/Formatter/CompatibilityFormatter.php

This file was deleted.

61 changes: 28 additions & 33 deletions src/Symfony/Bridge/Monolog/Formatter/ConsoleFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Symfony\Bridge\Monolog\Formatter;

use Monolog\Formatter\FormatterInterface;
use Monolog\Logger;
use Monolog\Level;
use Monolog\LogRecord;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\VarDumper\Cloner\Data;
Expand All @@ -28,20 +28,18 @@
*/
final 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';

private const LEVEL_COLOR_MAP = [
Logger::DEBUG => 'fg=white',
Logger::INFO => 'fg=green',
Logger::NOTICE => 'fg=blue',
Logger::WARNING => 'fg=cyan',
Logger::ERROR => 'fg=yellow',
Logger::CRITICAL => 'fg=red',
Logger::ALERT => 'fg=red',
Logger::EMERGENCY => 'fg=white;bg=red',
Level::Debug->value => 'fg=white',
Level::Info->value => 'fg=green',
Level::Notice->value => 'fg=blue',
Level::Warning->value => 'fg=cyan',
Level::Error->value => 'fg=yellow',
Level::Critical->value => 'fg=red',
Level::Alert->value => 'fg=red',
Level::Emergency->value => 'fg=white;bg=red',
];

private array $options;
Expand Down Expand Up @@ -98,34 +96,31 @@ public function formatBatch(array $records): mixed
return $records;
}

private function doFormat(array|LogRecord $record): mixed
public function format(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'])) {
$context = ($this->options['multiline'] ? "\n" : ' ').$this->dumpData($record['context']);
if (!$this->options['ignore_empty_context_and_extra'] || !empty($record->context)) {
$context = $record->context;
$context = ($this->options['multiline'] ? "\n" : ' ').$this->dumpData($context);
} else {
$context = '';
}

if (!$this->options['ignore_empty_context_and_extra'] || !empty($record['extra'])) {
$extra = ($this->options['multiline'] ? "\n" : ' ').$this->dumpData($record['extra']);
if (!$this->options['ignore_empty_context_and_extra'] || !empty($record->extra)) {
$extra = $record->extra;
$extra = ($this->options['multiline'] ? "\n" : ' ').$this->dumpData($extra);
} else {
$extra = '';
}

$formatted = strtr($this->options['format'], [
'%datetime%' => $record['datetime'] instanceof \DateTimeInterface
? $record['datetime']->format($this->options['date_format'])
: $record['datetime'],
'%start_tag%' => sprintf('<%s>', self::LEVEL_COLOR_MAP[$record['level']]),
'%level_name%' => sprintf($this->options['level_name_format'], $record['level_name']),
'%datetime%' => $record->datetime->format($this->options['date_format']),
'%start_tag%' => sprintf('<%s>', self::LEVEL_COLOR_MAP[$record->level->value]),
'%level_name%' => sprintf($this->options['level_name_format'], $record->level->getName()),
'%end_tag%' => '</>',
'%channel%' => $record['channel'],
'%message%' => $this->replacePlaceHolder($record)['message'],
'%channel%' => $record->channel,
'%message%' => $this->replacePlaceHolder($record)->message,
'%context%' => $context,
'%extra%' => $extra,
]);
Expand Down Expand Up @@ -160,15 +155,15 @@ public function castObject(mixed $v, array $a, Stub $s, bool $isNested): array
return $a;
}

private function replacePlaceHolder(array $record): array
private function replacePlaceHolder(LogRecord $record): LogRecord
{
$message = $record['message'];
$message = $record->message;

if (!str_contains($message, '{')) {
return $record;
}

$context = $record['context'];
$context = $record->context;

$replacements = [];
foreach ($context as $k => $v) {
Expand All @@ -178,9 +173,7 @@ private function replacePlaceHolder(array $record): array
$replacements['{'.$k.'}'] = sprintf('<comment>%s</>', $v);
}

$record['message'] = strtr($message, $replacements);

return $record;
return $record->with(message: strtr($message, $replacements));
}

private function dumpData(mixed $data, bool $colors = null): string
Expand All @@ -195,7 +188,9 @@ private function dumpData(mixed $data, bool $colors = null): string
$this->dumper->setColors($colors);
}

if (!$data instanceof Data) {
if (($data['data'] ?? null) instanceof Data) {
$data = $data['data'];
} elseif (!$data instanceof Data) {
$data = $this->cloner->cloneVar($data);
}
$data = $data->withRefHandles(false);
Expand Down
8 changes: 2 additions & 6 deletions src/Symfony/Bridge/Monolog/Formatter/VarDumperFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,16 @@
*/
final class VarDumperFormatter implements FormatterInterface
{
use CompatibilityFormatter;

private VarCloner $cloner;

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

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

$record['context'] = $this->cloner->cloneVar($record['context']);
$record['extra'] = $this->cloner->cloneVar($record['extra']);
Expand Down
51 changes: 0 additions & 51 deletions src/Symfony/Bridge/Monolog/Handler/CompatibilityHandler.php

This file was deleted.

This file was deleted.

Loading