Skip to content

Commit 203a8b4

Browse files
committed
add config and detection for lower monolog bundle versions
1 parent 4815ebc commit 203a8b4

File tree

5 files changed

+62
-52
lines changed

5 files changed

+62
-52
lines changed

src/EventListener/BufferFlusher.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,29 @@ public static function getSubscribedEvents(): array
4444
// Running with lower priority will make the ConsoleListener run before and create a new scope
4545
// with the new command name when running a symfony Command within another Command.
4646
return [
47-
KernelEvents::TERMINATE => ['onKernelTerminate', 10],
48-
ConsoleEvents::COMMAND => ['onConsoleCommand', 150],
49-
ConsoleEvents::TERMINATE => ['onConsoleTerminate', 10],
50-
ConsoleEvents::ERROR => ['onConsoleError', 10],
47+
KernelEvents::TERMINATE => ['handleKernelTerminateEvent', 10],
48+
ConsoleEvents::COMMAND => ['handleConsoleCommandEvent', 150],
49+
ConsoleEvents::TERMINATE => ['handleConsoleTerminateEvent', 10],
50+
ConsoleEvents::ERROR => ['handleConsoleErrorEvent', 10],
5151
];
5252
}
5353

54-
public function onKernelTerminate(TerminateEvent $event): void
54+
public function handleKernelTerminateEvent(TerminateEvent $event): void
5555
{
5656
$this->flushBuffers();
5757
}
5858

59-
public function onConsoleTerminate(ConsoleTerminateEvent $event): void
59+
public function handleConsoleTerminateEvent(ConsoleTerminateEvent $event): void
6060
{
6161
$this->flushBuffers();
6262
}
6363

64-
public function onConsoleError(ConsoleErrorEvent $event): void
64+
public function handleConsoleErrorEvent(ConsoleErrorEvent $event): void
6565
{
6666
$this->flushBuffers();
6767
}
6868

69-
public function onConsoleCommand(ConsoleCommandEvent $event): void
69+
public function handleConsoleCommandEvent(ConsoleCommandEvent $event): void
7070
{
7171
$this->flushBuffers();
7272
}

tests/End2End/App/KernelForBufferTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@ class KernelForBufferTest extends Kernel
1414
{
1515
public function registerContainerConfiguration(LoaderInterface $loader): void
1616
{
17+
parent::registerContainerConfiguration($loader);
1718
$loader->load(__DIR__ . '/config_buffer_test.yml');
19+
20+
if (!$this->supportsHubId()) {
21+
$loader->load(__DIR__ . '/config_buffer_test_php72.yml');
22+
}
23+
}
24+
25+
/**
26+
* Monolog Bundle supports hub_id from version 3.5 onwards.
27+
*/
28+
private function supportsHubId(): bool
29+
{
30+
try {
31+
if (class_exists('Composer\InstalledVersions')) {
32+
$version = \Composer\InstalledVersions::getVersion('symfony/monolog-bundle');
33+
if ($version && version_compare($version, '3.5.0', '>=')) {
34+
return true;
35+
}
36+
}
37+
38+
return true;
39+
} catch (\Exception $e) {
40+
return false;
41+
}
1842
}
1943
}

tests/End2End/App/config_buffer_test.yml

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,39 @@
1-
sentry:
2-
dsn: http://[email protected]/sentry/1
3-
register_error_handler: false
4-
tracing:
5-
enabled: true
6-
options:
7-
capture_silenced_errors: false
8-
error_types: E_ALL & ~E_USER_DEPRECATED
9-
traces_sample_rate: 0
10-
ignore_exceptions:
11-
- 'Symfony\Component\HttpKernel\Exception\BadRequestHttpException'
12-
- 'Symfony\Component\ErrorHandler\Error\FatalError'
13-
- 'Symfony\Component\Debug\Exception\FatalErrorException'
14-
ignore_transactions: 'GET tracing_ignored_transaction'
15-
transport: 'Sentry\SentryBundle\Tests\End2End\StubTransport'
16-
17-
18-
framework:
19-
router: { resource: "%routing_config_dir%/routing.yml" }
20-
secret: secret
21-
test: ~
22-
annotations: false
23-
php_errors:
24-
log: true
25-
261
services:
27-
test.hub:
28-
alias: Sentry\State\HubInterface
29-
public: true
30-
31-
Sentry\SentryBundle\Tests\End2End\StubTransport: ~
32-
332
Sentry\Monolog\BreadcrumbHandler:
343
arguments:
354
- '@Sentry\State\HubInterface'
365
- !php/const Monolog\Logger::WARNING
376

