Skip to content

Commit 085cf24

Browse files
committed
Monolog 2 compatibility.
1 parent 09dee17 commit 085cf24

12 files changed

+190
-70
lines changed

UPGRADE-4.3.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,44 @@ HttpKernel
8888
* Renamed `PostResponseEvent` to `TerminateEvent`
8989
* Deprecated `TranslatorListener` in favor of `LocaleAwareListener`
9090

91+
MonologBridge
92+
-------------
93+
94+
* Handlers have been made compatible with the interfaces of Monolog 2. If you extend any handlers of MonologBridge,
95+
you need to add return types to certain methods:
96+
97+
Before:
98+
```php
99+
class MyConsoleHandler extends \Symfony\Bridge\Monolog\Handler\ConsoleHandler
100+
{
101+
public function isHandling(array $record)
102+
{
103+
// ...
104+
}
105+
106+
public function handle(array $record)
107+
{
108+
// ..
109+
}
110+
}
111+
```
112+
113+
After:
114+
```php
115+
class MyConsoleHandler extends \Symfony\Bridge\Monolog\Handler\ConsoleHandler
116+
{
117+
public function isHandling(array $record): bool
118+
{
119+
// ...
120+
}
121+
122+
public function handle(array $record): bool
123+
{
124+
// ..
125+
}
126+
}
127+
```
128+
91129
Messenger
92130
---------
93131

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
"doctrine/reflection": "~1.0",
103103
"doctrine/doctrine-bundle": "~1.4",
104104
"masterminds/html5": "^2.6",
105-
"monolog/monolog": "~1.11",
105+
"monolog/monolog": "~1.11 || ^2",
106106
"nyholm/psr7": "^1.0",
107107
"ocramius/proxy-manager": "~0.4|~1.0|~2.0",
108108
"predis/predis": "~1.1",

src/Symfony/Bridge/Monolog/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* added `ConsoleCommandProcessor`: monolog processor that adds command name and arguments
88
* added `RouteProcessor`: monolog processor that adds route name, controller::action and route params
9+
* added experimental support for Monolog 2.
910

