Skip to content

Commit c7a5d84

Browse files
Laravel: catch any TypeError thrown within LogWatcher when attempting to filter log levels.
1 parent 2d2c37b commit c7a5d84

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

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

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

4454
$attributes = [

0 commit comments

Comments
 (0)