Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"php-http/message": "^1.15",
"psr/container": "^1.1 || ^2.0",
"psr/http-message": "^1.1 || ^2",
"psr/log": "^1 || ^2 || ^3",
"symfony/console": "^6.4 || ^7 || ^8",
"symfony/var-dumper": "^6.3 || ^7 || ^8",
"yiisoft/injector": "^1.2"
Expand Down
72 changes: 51 additions & 21 deletions src/Client/TrapHandle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Buggregator\Trap\Client\TrapHandle\Counter;
use Buggregator\Trap\Client\TrapHandle\Dumper as VarDumper;
use Buggregator\Trap\Client\TrapHandle\StaticState;
use Buggregator\Trap\Log\TrapLogger;
use Psr\Log\LoggerInterface;
use Symfony\Component\VarDumper\Caster\TraceStub;

/**
Expand All @@ -20,6 +22,7 @@ final class TrapHandle
private string $timesCounterKey = '';
private int $depth = 0;
private readonly StaticState $staticState;
private static ?TrapLogger $logger = null;

private function __construct(
private array $values,
Expand All @@ -35,6 +38,33 @@ public static function fromArray(array $array): self
return new self($array);
}

/**
* Get a PSR-3 compatible logger instance for Trap client logging.
*
* Uses TRAP_MONOLOG_HOST and TRAP_MONOLOG_PORT env variables,
* falling back to 127.0.0.1:9913.
*/
public static function logger(): LoggerInterface
{
if (self::$logger === null) {
$host = self::getEnvValue('TRAP_MONOLOG_HOST', '127.0.0.1');
$port = self::getEnvValue('TRAP_MONOLOG_PORT', '9913');

$port = \is_numeric($port) ? (int) $port : 9913;

if ($port < 1 || $port > 65535) {
$port = 9913;
}

self::$logger = new TrapLogger(
host: $host,
port: $port,
);
Comment on lines +50 to +62
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TRAP_MONOLOG_PORT is cast to int without validation. If the env var is non-numeric (or empty), this becomes 0 and the logger will attempt to connect to an invalid port instead of falling back to the default. Consider validating the value (numeric and within 1..65535) and using 9913 when invalid.

Copilot uses AI. Check for mistakes.
}

return self::$logger;
}

/**
* Create a new instance with a single value.
*
Expand Down Expand Up @@ -214,6 +244,25 @@ public function __destruct()
$this->haveToSend() and $this->sendDump();
}

private static function getEnvValue(string $name, string $default): string
{
if (\array_key_exists($name, $_ENV)) {
return $_ENV[$name];
}

$value = \getenv($name, true);
if ($value !== false) {
return $value;
}

$value = \getenv($name);
if ($value !== false) {
return $value;
}

return $default;
}

private function sendDump(): void
{
$staticState = StaticState::getValue();
Expand All @@ -223,9 +272,9 @@ private function sendDump(): void
try {
// Set default values if not set
if (!isset($_SERVER['VAR_DUMPER_FORMAT'], $_SERVER['VAR_DUMPER_SERVER'])) {
$_SERVER['VAR_DUMPER_FORMAT'] = $this->getEnvValue('VAR_DUMPER_FORMAT', 'server');
$_SERVER['VAR_DUMPER_FORMAT'] = self::getEnvValue('VAR_DUMPER_FORMAT', 'server');
// todo use the config file in the future
$_SERVER['VAR_DUMPER_SERVER'] = $this->getEnvValue('VAR_DUMPER_SERVER', '127.0.0.1:9912');
$_SERVER['VAR_DUMPER_SERVER'] = self::getEnvValue('VAR_DUMPER_SERVER', '127.0.0.1:9912');
}

// Dump single value
Expand All @@ -248,25 +297,6 @@ private function sendDump(): void
}
}

private function getEnvValue(string $name, string $default): string
{
if (\array_key_exists($name, $_ENV)) {
return $_ENV[$name];
}

$value = \getenv($name, true);
if ($value !== false) {
return $value;
}

$value = \getenv($name);
if ($value !== false) {
return $value;
}

return $default;
}

private function haveToSend(): bool
{
if (!$this->haveToSend || $this->values === []) {
Expand Down
Loading
Loading