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

Options to disable error listener and/or enable Monolog handler #247

Merged
merged 3 commits into from Sep 30, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Fix compatibility with sentry/sentry 2.2+ (#244)
- Add support for `class_serializers` option (#245)
- Add support for `max_request_body_size` option (#249)
- Add option to disable the error listener completely (#247, thanks to @HypeMC)
- Add options to register the Monolog Handler (#247, thanks to @HypeMC)

## 3.1.0 - 2019-07-02
- Add support for Symfony 2.8 (#233, thanks to @nocive)
Expand Down
20 changes: 11 additions & 9 deletions README.md
Expand Up @@ -103,17 +103,20 @@ the [PHP specific](https://docs.sentry.io/platforms/php/#php-specific-options) o
#### Optional: use monolog handler provided by `sentry/sentry`
*Note: this step is optional*

If You're using `monolog` for logging e.g. in-app errors, You
If you're using `monolog` for logging e.g. in-app errors, you
can use this handler in order for them to show up in Sentry.

First, define `Sentry\Monolog\Handler` as a service in `config/services.yaml`
First, enable & configure the `Sentry\Monolog\Handler`; you'll also need
to disable the `Sentry\SentryBundle\EventListener\ErrorListener` to
avoid having duplicate events in Sentry:

```yaml
services:
sentry.monolog.handler:
class: Sentry\Monolog\Handler
arguments:
$level: 'error'
sentry:
register_error_listener: false # Disables the ErrorListener
monolog:
error_handler:
enabled: true
level: error
```

Then enable it in `monolog` config:
Expand All @@ -123,8 +126,7 @@ monolog:
handlers:
sentry:
type: service
id: sentry.monolog.handler
level: error
id: Sentry\Monolog\Handler
```

## Maintained versions
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Expand Up @@ -32,20 +32,24 @@
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.8",
"jangregor/phpstan-prophecy": "^0.3.0",
"monolog/monolog": "^1.11||^2.0",
"php-http/mock-client": "^1.0",
"phpstan/phpstan": "^0.11",
"phpstan/phpstan-phpunit": "^0.11",
"phpunit/phpunit": "^7.5",
"scrutinizer/ocular": "^1.4",
"symfony/expression-language": "^2.8||^3.0||^4.0"
},
"suggest": {
"monolog/monolog": "Required to use the Monolog handler"
},
"autoload": {
"psr-4" : {
"psr-4": {
"Sentry\\SentryBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4" : {
"psr-4": {
"Sentry\\SentryBundle\\Test\\": "test"
}
},
Expand Down
22 changes: 22 additions & 0 deletions src/DependencyInjection/Configuration.php
Expand Up @@ -37,6 +37,10 @@ public function getConfigTreeBuilder(): TreeBuilder
->ifString()
->then($this->getTrimClosure());

$rootNode->children()
->booleanNode('register_error_listener')
->defaultTrue();

// Options array (to be passed to Sentry\Options constructor) -- please keep alphabetical order!
$optionsNode = $rootNode->children()
->arrayNode('options')
Expand Down Expand Up @@ -139,6 +143,24 @@ public function getConfigTreeBuilder(): TreeBuilder
$listenerPriorities->scalarNode('console_error')
->defaultValue(128);

// Monolog handler configuration
$monologConfiguration = $rootNode->children()
->arrayNode('monolog')
->addDefaultsIfNotSet()
->children();

$errorHandler = $monologConfiguration
->arrayNode('error_handler')
->addDefaultsIfNotSet()
->children();
$errorHandler->booleanNode('enabled')
->defaultFalse();
$errorHandler->scalarNode('level')
->defaultValue('DEBUG')
Jean85 marked this conversation as resolved.
Show resolved Hide resolved
->cannotBeEmpty();
$errorHandler->booleanNode('bubble')
->defaultTrue();

return $treeBuilder;
}

Expand Down
50 changes: 48 additions & 2 deletions src/DependencyInjection/SentryExtension.php
Expand Up @@ -2,7 +2,9 @@

namespace Sentry\SentryBundle\DependencyInjection;

use Monolog\Logger as MonologLogger;
use Sentry\ClientBuilderInterface;
use Sentry\Monolog\Handler;
use Sentry\Options;
use Sentry\SentryBundle\Command\SentryTestCommand;
use Sentry\SentryBundle\ErrorTypesParser;
Expand All @@ -14,6 +16,7 @@
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
Expand Down Expand Up @@ -47,8 +50,9 @@ public function load(array $configs, ContainerBuilder $container): void
$container->setParameter('sentry.listener_priorities.' . $key, $priority);
}

$this->tagConsoleErrorListener($container);
$this->configureErrorListener($container, $processedConfiguration);
$this->setLegacyVisibilities($container);
$this->configureMonologHandler($container, $processedConfiguration['monolog']);
}

private function passConfigurationToOptions(ContainerBuilder $container, array $processedConfiguration): void
Expand Down Expand Up @@ -134,6 +138,17 @@ private function valueToCallable($value)
return $value;
}

private function configureErrorListener(ContainerBuilder $container, array $processedConfiguration): void
{
if (! $processedConfiguration['register_error_listener']) {
$container->removeDefinition(ErrorListener::class);

return;
}

$this->tagConsoleErrorListener($container);
}

/**
* BC layer for Symfony < 3.3; see https://symfony.com/blog/new-in-symfony-3-3-better-handling-of-command-exceptions
*/
Expand Down Expand Up @@ -166,9 +181,40 @@ private function setLegacyVisibilities(ContainerBuilder $container): void
if (Kernel::VERSION_ID < 30300) {
$container->getDefinition(SentryTestCommand::class)->setPublic(true);
$container->getDefinition(ConsoleListener::class)->setPublic(true);
$container->getDefinition(ErrorListener::class)->setPublic(true);
$container->getDefinition(RequestListener::class)->setPublic(true);
$container->getDefinition(SubRequestListener::class)->setPublic(true);

if ($container->hasDefinition(ErrorListener::class)) {
$container->getDefinition(ErrorListener::class)->setPublic(true);
}
}
}

private function configureMonologHandler(ContainerBuilder $container, array $monologConfiguration): void
{
$errorHandler = $monologConfiguration['error_handler'];

if (! $errorHandler['enabled']) {
$container->removeDefinition(Handler::class);

return;
}

if (! class_exists(Handler::class)) {
throw new LogicException(
sprintf('Missing class "%s", try updating "sentry/sentry" to a newer version.', Handler::class)
);
}

if (! class_exists(MonologLogger::class)) {
throw new LogicException(
sprintf('You cannot use "%s" if Monolog is not available.', Handler::class)
);
}

$container
->getDefinition(Handler::class)
->replaceArgument('$level', MonologLogger::toMonologLevel($errorHandler['level']))
->replaceArgument('$bubble', $errorHandler['bubble']);
}
}
6 changes: 6 additions & 0 deletions src/Resources/config/services.xml
Expand Up @@ -51,5 +51,11 @@
<service id="Sentry\SentryBundle\Command\SentryTestCommand" class="Sentry\SentryBundle\Command\SentryTestCommand" public="false">
<tag name="console.command" />
</service>

<service id="Sentry\Monolog\Handler" class="Sentry\Monolog\Handler" public="false">
<argument type="service" id="Sentry\State\HubInterface" />
<argument key="$level" />
<argument key="$bubble" />
</service>
</services>
</container>
8 changes: 8 additions & 0 deletions test/DependencyInjection/ConfigurationTest.php
Expand Up @@ -48,6 +48,7 @@ public function testConfigurationDefaults(): void
$processed = $this->processConfiguration([]);
$expectedDefaults = [
'dsn' => null,
'register_error_listener' => true,
'listener_priorities' => [
'request' => 1,
'sub_request' => 1,
Expand All @@ -67,6 +68,13 @@ public function testConfigurationDefaults(): void
'project_root' => '%kernel.root_dir%/..',
'tags' => [],
],
'monolog' => [
'error_handler' => [
'enabled' => false,
'level' => 'DEBUG',
'bubble' => true,
],
],
];

if (method_exists(Kernel::class, 'getProjectDir')) {
Expand Down