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

Added possibility to register many monolog handlers #1

Merged
merged 1 commit into from
Feb 22, 2019
Merged
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
48 changes: 39 additions & 9 deletions DependencyInjection/AppInsightsPHPExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AppInsightsPHP\Symfony\AppInsightsPHPBundle\DependencyInjection;

use AppInsightsPHP\Monolog\Handler\AppInsightsTraceHandler;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -19,19 +20,10 @@ public function load(array $configs, ContainerBuilder $container)

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('app_insights_php.xml');
$loader->load('app_insights_php_monolog.xml');

$container->setParameter('app_insights_php.instrumentation_key', $config['instrumentation_key']);
$container->setParameter('app_insights_php.doctrine.track_dependency', $config['doctrine']['track_dependency']);

if ($config['doctrine']['track_dependency']) {
if (!class_exists('AppInsightsPHP\\Doctrine\\DBAL\\Logging\\DependencyLogger')) {
throw new \RuntimeException('Please first run `composer require download app-insights-php/doctrine-dependency-logger` if you want to log DBAL queries.');
}

$loader->load('app_insights_php_doctrine.xml');
}

$container->setDefinition('app_insights_php.configuration.exceptions',
new Definition(\AppInsightsPHP\Client\Configuration\Exceptions::class, [
$config['exceptions']['enabled'],
Expand Down Expand Up @@ -64,5 +56,43 @@ public function load(array $configs, ContainerBuilder $container)
);

$container->getDefinition('app_insights_php.telemetry.factory')->replaceArgument(1, new Reference('app_insights_php.configuration'));

// Doctrine
if ($config['doctrine']['track_dependency']) {
if (!class_exists('AppInsightsPHP\\Doctrine\\DBAL\\Logging\\DependencyLogger')) {
throw new \RuntimeException('Please first run `composer require download app-insights-php/doctrine-dependency-logger` if you want to log DBAL queries.');
}

$loader->load('app_insights_php_doctrine.xml');
}

// Monolog
if (\count($config['monolog']['handlers'])) {
foreach ($config['monolog']['handlers'] as $name => $handlerConfig) {
$id = sprintf(sprintf('app_insights_php.monolog.handler.%s', $name));

switch ($handlerConfig['type']) {
case 'trace':
$class = AppInsightsTraceHandler::class;
$arguments = [
new Reference('app_insights_php.telemetry'),
$this->levelToMonologConst($handlerConfig['level']),
(bool) $handlerConfig['bubble'],
];
break;
default:
throw new \RuntimeException('Unrecognized monolog handler type %s', $handlerConfig['type']);
}

$container->register($id, $class)
->setArguments($arguments)
->setPublic(false);
}
}
}

private function levelToMonologConst($level)
{
return \is_int($level) ? $level : \constant('Monolog\Logger::'.strtoupper($level));
}
}
25 changes: 25 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace AppInsightsPHP\Symfony\AppInsightsPHPBundle\DependencyInjection;

use Monolog\Logger;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -14,6 +15,8 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder('app_insights_php');
$rootNode = method_exists(TreeBuilder::class, 'getRootNode') ? $treeBuilder->getRootNode() : $treeBuilder->root('app_insights_php');

$allowedLoggerTypes = ['trace'];

$rootNode
->children()
->booleanNode('enabled')->defaultTrue()->end()
Expand Down Expand Up @@ -51,6 +54,28 @@ public function getConfigTreeBuilder()
->booleanNode('track_dependency')->defaultFalse()->end()
->end()
->end()
->arrayNode('monolog')
->addDefaultsIfNotSet()
->children()
->arrayNode('handlers')
->canBeUnset()
->useAttributeAsKey('name')
->arrayPrototype()
->children()
->scalarNode('name')->end()
->scalarNode('level')->defaultValue(Logger::DEBUG)->end()
->scalarNode('bubble')->defaultTrue()->end()
->scalarNode('type')
->defaultValue('trace')
->validate()
->ifNotInArray($allowedLoggerTypes)
->thenInvalid(sprintf('Allowed types: [%s]', implode(', ', $allowedLoggerTypes)))
->end()
->end()
->end()
->end()
->end()
->end()
->end()
;

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,20 @@ app_insights_php:
enabled: true
doctrine:
track_dependency: true
monolog:
handlers:
trace: # register: app_insights_php.monolog.handler.trace - service
type: trace
level: DEBUG
bubble: true
foo: # register: app_insights_php.monolog.handler.foo - service
type: trace
level: ERROR
bubble: true

monolog:
handlers:
app_insights:
type: service
id: "app_insights_php.monolog.handler.trace"
```
23 changes: 23 additions & 0 deletions Tests/DependencyInjection/AppInsightsPHPExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use AppInsightsPHP\Client\Client;
use AppInsightsPHP\Doctrine\DBAL\Logging\DependencyLogger;
use AppInsightsPHP\Monolog\Handler\AppInsightsTraceHandler;
use AppInsightsPHP\Symfony\AppInsightsPHPBundle\DependencyInjection\AppInsightsPHPExtension;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\KernelInterface;
Expand Down Expand Up @@ -96,4 +98,25 @@ public function test_ignored_exceptions_configuration()
$this->assertTrue($this->container->get('app_insights_php.configuration')->exceptions()->isIgnored(\RuntimeException::class));
$this->assertFalse($this->container->get('app_insights_php.configuration')->exceptions()->isIgnored(\Exception::class));
}

public function test_monolog_configuration()
{
$extension = new AppInsightsPHPExtension();
$extension->load(
[[
'instrumentation_key' => 'test_key',
'monolog' => [
'handlers' => [
[
'name' => 'foo.logger',
'level' => Logger::DEBUG,
],
],
],
]],
$this->container
);

$this->assertInstanceOf(AppInsightsTraceHandler::class, $this->container->get('app_insights_php.monolog.handler.foo.logger'));
}
}
26 changes: 26 additions & 0 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace AppInsightsPHP\Symfony\AppInsightsPHPBundle\Tests\DependencyInjection;

use AppInsightsPHP\Symfony\AppInsightsPHPBundle\DependencyInjection\Configuration;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Processor;

Expand All @@ -24,6 +25,31 @@ public function test_default_configuration()
$this->assertTrue($config['enabled']);
$this->assertEquals([], $config['exceptions']['ignored_exceptions']);
$this->assertFalse($config['doctrine']['track_dependency']);
$this->assertEmpty($config['monolog']['handlers']);
}

public function test_monolog_configuration()
{
$configs = [
[
'instrumentation_key' => 'test_key',
'monolog' => [
'handlers' => [
[
'name' => 'foo.logger',
'level' => Logger::DEBUG,
],
],
],
],
];

$config = $this->process($configs);

$this->assertArrayHasKey('foo.logger', $config['monolog']['handlers']);
$this->assertEquals(Logger::DEBUG, $config['monolog']['handlers']['foo.logger']['level']);
$this->assertTrue($config['monolog']['handlers']['foo.logger']['bubble']);
$this->assertEquals('trace', $config['monolog']['handlers']['foo.logger']['type']);
}

protected function process($configs)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"require": {
"php": "^7.1",
"app-insights-php/monolog-handler": "^0.1",
"app-insights-php/monolog-handler": "^0.1.1",
"symfony/framework-bundle": "^3.4|^4.0",
"guzzlehttp/guzzle": "^6.0"
},
Expand Down