Skip to content

Commit 5feefa9

Browse files
committed
--
1 parent 3bed909 commit 5feefa9

File tree

4 files changed

+43
-26
lines changed

4 files changed

+43
-26
lines changed

src/Symfony/Bridge/Monolog/Command/ServerLogCommand.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9595
'date_format' => $input->getOption('date-format'),
9696
'colors' => $output->isDecorated(),
9797
'multiline' => OutputInterface::VERBOSITY_DEBUG <= $output->getVerbosity(),
98+
'from_server_log' => true,
9899
]));
99100

100101
if (!str_contains($host = $input->getOption('host'), '://')) {
@@ -146,14 +147,26 @@ private function getLogs($socket): iterable
146147
}
147148
}
148149

149-
private function displayLog(OutputInterface $output, int $clientId, LogRecord $record): void
150+
private function displayLog(OutputInterface $output, int $clientId, array $record): void
150151
{
151152
if (isset($record['log_id'])) {
152153
$clientId = unpack('H*', $record['log_id'])[1];
153154
}
154155
$logBlock = sprintf('<bg=%s> </>', self::BG_COLOR[$clientId % 8]);
155156
$output->write($logBlock);
156157

158+
$record = new LogRecord(
159+
$record['datetime'],
160+
$record['channel'],
161+
Level::fromValue($record['level']),
162+
$record['message'],
163+
// We wrap context and extra, because they have been already dumped.
164+
// So they are instance of Symfony\Component\VarDumper\Cloner\Data
165+
// But LogRecord expects array
166+
[$record['context']],
167+
[$record['extra']],
168+
);
169+
157170
$this->handler->handle($record);
158171
}
159172
}

src/Symfony/Bridge/Monolog/Formatter/ConsoleFormatter.php

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Monolog\Level;
1616
use Monolog\LogRecord;
1717
use Symfony\Component\Console\Formatter\OutputFormatter;
18+
use Symfony\Component\DependencyInjection\Tests\Compiler\I;
1819
use Symfony\Component\VarDumper\Cloner\Data;
1920
use Symfony\Component\VarDumper\Cloner\Stub;
2021
use Symfony\Component\VarDumper\Cloner\VarCloner;
@@ -68,6 +69,11 @@ public function __construct(array $options = [])
6869
'multiline' => false,
6970
'level_name_format' => '%-9s',
7071
'ignore_empty_context_and_extra' => true,
72+
/**
73+
* @internal option
74+
* @see Symfony\Bridge\Monolog\Command\ServerLogCommand::displayLog
75+
*/
76+
'from_server_log' => false,
7177
], $options);
7278

7379
if (class_exists(VarCloner::class)) {
@@ -98,30 +104,35 @@ public function formatBatch(array $records): mixed
98104

99105
public function format(LogRecord $record): mixed
100106
{
101-
$record = $record->toArray();
102107
$record = $this->replacePlaceHolder($record);
103108

104-
if (!$this->options['ignore_empty_context_and_extra'] || !empty($record['context'])) {
105-
$context = ($this->options['multiline'] ? "\n" : ' ').$this->dumpData($record['context']);
109+
if (!$this->options['ignore_empty_context_and_extra'] || !empty($record->context)) {
110+
$context = $record->context;
111+
if ($this->options['from_server_log']) {
112+
$context = $context[0];
113+
}
114+
$context = ($this->options['multiline'] ? "\n" : ' ').$this->dumpData($context);
106115
} else {
107116
$context = '';
108117
}
109118

110-
if (!$this->options['ignore_empty_context_and_extra'] || !empty($record['extra'])) {
111-
$extra = ($this->options['multiline'] ? "\n" : ' ').$this->dumpData($record['extra']);
119+
if (!$this->options['ignore_empty_context_and_extra'] || !empty($record->extra)) {
120+
$extra = $record->extra;
121+
if ($this->options['from_server_log']) {
122+
$extra = $extra[0];
123+
}
124+
$extra = ($this->options['multiline'] ? "\n" : ' ').$this->dumpData($extra);
112125
} else {
113126
$extra = '';
114127
}
115128

116129
$formatted = strtr($this->options['format'], [
117-
'%datetime%' => $record['datetime'] instanceof \DateTimeInterface
118-
? $record['datetime']->format($this->options['date_format'])
119-
: $record['datetime'],
120-
'%start_tag%' => sprintf('<%s>', self::LEVEL_COLOR_MAP[$record['level']]),
121-
'%level_name%' => sprintf($this->options['level_name_format'], $record['level_name']),
130+
'%datetime%' => $record->datetime->format($this->options['date_format']),
131+
'%start_tag%' => sprintf('<%s>', self::LEVEL_COLOR_MAP[$record->level->value]),
132+
'%level_name%' => sprintf($this->options['level_name_format'], $record->level->getName()),
122133
'%end_tag%' => '</>',
123-
'%channel%' => $record['channel'],
124-
'%message%' => $this->replacePlaceHolder($record)['message'],
134+
'%channel%' => $record->channel,
135+
'%message%' => $this->replacePlaceHolder($record)->message,
125136
'%context%' => $context,
126137
'%extra%' => $extra,
127138
]);
@@ -156,15 +167,15 @@ public function castObject(mixed $v, array $a, Stub $s, bool $isNested): array
156167
return $a;
157168
}
158169

159-
private function replacePlaceHolder(array $record): array
170+
private function replacePlaceHolder(LogRecord $record): LogRecord
160171
{
161-
$message = $record['message'];
172+
$message = $record->message;
162173

163174
if (!str_contains($message, '{')) {
164175
return $record;
165176
}
166177

167-
$context = $record['context'];
178+
$context = $record->context;
168179

169180
$replacements = [];
170181
foreach ($context as $k => $v) {
@@ -174,9 +185,7 @@ private function replacePlaceHolder(array $record): array
174185
$replacements['{'.$k.'}'] = sprintf('<comment>%s</>', $v);
175186
}
176187

177-
$record['message'] = strtr($message, $replacements);
178-
179-
return $record;
188+
return $record->with(message: strtr($message, $replacements));
180189
}
181190

182191
private function dumpData(mixed $data, bool $colors = null): string

src/Symfony/Bridge/Monolog/Handler/MailerHandler.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public function handleBatch(array $records): void
4040
{
4141
$messages = [];
4242

43-
/** @var LogRecord $record */
4443
foreach ($records as $record) {
4544
if ($record->level->isLowerThan($this->level)) {
4645
continue;

src/Symfony/Bridge/Monolog/Processor/DebugProcessor.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,8 @@ public function __invoke(LogRecord $record): LogRecord
4747
$this->errorCount[$key] = 0;
4848
}
4949

50-
switch ($record->level) {
51-
case Level::Error:
52-
case Level::Critical:
53-
case Level::Alert:
54-
case Level::Emergency:
55-
++$this->errorCount[$key];
50+
if ($record->level->isHigherThan(Level::Warning)) {
51+
++$this->errorCount[$key];
5652
}
5753

5854
return $record;

0 commit comments

Comments
 (0)