Skip to content

Commit

Permalink
Laravel: catch any TypeError thrown within LogWatcher when attempting…
Browse files Browse the repository at this point in the history
… to filter log levels. (#311)
  • Loading branch information
ChrisLightfootWild authored Oct 27, 2024
1 parent 73e81d8 commit 0a41216
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/Instrumentation/Laravel/src/Watchers/LogWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use OpenTelemetry\API\Instrumentation\CachedInstrumentation;
use OpenTelemetry\API\Logs\LogRecord;
use OpenTelemetry\API\Logs\Map\Psr3;
use TypeError;

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

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

$attributes = [
Expand Down

0 comments on commit 0a41216

Please sign in to comment.