Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Tracy 2.9.6 compatibility #3

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"phpstan/phpstan-strict-rules": "^1.0.0",
"phpunit/phpunit": "^9.5.0",
"staabm/annotate-pull-request-from-checkstyle": "^1.7.0",
"tracy/tracy": "^2.8.8"
"tracy/tracy": "v2.9.x-dev"
},
"autoload": {
"psr-4": {
Expand Down
42 changes: 42 additions & 0 deletions src/Tracy/LazyTracyToPsrLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use OriNette\DI\Services\ServiceManager;
use Orisai\Exceptions\Logic\MemberInaccessible;
use Psr\Log\LoggerInterface;
use ReflectionClass;
use Tracy\Bridges\Psr\PsrToTracyLoggerAdapter;
use Tracy\ILogger;
use function property_exists;

final class LazyTracyToPsrLogger extends ServiceManager implements ILogger
{
Expand All @@ -17,10 +19,23 @@ final class LazyTracyToPsrLogger extends ServiceManager implements ILogger

private ?ILogger $tracyOriginalLogger;

private bool $loggerOptionsSet = false;

/** @var mixed */
public $fromEmail;

/** @var mixed */
public $emailSnooze;

/** @var mixed */
public $mailer;

public function __construct(array $serviceMap, Container $container, ?ILogger $tracyOriginalLogger = null)
{
parent::__construct($serviceMap, $container);
$this->tracyOriginalLogger = $tracyOriginalLogger;

unset($this->fromEmail, $this->emailSnooze, $this->mailer);
}

/**
Expand All @@ -29,6 +44,33 @@ public function __construct(array $serviceMap, Container $container, ?ILogger $t
*/
public function log($value, $level = self::INFO): void
{
if (!$this->loggerOptionsSet && $this->tracyOriginalLogger !== null) {
$reflector = new ReflectionClass($this);

if (
property_exists($this->tracyOriginalLogger, 'fromEmail')
&& $reflector->getProperty('fromEmail')->isInitialized($this)
) {
$this->tracyOriginalLogger->fromEmail = $this->fromEmail;
}

if (
property_exists($this->tracyOriginalLogger, 'emailSnooze')
&& $reflector->getProperty('emailSnooze')->isInitialized($this)
) {
$this->tracyOriginalLogger->emailSnooze = $this->emailSnooze;
}

if (
property_exists($this->tracyOriginalLogger, 'mailer')
&& $reflector->getProperty('mailer')->isInitialized($this)
) {
$this->tracyOriginalLogger->mailer = $this->mailer;
}

$this->loggerOptionsSet = true;
}

foreach ($this->getLoggers() as $logger) {
$logger->log($value, $level);
}
Expand Down
3 changes: 0 additions & 3 deletions tools/phpstan.tests.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ parameters:
- '#^Class (.+)Logger extends @final class Monolog\\Logger\.$#'

# Workaround for TracyExtension setting Tracy\Logger options via static method instead of service
- message: '#^Access to an undefined property (.+)$#'
path: ../tests/Unit/Tracy/LazyTracyToPsrLoggerTest.php
count: 5
- message: '#^Call to an undefined method (.+)#'
path: ../tests/Unit/Tracy/LazyTracyToPsrLoggerTest.php
count: 2