From ac611584a97a9d8dde5179a4243ad97488dd0979 Mon Sep 17 00:00:00 2001 From: Alec Sammon Date: Wed, 22 May 2024 07:23:07 +0100 Subject: [PATCH] Use configuration to determine log level --- .../Laravel/src/Watchers/LogWatcher.php | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Instrumentation/Laravel/src/Watchers/LogWatcher.php b/src/Instrumentation/Laravel/src/Watchers/LogWatcher.php index 0b568f68..fa54072d 100644 --- a/src/Instrumentation/Laravel/src/Watchers/LogWatcher.php +++ b/src/Instrumentation/Laravel/src/Watchers/LogWatcher.php @@ -6,13 +6,21 @@ use Illuminate\Contracts\Foundation\Application; use Illuminate\Log\Events\MessageLogged; -use Illuminate\Log\LogManager; use OpenTelemetry\API\Trace\Span; use OpenTelemetry\Context\Context; class LogWatcher extends Watcher { - private LogManager $logger; + private const DEBUG = 1; + private const INFO = 2; + private const NOTICE = 3; + private const WARNING = 4; + private const ERROR = 5; + private const CRITICAL = 6; + private const ALERT = 7; + private const EMERGENCY = 8; + + private int $minLogLevel; /** @psalm-suppress UndefinedInterfaceMethod */ public function register(Application $app): void @@ -20,8 +28,11 @@ public function register(Application $app): void /** @phan-suppress-next-line PhanTypeArraySuspicious */ $app['events']->listen(MessageLogged::class, [$this, 'recordLog']); - /** @phan-suppress-next-line PhanTypeArraySuspicious */ - $this->logger = $app['log']; + $this->minLogLevel = self::INFO; + $levelStr = strtoupper(env("OTEL_PHP_LOG_LEVEL", "INFO")); + if (defined(self::class.'::'.$levelStr)) { + $this->minLogLevel = constant(self::class . '::' . $levelStr); + } } /** @@ -29,10 +40,10 @@ public function register(Application $app): void */ public function recordLog(MessageLogged $log): void { - $underlyingLogger = $this->logger->getLogger(); - - /** @phan-suppress-next-line PhanUndeclaredMethod */ - if (method_exists($underlyingLogger, 'isHandling') && !$underlyingLogger->isHandling($log->level)) { + if ( + defined(self::class.'::'.strtoupper($log->level)) && + constant(self::class . '::' . strtoupper($log->level)) < $this->minLogLevel + ) { return; }