Skip to content

Commit 0425600

Browse files
authored
fix(logs): check if record should be handled within handle method (#1888)
1 parent a00446d commit 0425600

2 files changed

Lines changed: 110 additions & 3 deletions

File tree

src/Monolog/LogsHandler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public function isHandling($record): bool
5454
*/
5555
public function handle($record): bool
5656
{
57+
if (!$this->isHandling($record)) {
58+
return false;
59+
}
5760
// Do not collect logs for exceptions, they should be handled seperately by the `Handler` or `captureException`
5861
if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) {
5962
return false;

tests/Monolog/LogsHandlerTest.php

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
final class LogsHandlerTest extends TestCase
1818
{
19+
protected function setUp(): void
20+
{
21+
Logs::getInstance()->flush();
22+
}
23+
1924
/**
2025
* @dataProvider handleDataProvider
2126
*/
@@ -36,9 +41,6 @@ public function testHandle($record, Log $expectedLog): void
3641

3742
$logs = Logs::getInstance()->aggregator()->all();
3843

39-
// Clear the logs aggregator to avoid side effects in other tests
40-
Logs::getInstance()->aggregator()->flush();
41-
4244
$this->assertCount(1, $logs);
4345

4446
$log = $logs[0];
@@ -58,6 +60,28 @@ static function (string $key) {
5860
);
5961
}
6062

63+
/**
64+
* @dataProvider logLevelDataProvider
65+
*/
66+
public function testLogLevels($record, int $countLogs): void
67+
{
68+
$client = ClientBuilder::create([
69+
'enable_logs' => true,
70+
'before_send' => static function () {
71+
return null;
72+
},
73+
])->getClient();
74+
75+
$hub = new Hub($client);
76+
SentrySdk::setCurrentHub($hub);
77+
78+
$handler = new LogsHandler(LogLevel::warn());
79+
$handler->handle($record);
80+
81+
$logs = Logs::getInstance()->aggregator()->all();
82+
$this->assertCount($countLogs, $logs);
83+
}
84+
6185
public static function handleDataProvider(): iterable
6286
{
6387
yield [
@@ -197,4 +221,84 @@ public static function handleDataProvider(): iterable
197221
->setAttribute('bar', 'baz'),
198222
];
199223
}
224+
225+
public static function logLevelDataProvider(): iterable
226+
{
227+
yield [
228+
RecordFactory::create(
229+
'foo bar',
230+
Logger::DEBUG,
231+
'channel.foo',
232+
[],
233+
[]
234+
),
235+
0,
236+
];
237+
238+
yield [
239+
RecordFactory::create(
240+
'foo bar',
241+
Logger::NOTICE,
242+
'channel.foo',
243+
[],
244+
[]
245+
),
246+
0,
247+
];
248+
249+
yield [
250+
RecordFactory::create(
251+
'foo bar',
252+
Logger::INFO,
253+
'channel.foo',
254+
[],
255+
[]
256+
),
257+
0,
258+
];
259+
260+
yield [
261+
RecordFactory::create(
262+
'foo bar',
263+
Logger::WARNING,
264+
'channel.foo',
265+
[],
266+
[]
267+
),
268+
1,
269+
];
270+
271+
yield [
272+
RecordFactory::create(
273+
'foo bar',
274+
Logger::CRITICAL,
275+
'channel.foo',
276+
[],
277+
[]
278+
),
279+
1,
280+
];
281+
282+
yield [
283+
RecordFactory::create(
284+
'foo bar',
285+
Logger::ALERT,
286+
'channel.foo',
287+
[],
288+
[]
289+
),
290+
1,
291+
];
292+
293+
yield [
294+
RecordFactory::create(
295+
'foo bar',
296+
Logger::EMERGENCY,
297+
'channel.foo',
298+
[],
299+
[]
300+
),
301+
1,
302+
];
303+
}
200304
}

0 commit comments

Comments
 (0)