Skip to content

Commit 0a41216

Browse files
Laravel: catch any TypeError thrown within LogWatcher when attempting to filter log levels. (open-telemetry#311)
1 parent 73e81d8 commit 0a41216

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/Instrumentation/Laravel/src/Watchers/LogWatcher.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;
1111
use OpenTelemetry\API\Logs\LogRecord;
1212
use OpenTelemetry\API\Logs\Map\Psr3;
13+
use TypeError;
1314

1415
class LogWatcher extends Watcher
1516
{
@@ -36,9 +37,17 @@ public function recordLog(MessageLogged $log): void
3637
{
3738
$underlyingLogger = $this->logger->getLogger();
3839

39-
/** @phan-suppress-next-line PhanUndeclaredMethod */
40-
if (method_exists($underlyingLogger, 'isHandling') && !$underlyingLogger->isHandling($log->level)) {
41-
return;
40+
/**
41+
* This assumes that the underlying logger (expected to be monolog) would accept `$log->level` as a string.
42+
* With monolog < 3.x, this method would fail. Let's prevent this blowing up in Laravel<10.x.
43+
*/
44+
try {
45+
/** @phan-suppress-next-line PhanUndeclaredMethod */
46+
if (method_exists($underlyingLogger, 'isHandling') && !$underlyingLogger->isHandling($log->level)) {
47+
return;
48+
}
49+
} catch (TypeError) {
50+
// Should this fail, we should continue to emit the LogRecord.
4251
}
4352

4453
$attributes = [

0 commit comments

Comments
 (0)