Skip to content

Commit 31ca51b

Browse files
committed
change to inheritance
1 parent bb3ec9b commit 31ca51b

File tree

2 files changed

+60
-61
lines changed

2 files changed

+60
-61
lines changed

src/Monolog/LogsHandler.php

Lines changed: 7 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,24 @@
44

55
namespace Sentry\SentryBundle\Monolog;
66

7-
use Monolog\Formatter\FormatterInterface;
8-
use Monolog\Handler\HandlerInterface;
97
use Monolog\Logger as MonologLogger;
108
use Monolog\LogRecord;
119
use Sentry\Monolog\CompatibilityLogLevelTrait;
1210
use Sentry\Monolog\LogsHandler as BaseLogsHandler;
1311

1412
/**
15-
* Wrapper for the Sentry LogsHandler that uses Monolog constants for initialization.
16-
* Sentry's LogLevel classes cannot be properly used in Symfony configuration so this acts as
17-
* a service facade that can be instantiated using yml/xml config files.
13+
* Extends the base LogsHandler so that it can be used with Monolog constants.
14+
* Sentry LogLevel objects are not easily usable with symfony config files, so this provides
15+
* a convenient way to instantiate the service in yaml/xml.
1816
*/
19-
class LogsHandler implements HandlerInterface
17+
class LogsHandler extends BaseLogsHandler
2018
{
2119
use CompatibilityLogLevelTrait;
2220

23-
/**
24-
* @var BaseLogsHandler
25-
*/
26-
private $logsHandler;
27-
2821
public function __construct(int $level = MonologLogger::DEBUG, bool $bubble = true)
2922
{
3023
$logLevel = self::getSentryLogLevelFromMonologLevel($level);
31-
$this->logsHandler = new BaseLogsHandler($logLevel, $bubble);
32-
}
33-
34-
/**
35-
* @param array<string, mixed>|LogRecord $record
36-
*/
37-
public function isHandling($record): bool
38-
{
39-
return $this->logsHandler->isHandling($record);
24+
parent::__construct($logLevel, $bubble);
4025
}
4126

4227
/**
@@ -46,49 +31,10 @@ public function handle($record): bool
4631
{
4732
// Extra check required here because `isHandling` is not guaranteed to
4833
// be called, and we might accidentally capture log messages that should be filtered.
49-
if ($this->logsHandler->isHandling($record)) {
50-
return $this->logsHandler->handle($record);
34+
if ($this->isHandling($record)) {
35+
return parent::handle($record);
5136
}
5237

5338
return false;
5439
}
55-
56-
/**
57-
* @param array<array<string, mixed>|LogRecord> $records
58-
*/
59-
public function handleBatch(array $records): void
60-
{
61-
$this->logsHandler->handleBatch($records);
62-
}
63-
64-
public function close(): void
65-
{
66-
$this->logsHandler->close();
67-
}
68-
69-
/**
70-
* @param callable $callback
71-
*/
72-
public function pushProcessor($callback): void
73-
{
74-
$this->logsHandler->pushProcessor($callback);
75-
}
76-
77-
/**
78-
* @return callable
79-
*/
80-
public function popProcessor()
81-
{
82-
return $this->logsHandler->popProcessor();
83-
}
84-
85-
public function setFormatter(FormatterInterface $formatter): void
86-
{
87-
$this->logsHandler->setFormatter($formatter);
88-
}
89-
90-
public function getFormatter(): FormatterInterface
91-
{
92-
return $this->logsHandler->getFormatter();
93-
}
9440
}

tests/Monolog/LogsHandlerTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\Monolog;
6+
7+
use Monolog\Logger as MonologLogger;
8+
use PHPUnit\Framework\TestCase;
9+
use Sentry\SentryBundle\Monolog\LogsHandler;
10+
11+
final class LogsHandlerTest extends TestCase
12+
{
13+
public function testHandleReturnsFalseBelowThresholdEvenWhenBubbleFalse(): void
14+
{
15+
$handler = new LogsHandler(MonologLogger::WARNING, false);
16+
$record = [
17+
'level' => MonologLogger::DEBUG,
18+
'message' => 'msg',
19+
'context' => [],
20+
'extra' => [],
21+
];
22+
23+
$this->assertFalse($handler->handle($record));
24+
}
25+
26+
public function testHandleReturnsFalseAboveThresholdWhenBubbleTrue(): void
27+
{
28+
$handler = new LogsHandler(MonologLogger::DEBUG, true);
29+
$record = [
30+
'level' => MonologLogger::WARNING,
31+
'message' => 'msg',
32+
'context' => [],
33+
'extra' => [],
34+
];
35+
36+
$this->assertFalse($handler->handle($record));
37+
}
38+
39+
public function testHandleReturnsTrueAboveThresholdWhenBubbleFalse(): void
40+
{
41+
$handler = new LogsHandler(MonologLogger::DEBUG, false);
42+
$record = [
43+
'level' => MonologLogger::WARNING,
44+
'message' => 'msg',
45+
'context' => [],
46+
'extra' => [],
47+
];
48+
49+
$this->assertTrue($handler->handle($record));
50+
}
51+
}
52+
53+

0 commit comments

Comments
 (0)