Skip to content

Commit 3bd62b7

Browse files
authored
feat(sentry): upgrade to v4.18.0 and refactor LogsHandler (#988)
- Upgrade sentry/sentry from ^4.16.0 to ^4.18.0 in root and sentry package - Refactor LogsHandler to use parent class's handle() method by overriding compileAttributes() - Add #[Override] attribute for better type safety - Simplify code by removing duplicate logic and leveraging parent implementation - Improve code maintainability by following DRY principles Co-authored-by: Deeka Wong <[email protected]>
1 parent 4177cd2 commit 3bd62b7

File tree

2 files changed

+13
-30
lines changed

2 files changed

+13
-30
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"hyperf/http-server": "~3.1.0",
3434
"hyperf/support": "~3.1.0",
3535
"hyperf/tappable": "~3.1.0",
36-
"sentry/sentry": "^4.16.0",
36+
"sentry/sentry": "^4.18.0",
3737
"symfony/polyfill-php85": "^1.33"
3838
},
3939
"suggest": {

src/Monolog/LogsHandler.php

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313

1414
use Hyperf\Collection\Arr;
1515
use Monolog\LogRecord;
16+
use Override;
1617
use Sentry\Logs\LogLevel;
17-
use Sentry\Logs\Logs;
1818
use Sentry\Monolog\CompatibilityLogLevelTrait;
19-
use Throwable;
2019

2120
class LogsHandler extends \Sentry\Monolog\LogsHandler
2221
{
@@ -35,35 +34,19 @@ public function __construct(
3534
}
3635

3736
/**
38-
* @param array<string, mixed>|LogRecord $record
37+
* @param array<string,mixed>|LogRecord $record
38+
*
39+
* @return array<string,mixed>
3940
*/
40-
public function handle($record): bool
41+
#[Override]
42+
protected function compileAttributes($record): array
4143
{
42-
if (! $this->isHandling($record)) {
43-
return false;
44-
}
45-
46-
// Do not collect logs for exceptions, they should be handled separately by the `Handler` or `captureException`
47-
if (
48-
isset($record['context']['exception'])
49-
&& $record['context']['exception'] instanceof Throwable
50-
) {
51-
return false;
52-
}
53-
54-
Logs::getInstance()->aggregator()->add(
55-
self::getSentryLogLevelFromMonologLevel($record['level']),
56-
$record['message'],
57-
[],
58-
array_merge(
59-
Arr::dot($record['context'] ?? [], 'context.'),
60-
Arr::dot($record['extra'] ?? [], 'extra.'),
61-
['logger.channel' => $record['channel'] ?? ''],
62-
['logger.group' => $this->group],
63-
['sentry.origin' => 'auto.log.monolog']
64-
)
44+
return array_merge(
45+
Arr::dot($record['context'] ?? [], 'context.'),
46+
Arr::dot($record['extra'] ?? [], 'extra.'),
47+
['logger.channel' => $record['channel'] ?? ''],
48+
['logger.group' => $this->group],
49+
['sentry.origin' => 'auto.log.monolog']
6550
);
66-
67-
return $this->bubble === false;
6851
}
6952
}

0 commit comments

Comments
 (0)