1011
4.2.0
1112
-----

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function onKernelResponse(FilterResponseEvent $event)
5757
/**
5858
* {@inheritdoc}
5959
*/
60-
protected function sendHeader($header, $content)
60+
protected function sendHeader($header, $content): void
6161
{
6262
if (!$this->sendHeaders) {
6363
return;
@@ -73,7 +73,7 @@ protected function sendHeader($header, $content)
7373
/**
7474
* Override default behavior since we check it in onKernelResponse.
7575
*/
76-
protected function headersAccepted()
76+
protected function headersAccepted(): bool
7777
{
7878
return true;
7979
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bridge\Monolog\Handler;
1313

14+
use Monolog\Formatter\FormatterInterface;
1415
use Monolog\Formatter\LineFormatter;
1516
use Monolog\Handler\AbstractProcessingHandler;
1617
use Monolog\Logger;
@@ -74,15 +75,15 @@ public function __construct(OutputInterface $output = null, bool $bubble = true,
7475
/**
7576
* {@inheritdoc}
7677
*/
77-
public function isHandling(array $record)
78+
public function isHandling(array $record): bool
7879
{
7980
return $this->updateLevel() && parent::isHandling($record);
8081
}
8182

8283
/**
8384
* {@inheritdoc}
8485
*/
85-
public function handle(array $record)
86+
public function handle(array $record): bool
8687
{
8788
// we have to update the logging level each time because the verbosity of the
8889
// console output might have changed in the meantime (it is not immutable)
@@ -100,7 +101,7 @@ public function setOutput(OutputInterface $output)
100101
/**
101102
* Disables the output.
102103
*/
103-
public function close()
104+
public function close(): void
104105
{
105106
$this->output = null;
106107

@@ -143,7 +144,7 @@ public static function getSubscribedEvents()
143144
/**
144145
* {@inheritdoc}
145146
*/
146-
protected function write(array $record)
147+
protected function write(array $record): void
147148
{
148149
// at this point we've determined for sure that we want to output the record, so use the output's own verbosity
149150
$this->output->write((string) $record['formatted'], false, $this->output->getVerbosity());
@@ -152,7 +153,7 @@ protected function write(array $record)
152153
/**
153154
* {@inheritdoc}
154155
*/
155-
protected function getDefaultFormatter()
156+
protected function getDefaultFormatter(): FormatterInterface
156157
{
157158
if (!class_exists(CliDumper::class)) {
158159
return new LineFormatter();

src/Symfony/Bridge/Monolog/Handler/FingersCrossed/HttpCodeActivationStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function __construct(RequestStack $requestStack, array $exclusions, $acti
4545
$this->exclusions = $exclusions;
4646
}
4747

48-
public function isHandlerActivated(array $record)
48+
public function isHandlerActivated(array $record): bool
4949
{
5050
$isActivated = parent::isHandlerActivated($record);
5151

src/Symfony/Bridge/Monolog/Handler/FingersCrossed/NotFoundActivationStrategy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct(RequestStack $requestStack, array $excludedUrls, $ac
3434
$this->blacklist = '{('.implode('|', $excludedUrls).')}i';
3535
}
3636

37-
public function isHandlerActivated(array $record)
37+
public function isHandlerActivated(array $record): bool
3838
{
3939
$isActivated = parent::isHandlerActivated($record);
4040

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function onKernelResponse(FilterResponseEvent $event)
5959
/**
6060
* {@inheritdoc}
6161
*/
62-
protected function sendHeader($header, $content)
62+
protected function sendHeader($header, $content): void
6363
{
6464
if (!self::$sendHeaders) {
6565
return;
@@ -75,7 +75,7 @@ protected function sendHeader($header, $content)
7575
/**
7676
* Override default behavior since we check the user agent in onKernelResponse.
7777
*/
78-
protected function headersAccepted()
78+
protected function headersAccepted(): bool
7979
{
8080
return true;
8181
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(string $host, int $level = Logger::DEBUG, bool $bubb
3939
/**
4040
* {@inheritdoc}
4141
*/
42-
public function handle(array $record)
42+
public function handle(array $record): bool
4343
{
4444
if (!$this->isHandling($record)) {
4545
return false;

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

Lines changed: 135 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,82 +12,162 @@
1212
namespace Symfony\Bridge\Monolog\Handler;
1313

1414
use Monolog\Handler\SwiftMailerHandler as BaseSwiftMailerHandler;
15+
use Monolog\Logger;
1516
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
1617
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
1718

18-
/**
19-
* Extended SwiftMailerHandler that flushes mail queue if necessary.
20-
*
21-
* @author Philipp Kräutli <pkraeutli@astina.ch>
22-
*
23-
* @final since Symfony 4.3
24-
*/
25-
class SwiftMailerHandler extends BaseSwiftMailerHandler
26-
{
27-
protected $transport;
28-
29-
protected $instantFlush = false;
30-
31-
public function setTransport(\Swift_Transport $transport)
32-
{
33-
$this->transport = $transport;
34-
}
35-
19+
if (Logger::API < 2) {
3620
/**
37-
* After the kernel has been terminated we will always flush messages.
21+
* Extended SwiftMailerHandler that flushes mail queue if necessary.
22+
*
23+
* @author Philipp Kräutli <pkraeutli@astina.ch>
24+
*
25+
* @final since Symfony 4.3
3826
*/
39-
public function onKernelTerminate(PostResponseEvent $event)
27+
class SwiftMailerHandler extends BaseSwiftMailerHandler
4028
{
41-
$this->instantFlush = true;
42-
}
29+
protected $transport;
4330

44-
/**
45-
* After the CLI application has been terminated we will always flush messages.
46-
*/
47-
public function onCliTerminate(ConsoleTerminateEvent $event)
48-
{
49-
$this->instantFlush = true;
50-
}
31+
protected $instantFlush = false;
5132

52-
/**
53-
* {@inheritdoc}
54-
*/
55-
protected function send($content, array $records)
56-
{
57-
parent::send($content, $records);
33+
public function setTransport(\Swift_Transport $transport)
34+
{
35+
$this->transport = $transport;
36+
}
5837

59-
if ($this->instantFlush) {
38+
/**
39+
* After the kernel has been terminated we will always flush messages.
40+
*/
41+
public function onKernelTerminate(PostResponseEvent $event)
42+
{
43+
$this->instantFlush = true;
44+
}
45+
46+
/**
47+
* After the CLI application has been terminated we will always flush messages.
48+
*/
49+
public function onCliTerminate(ConsoleTerminateEvent $event)
50+
{
51+
$this->instantFlush = true;
52+
}
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
protected function send($content, array $records)
58+
{
59+
parent::send($content, $records);
60+
61+
if ($this->instantFlush) {
62+
$this->flushMemorySpool();
63+
}
64+
}
65+
66+
/**
67+
* {@inheritdoc}
68+
*/
69+
public function reset()
70+
{
6071
$this->flushMemorySpool();
6172
}
62-
}
6373

64-
/**
65-
* {@inheritdoc}
66-
*/
67-
public function reset()
68-
{
69-
$this->flushMemorySpool();
70-
}
74+
/**
75+
* Flushes the mail queue if a memory spool is used.
76+
*/
77+
private function flushMemorySpool()
78+
{
79+
$mailerTransport = $this->mailer->getTransport();
80+
if (!$mailerTransport instanceof \Swift_Transport_SpoolTransport) {
81+
return;
82+
}
7183

84+
$spool = $mailerTransport->getSpool();
85+
if (!$spool instanceof \Swift_MemorySpool) {
86+
return;
87+
}
88+
89+
if (null === $this->transport) {
90+
throw new \Exception('No transport available to flush mail queue');
91+
}
92+
93+
$spool->flushQueue($this->transport);
94+
}
95+
}
96+
} else {
7297
/**
73-
* Flushes the mail queue if a memory spool is used.
98+
* Extended SwiftMailerHandler that flushes mail queue if necessary.
99+
*
100+
* @author Philipp Kräutli <pkraeutli@astina.ch>
101+
*
102+
* @final since Symfony 4.3
74103
*/
75-
private function flushMemorySpool()
104+
class SwiftMailerHandler extends BaseSwiftMailerHandler
76105
{
77-
$mailerTransport = $this->mailer->getTransport();
78-
if (!$mailerTransport instanceof \Swift_Transport_SpoolTransport) {
79-
return;
106+
protected $transport;
107+
108+
protected $instantFlush = false;
109+
110+
public function setTransport(\Swift_Transport $transport)
111+
{
112+
$this->transport = $transport;
113+
}
114+
115+
/**
116+
* After the kernel has been terminated we will always flush messages.
117+
*/
118+
public function onKernelTerminate(PostResponseEvent $event)
119+
{
120+
$this->instantFlush = true;
80121
}
81122

82-
$spool = $mailerTransport->getSpool();
83-
if (!$spool instanceof \Swift_MemorySpool) {
84-
return;
123+
/**
124+
* After the CLI application has been terminated we will always flush messages.
125+
*/
126+
public function onCliTerminate(ConsoleTerminateEvent $event)
127+
{
128+
$this->instantFlush = true;
85129
}
86130

87-
if (null === $this->transport) {
88-
throw new \Exception('No transport available to flush mail queue');
131+
/**
132+
* {@inheritdoc}
133+
*/
134+
protected function send(string $content, array $records): void
135+
{
136+
parent::send($content, $records);
137+
138+
if ($this->instantFlush) {
139+
$this->flushMemorySpool();
140+
}
89141
}
90142

91-
$spool->flushQueue($this->transport);
143+
/**
144+
* {@inheritdoc}
145+
*/
146+
public function reset()
147+
{
148+
$this->flushMemorySpool();
149+
}
150+
151+
/**
152+
* Flushes the mail queue if a memory spool is used.
153+
*/
154+
private function flushMemorySpool()
155+
{
156+
$mailerTransport = $this->mailer->getTransport();
157+
if (!$mailerTransport instanceof \Swift_Transport_SpoolTransport) {
158+
return;
159+
}
160+
161+
$spool = $mailerTransport->getSpool();
162+
if (!$spool instanceof \Swift_MemorySpool) {
163+
return;
164+
}
165+
166+
if (null === $this->transport) {
167+
throw new \Exception('No transport available to flush mail queue');
168+
}
169+
170+
$spool->flushQueue($this->transport);
171+
}
92172
}
93173
}

0 commit comments

Comments
 (0)