Skip to content
Permalink
Browse files

Lazy PsrToTracyLoggerAdapter

  • Loading branch information...
mabar authored and f3l1x committed May 3, 2019
1 parent 9aed0c2 commit 2ae8e6c128a87af435a497e973c1c2ab440da1df
Showing with 49 additions and 3 deletions.
  1. +1 −0 phpstan.neon
  2. +9 −3 src/DI/MonologExtension.php
  3. +39 −0 src/Tracy/LazyTracyLogger.php
@@ -8,3 +8,4 @@ parameters:
ignoreErrors:
- '#^Variable \$manager might not be defined\.$#'
- '#^Call to deprecated method formatPhp\(\) of class Nette\\DI\\ContainerBuilder\.$#'
- '#^Method Contributte\\Monolog\\Tracy\\LazyTracyLogger\:\:log\(\) has parameter (\$priority|\$value) with no typehint specified\.$#'
@@ -6,10 +6,12 @@
use Contributte\Monolog\Exception\Logic\InvalidStateException;
use Contributte\Monolog\LazyLoggerManager;
use Contributte\Monolog\LoggerManager;
use Contributte\Monolog\Tracy\LazyTracyLogger;
use Monolog\Handler\PsrHandler;
use Monolog\Logger;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
use Nette\DI\Container;
use Nette\DI\Statement;
use Nette\PhpGenerator\ClassType;
use Nette\Utils\Strings;
@@ -140,7 +142,12 @@ public function loadConfiguration(): void
->setAutowired(false);
$builder->addDefinition($this->prefix('psrToTracyAdapter'))
->setFactory(PsrToTracyLoggerAdapter::class);
->setFactory(PsrToTracyLoggerAdapter::class)
->setAutowired(false);
$builder->addDefinition($this->prefix('psrToTracyLazyAdapter'))
->setFactory(LazyTracyLogger::class, [$this->prefix('psrToTracyAdapter')])
->setAutowired(false);
}
}
@@ -151,8 +158,7 @@ public function afterCompile(ClassType $class): void
$initialize = $class->getMethod('initialize');
if (class_exists(Debugger::class) && $config['hook']['fromTracy'] && $builder->hasDefinition('tracy.logger')) {
$logger = $builder->getDefinition($this->prefix('psrToTracyAdapter'));
$initialize->addBody($builder->formatPhp('Tracy\Debugger::setLogger(?);', [$logger]));
$initialize->addBody($builder->formatPhp('Tracy\Debugger::setLogger(?);', [$this->prefix('@psrToTracyLazyAdapter')]));
}
if ($config['holder']['enabled']) {
@@ -0,0 +1,39 @@
<?php declare(strict_types = 1);
namespace Contributte\Monolog\Tracy;
use Nette\DI\Container;
use Tracy\Bridges\Psr\PsrToTracyLoggerAdapter;
use Tracy\ILogger;
class LazyTracyLogger implements ILogger
{
/** @var string */
private $loggerServiceName;
/** @var Container */
private $container;
/** @var PsrToTracyLoggerAdapter|null */
private $internalLogger;
public function __construct(string $loggerServiceName, Container $container)
{
$this->loggerServiceName = $loggerServiceName;
$this->container = $container;
}
/**
* @inheritdoc
*/
public function log($value, $priority = self::INFO): void
{
if ($this->internalLogger === null) {
$this->internalLogger = $this->container->getService($this->loggerServiceName);
}
$this->internalLogger->log($value, $priority);
}
}

0 comments on commit 2ae8e6c

Please sign in to comment.
You can’t perform that action at this time.