Skip to content
Permalink
Browse files

Improve Tracy hook

  • Loading branch information...
mabar authored and f3l1x committed May 3, 2019
1 parent 4b2b017 commit 9aed0c2b666f01c34de9b83c588f506e5bbbdb99
Showing with 37 additions and 86 deletions.
  1. +3 −2 .docs/README.md
  2. +1 −1 phpstan.neon
  3. +33 −11 src/DI/MonologExtension.php
  4. +0 −72 src/Tracy/TracyAdapter.php
@@ -23,8 +23,9 @@ extensions:

```yaml
monolog:
tracy:
hook: true # use monolog inside tracy (required to log exceptions with monolog)
hook:
fromTracy: true # enabled by default, log through Tracy into Monolog
toTracy: true # enabled by default, log through Monolog into Tracy
channel:
default: # default channel is required
handlers:
@@ -7,4 +7,4 @@ includes:
parameters:
ignoreErrors:
- '#^Variable \$manager might not be defined\.$#'
- '#^Method Contributte\\Monolog\\Tracy\\TracyAdapter\:\:log\(\) has parameter (\$message|\$priority) with no typehint specified\.$#'
- '#^Call to deprecated method formatPhp\(\) of class Nette\\DI\\ContainerBuilder\.$#'
@@ -6,12 +6,15 @@
use Contributte\Monolog\Exception\Logic\InvalidStateException;
use Contributte\Monolog\LazyLoggerManager;
use Contributte\Monolog\LoggerManager;
use Contributte\Monolog\Tracy\TracyAdapter;
use Monolog\Handler\PsrHandler;
use Monolog\Logger;
use Nette\DI\Compiler;
use Nette\DI\CompilerExtension;
use Nette\DI\Statement;
use Nette\PhpGenerator\ClassType;
use Nette\Utils\Strings;
use Tracy\Bridges\Psr\PsrToTracyLoggerAdapter;
use Tracy\Bridges\Psr\TracyToPsrLoggerAdapter;
use Tracy\Debugger;
class MonologExtension extends CompilerExtension
@@ -20,8 +23,9 @@ class MonologExtension extends CompilerExtension
/** @var mixed[] */
private $defaults = [
'channel' => [],
'tracy' => [
'hook' => true,
'hook' => [
'fromTracy' => true, // log through Tracy
'toTracy' => true, // log through Monolog
],
'holder' => [
'enabled' => false,
@@ -57,13 +61,27 @@ public function loadConfiguration(): void
}
}
$tracyHandler = null;
if (class_exists(Debugger::class) && $config['hook']['toTracy'] && $builder->hasDefinition('tracy.logger')) {
$tracyAdapter = new Statement(TracyToPsrLoggerAdapter::class);
$tracyAdapter->arguments = ['@tracy.logger'];
$tracyHandler = new Statement(PsrHandler::class);
$tracyHandler->arguments = [$tracyAdapter];
}
foreach ($config['channel'] as $name => $channel) {
$channel = $this->validateConfig($this->channelDefaults, $channel, $this->prefix('channel.' . $name));
if (!is_string($name)) {
throw new InvalidArgumentException(sprintf('%s.channel.%s name must be a string', $this->name, (string) $name));
}
if ($tracyHandler !== null) {
$channel['handlers']['tracy'] = $tracyHandler;
}
if (!isset($channel['handlers']) || $channel['handlers'] === []) {
throw new InvalidStateException(sprintf('%s.channel.%s.handlers must contain at least one handler', $this->name, $name));
}
@@ -117,23 +135,27 @@ public function loadConfiguration(): void
}
}
if ($config['tracy']['hook'] === true && class_exists(Debugger::class)) {
if ($builder->hasDefinition('tracy.logger')) {
$builder->removeDefinition('tracy.logger');
$builder->addAlias('tracy.logger', $this->prefix('tracyAdapter'));
}
if (class_exists(Debugger::class) && $config['hook']['fromTracy'] && $builder->hasDefinition('tracy.logger')) {
$builder->getDefinition('tracy.logger')
->setAutowired(false);
$builder->addDefinition($this->prefix('tracyAdapter'))
->setFactory(TracyAdapter::class, [$this->prefix('@logger.default'), Debugger::$logDirectory, Debugger::$email]);
$builder->addDefinition($this->prefix('psrToTracyAdapter'))
->setFactory(PsrToTracyLoggerAdapter::class);
}
}
public function afterCompile(ClassType $class): void
{
$builder = $this->getContainerBuilder();
$config = $this->validateConfig($this->defaults);
$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]));
}
if ($config['holder']['enabled']) {
$initialize = $class->getMethod('initialize');
$initialize->addBody('Contributte\Monolog\LoggerHolder::setLogger($this->getByType(?));', [Logger::class]);
}
}

This file was deleted.

0 comments on commit 9aed0c2

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