38-
Sentry\SentryBundle\Tests\End2End\App\Controller\MainController:
39-
autowire: true
40-
tags:
41-
- controller.service_arguments
42-
437
Sentry\SentryBundle\Tests\End2End\App\Controller\BufferFlushController:
448
autowire: true
459
tags:
4610
- controller.service_arguments
4711

4812
Sentry\SentryBundle\Tests\End2End\App\Command\MainCommand:
49-
tags: [{ name: 'console.command', command: 'main-command' }]
13+
tags: [ { name: 'console.command', command: 'main-command' } ]
5014

5115
Sentry\SentryBundle\Tests\End2End\App\Command\BreadcrumbTestCommand:
5216
autowire: true
53-
tags: [{ name: 'console.command', command: 'sentry:breadcrumb:test'}]
17+
tags: [ { name: 'console.command', command: 'sentry:breadcrumb:test' } ]
5418

5519
Sentry\SentryBundle\Tests\End2End\App\Command\DummyTestCommand:
5620
autowire: true
57-
tags: [{ name: 'console.command', command: 'sentry:dummy:test'}]
21+
tags: [ { name: 'console.command', command: 'sentry:dummy:test' } ]
5822

5923
Sentry\SentryBundle\Tests\End2End\App\Command\SubcommandTestCommand:
6024
autowire: true
61-
tags: [{ name: 'console.command', command: 'sentry:subcommand:test'}]
25+
tags: [ { name: 'console.command', command: 'sentry:subcommand:test' } ]
6226

6327
Sentry\SentryBundle\Tests\End2End\App\Command\CrashingSubcommandTestCommand:
6428
autowire: true
65-
tags: [{ name: 'console.command', command: 'sentry:subcommand:crash'}]
29+
tags: [ { name: 'console.command', command: 'sentry:subcommand:crash' } ]
6630

6731

6832
monolog:
6933
handlers:
7034
main:
71-
type: stream
72-
path: "%kernel.logs_dir%/%kernel.environment%.log"
35+
type: stream
36+
path: "%kernel.logs_dir%/%kernel.environment%.log"
7337
level: debug
7438
sentry:
7539
type: sentry
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
services:
2+
app.sentry.handler:
3+
class: Sentry\Monolog\Handler
4+
arguments:
5+
- '@Sentry\State\HubInterface'
6+
- !php/const Monolog\Logger::WARNING
7+
8+
monolog:
9+
handlers:
10+
sentry:
11+
type: service
12+
id: app.sentry.handler
13+
level: warning
14+
sentry_breadcrumbs:
15+
type: service
16+
name: sentry_breadcrumbs
17+
id: Sentry\Monolog\BreadcrumbHandler
18+
sentry_buffer:
19+
type: buffer
20+
handler: sentry
21+
level: notice
22+
buffer_size: 50

tests/End2End/BreadcrumbTestCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function testBreadcrumbWithConsoleListener()
6868
$this->assertCount(2, $event->getBreadcrumbs());
6969
$this->assertEquals('breadcrumb 1 error', $event->getBreadcrumbs()[0]->getMessage());
7070
$this->assertCount(1, $event->getExceptions());
71-
$this->assertEquals('Error thrown while running command "sentry:breadcrumb:test". Message: "Breadcrumb error"', $event->getMessage());
71+
$this->assertStringStartsWith('Error thrown while running command', $event->getMessage());
7272
}
7373

7474
/**

0 commit comments

Comments
 (0)