Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 13350dc

Browse files
feature #46153 [MonologBridge] Add support for Monolog 3 (Seldaek)
This PR was merged into the 6.1 branch. Discussion ---------- [MonologBridge] Add support for Monolog 3 | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Adds support for the upcoming version of Monolog. I mostly wanted to do this before releasing it to ensure it is possible to integrate with it and Monolog 2 using the same codebase with only minimal amounts of version-based forks. I think the result is pretty good, so I will try and keep pushing for a beta release soon. I still need to add a SymfonyMailerHandler to replace the SwiftMailerHandler which got axed though. To try it with Monolog 3 one needs to switch the requirement to `^3@dev` for now, but I did that locally and all tests pass with v3. Commits ------- 720444c [MonologBridge] Add support for Monolog 3
2 parents c70be09 + 720444c commit 13350dc

34 files changed

+519
-214
lines changed

src/Symfony/Bridge/Monolog/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Add support for Monolog 3
8+
49
6.0
510
---
611

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Monolog\Formatter;
13+
14+
use Monolog\Logger;
15+
use Monolog\LogRecord;
16+
17+
if (Logger::API >= 3) {
18+
/**
19+
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
20+
*
21+
* @author Jordi Boggiano <[email protected]>
22+
*
23+
* @internal
24+
*/
25+
trait CompatibilityFormatter
26+
{
27+
abstract private function doFormat(array|LogRecord $record): mixed;
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function format(LogRecord $record): mixed
33+
{
34+
return $this->doFormat($record);
35+
}
36+
}
37+
} else {
38+
/**
39+
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
40+
*
41+
* @author Jordi Boggiano <[email protected]>
42+
*
43+
* @internal
44+
*/
45+
trait CompatibilityFormatter
46+
{
47+
abstract private function doFormat(array|LogRecord $record): mixed;
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function format(array $record): mixed
53+
{
54+
return $this->doFormat($record);
55+
}
56+
}
57+
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Monolog\Formatter\FormatterInterface;
1515
use Monolog\Logger;
16+
use Monolog\LogRecord;
1617
use Symfony\Component\Console\Formatter\OutputFormatter;
1718
use Symfony\Component\VarDumper\Cloner\Data;
1819
use Symfony\Component\VarDumper\Cloner\Stub;
@@ -24,9 +25,13 @@
2425
*
2526
* @author Tobias Schultze <http://tobion.de>
2627
* @author Grégoire Pineau <[email protected]>
28+
*
29+
* @final since Symfony 6.1
2730
*/
2831
class ConsoleFormatter implements FormatterInterface
2932
{
33+
use CompatibilityFormatter;
34+
3035
public const SIMPLE_FORMAT = "%datetime% %start_tag%%level_name%%end_tag% <comment>[%channel%]</> %message%%context%%extra%\n";
3136
public const SIMPLE_DATE = 'H:i:s';
3237

@@ -98,11 +103,11 @@ public function formatBatch(array $records): mixed
98103
return $records;
99104
}
100105

101-
/**
102-
* {@inheritdoc}
103-
*/
104-
public function format(array $record): mixed
106+
private function doFormat(array|LogRecord $record): mixed
105107
{
108+
if ($record instanceof LogRecord) {
109+
$record = $record->toArray();
110+
}
106111
$record = $this->replacePlaceHolder($record);
107112

108113
if (!$this->options['ignore_empty_context_and_extra'] || !empty($record['context'])) {

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,31 @@
1212
namespace Symfony\Bridge\Monolog\Formatter;
1313

1414
use Monolog\Formatter\FormatterInterface;
15+
use Monolog\LogRecord;
1516
use Symfony\Component\VarDumper\Cloner\VarCloner;
1617

1718
/**
1819
* @author Grégoire Pineau <[email protected]>
20+
*
21+
* @final since Symfony 6.1
1922
*/
2023
class VarDumperFormatter implements FormatterInterface
2124
{
25+
use CompatibilityFormatter;
26+
2227
private VarCloner $cloner;
2328

2429
public function __construct(VarCloner $cloner = null)
2530
{
2631
$this->cloner = $cloner ?? new VarCloner();
2732
}
2833

29-
/**
30-
* {@inheritdoc}
31-
*/
32-
public function format(array $record): mixed
34+
private function doFormat(array|LogRecord $record): mixed
3335
{
36+
if ($record instanceof LogRecord) {
37+
$record = $record->toArray();
38+
}
39+
3440
$record['context'] = $this->cloner->cloneVar($record['context']);
3541
$record['extra'] = $this->cloner->cloneVar($record['extra']);
3642

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Monolog\Handler;
13+
14+
use Monolog\Logger;
15+
use Monolog\LogRecord;
16+
17+
if (Logger::API >= 3) {
18+
/**
19+
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
20+
*
21+
* @author Jordi Boggiano <[email protected]>
22+
*
23+
* @internal
24+
*/
25+
trait CompatibilityHandler
26+
{
27+
abstract private function doHandle(array|LogRecord $record): bool;
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function handle(LogRecord $record): bool
33+
{
34+
return $this->doHandle($record);
35+
}
36+
}
37+
} else {
38+
/**
39+
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
40+
*
41+
* @author Jordi Boggiano <[email protected]>
42+
*
43+
* @internal
44+
*/
45+
trait CompatibilityHandler
46+
{
47+
abstract private function doHandle(array|LogRecord $record): bool;
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function handle(array $record): bool
53+
{
54+
return $this->doHandle($record);
55+
}
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Monolog\Handler;
13+
14+
use Monolog\Logger;
15+
use Monolog\LogRecord;
16+
17+
if (Logger::API >= 3) {
18+
/**
19+
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
20+
*
21+
* @author Jordi Boggiano <[email protected]>
22+
*
23+
* @internal
24+
*/
25+
trait CompatibilityProcessingHandler
26+
{
27+
abstract private function doWrite(array|LogRecord $record): void;
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
protected function write(LogRecord $record): void
33+
{
34+
$this->doWrite($record);
35+
}
36+
}
37+
} else {
38+
/**
39+
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
40+
*
41+
* @author Jordi Boggiano <[email protected]>
42+
*
43+
* @internal
44+
*/
45+
trait CompatibilityProcessingHandler
46+
{
47+
abstract private function doWrite(array|LogRecord $record): void;
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
protected function write(array $record): void
53+
{
54+
$this->doWrite($record);
55+
}
56+
}
57+
}

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

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Monolog\Formatter\LineFormatter;
1616
use Monolog\Handler\AbstractProcessingHandler;
1717
use Monolog\Logger;
18+
use Monolog\LogRecord;
1819
use Symfony\Bridge\Monolog\Formatter\ConsoleFormatter;
1920
use Symfony\Component\Console\ConsoleEvents;
2021
use Symfony\Component\Console\Event\ConsoleCommandEvent;
@@ -24,6 +25,48 @@
2425
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2526
use Symfony\Component\VarDumper\Dumper\CliDumper;
2627

28+
if (Logger::API >= 3) {
29+
/**
30+
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
31+
*
32+
* @author Jordi Boggiano <[email protected]>
33+
*
34+
* @internal
35+
*/
36+
trait CompatibilityIsHandlingHandler
37+
{
38+
abstract private function doIsHandling(array|LogRecord $record): bool;
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function isHandling(LogRecord $record): bool
44+
{
45+
return $this->doIsHandling($record);
46+
}
47+
}
48+
} else {
49+
/**
50+
* The base class for compatibility between Monolog 3 LogRecord and Monolog 1/2 array records.
51+
*
52+
* @author Jordi Boggiano <[email protected]>
53+
*
54+
* @internal
55+
*/
56+
trait CompatibilityIsHandlingHandler
57+
{
58+
abstract private function doIsHandling(array|LogRecord $record): bool;
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function isHandling(array $record): bool
64+
{
65+
return $this->doIsHandling($record);
66+
}
67+
}
68+
}
69+
2770
/**
2871
* Writes logs to the console output depending on its verbosity setting.
2972
*
@@ -40,9 +83,15 @@
4083
* This mapping can be customized with the $verbosityLevelMap constructor parameter.
4184
*
4285
* @author Tobias Schultze <http://tobion.de>
86+
*
87+
* @final since Symfony 6.1
4388
*/
4489
class ConsoleHandler extends AbstractProcessingHandler implements EventSubscriberInterface
4590
{
91+
use CompatibilityHandler;
92+
use CompatibilityIsHandlingHandler;
93+
use CompatibilityProcessingHandler;
94+
4695
private ?OutputInterface $output;
4796
private array $verbosityLevelMap = [
4897
OutputInterface::VERBOSITY_QUIET => Logger::ERROR,
@@ -75,15 +124,15 @@ public function __construct(OutputInterface $output = null, bool $bubble = true,
75124
/**
76125
* {@inheritdoc}
77126
*/
78-
public function isHandling(array $record): bool
127+
private function doIsHandling(array|LogRecord $record): bool
79128
{
80129
return $this->updateLevel() && parent::isHandling($record);
81130
}
82131

83132
/**
84133
* {@inheritdoc}
85134
*/
86-
public function handle(array $record): bool
135+
private function doHandle(array|LogRecord $record): bool
87136
{
88137
// we have to update the logging level each time because the verbosity of the
89138
// console output might have changed in the meantime (it is not immutable)
@@ -141,10 +190,7 @@ public static function getSubscribedEvents(): array
141190
];
142191
}
143192

144-
/**
145-
* {@inheritdoc}
146-
*/
147-
protected function write(array $record): void
193+
private function doWrite(array|LogRecord $record): void
148194
{
149195
// at this point we've determined for sure that we want to output the record, so use the output's own verbosity
150196
$this->output->write((string) $record['formatted'], false, $this->output->getVerbosity());

0 commit comments

Comments
 (0)