Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Use configuration to determine log level #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/Instrumentation/Laravel/src/Watchers/LogWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,44 @@

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
{
/** @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);
}
}

/**
* Record a log.
*/
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;
}

Expand Down