Skip to content

Commit

Permalink
support for Symfony 4
Browse files Browse the repository at this point in the history
  • Loading branch information
VasekPurchart committed Nov 29, 2018
2 parents 86b4fa7 + ae37afe commit 46d1fa9
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 78 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"require": {
"php": "~7.2",
"psr/log": "~1.0",
"symfony/config": "~3.0",
"symfony/console": "~3.0",
"symfony/dependency-injection": "~3.0",
"symfony/http-kernel": "~3.0",
"symfony/yaml": "~3.0"
"symfony/config": "~4.0",
"symfony/console": "~4.0",
"symfony/dependency-injection": "~4.0",
"symfony/http-kernel": "~4.0",
"symfony/yaml": "~4.0"
},
"require-dev": {
"consistence/coding-standard": "3.5",
Expand Down
35 changes: 16 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ Console Errors Bundle

**Logging of exceptions and error codes for [Symfony Console](http://symfony.com/doc/current/components/console/introduction.html).**

Symfony by default logs uncaught exceptions in your controllers, but does not do this for Console commands. This bundle ensures all uncaught exceptions and errors are logged.
This bundle ensures all uncaught exceptions and errors are logged in the same way as exceptions from controllers.

It also logs all command executions which ended with non-zero return code.
It also logs all command executions which ended with non-zero exit code.

Here is an example showing an exception, error and non-zero return code:
Here is an example showing an exception, error and non-zero exit code:
![Error reports from Console](docs/console-errors.png)

And these are corresponding log entries:
Expand All @@ -23,7 +23,7 @@ Configuration
Configuration structure with listed default values:

```yaml
# app/config/config.yml
# config/packages/console_errors.yml
console_errors:
exceptions:
# Enable logging for exceptions.
Expand All @@ -33,18 +33,18 @@ console_errors:
# Priority with which the listener will be registered.
listener_priority: 0

errors:
# Enable logging for errors (non zero exit codes).
exit_code:
# Enable logging for non-zero exit codes.
enabled: true
# Log level with which errors should be logged (accepts string or integer values).
# Log level with which exit codes should be logged (accepts string or integer values).
log_level: error
# Priority with which the listener will be registered.
listener_priority: 0
```
Symfony by default always converts errors to PHP exceptions. Warnings and notices are converted by default only in development environment. If you want to configure your application to always convert warnings and notices to exceptions use the `debug.error_handler.throw_at` parameter (see [PHP manual](http://php.net/manual/en/errorfunc.constants.php) for other available values):
```yaml
# app/config/config.yml
# config/packages/framework.yml
parameters:
debug.error_handler.throw_at: -1
```
Expand All @@ -54,11 +54,11 @@ You can also override services used internally, for example if you use a non sta
```yaml
services:
my_logger:
class: Monolog\Logger
class: 'Monolog\Logger'
arguments:
- 'my_channel'
vasek_purchart.console_errors.console.logger: @my_logger
vasek_purchart.console_errors.console.logger: '@my_logger'
```

Installation
Expand All @@ -70,14 +70,11 @@ Install package [`vasek-purchart/console-errors-bundle`](https://packagist.org/p
composer require vasek-purchart/console-errors-bundle
```

Register the bundle in your application kernel:
Register the bundle in your application:
```php
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new VasekPurchart\ConsoleErrorsBundle\ConsoleErrorsBundle(),
);
}
// config/bundles.php
return [
// ...
VasekPurchart\ConsoleErrorsBundle\ConsoleErrorsBundle::class => ['all' => true],
];
```
6 changes: 3 additions & 3 deletions src/Console/ConsoleExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace VasekPurchart\ConsoleErrorsBundle\Console;

use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;

class ConsoleExceptionListener
{
Expand All @@ -29,10 +29,10 @@ public function __construct(
$this->logLevel = $logLevel;
}

public function onConsoleException(ConsoleExceptionEvent $event): void
public function onConsoleException(ConsoleErrorEvent $event): void
{
$command = $event->getCommand();
$exception = $event->getException();
$exception = $event->getError();

$message = sprintf(
'%s: %s (uncaught exception) at %s line %s while running console command `%s`',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;

class ConsoleErrorListener
class ConsoleExitCodeListener
{

/** @var \Psr\Log\LoggerInterface */
Expand Down
28 changes: 14 additions & 14 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
class Configuration implements \Symfony\Component\Config\Definition\ConfigurationInterface
{

public const DEFAULT_ERROR_LISTENER_PRIORITY = 0;
public const DEFAULT_ERROR_LOG_LEVEL = LogLevel::ERROR;
public const DEFAULT_EXIT_CODE_LISTENER_PRIORITY = 0;
public const DEFAULT_EXIT_CODE_LOG_LEVEL = LogLevel::ERROR;
public const DEFAULT_EXCEPTION_LISTENER_PRIORITY = 0;
public const DEFAULT_EXCEPTION_LOG_LEVEL = LogLevel::ERROR;

public const PARAMETER_ERROR_ENABLED = 'enabled';
public const PARAMETER_ERROR_LISTENER_PRIORITY = 'listener_priority';
public const PARAMETER_ERROR_LOG_LEVEL = 'log_level';
public const PARAMETER_EXIT_CODE_ENABLED = 'enabled';
public const PARAMETER_EXIT_CODE_LISTENER_PRIORITY = 'listener_priority';
public const PARAMETER_EXIT_CODE_LOG_LEVEL = 'log_level';
public const PARAMETER_EXCEPTION_ENABLED = 'enabled';
public const PARAMETER_EXCEPTION_LISTENER_PRIORITY = 'listener_priority';
public const PARAMETER_EXCEPTION_LOG_LEVEL = 'log_level';

public const SECTION_ERRORS = 'errors';
public const SECTION_EXIT_CODE = 'exit_code';
public const SECTION_EXCEPTIONS = 'exceptions';

/** @var string */
Expand Down Expand Up @@ -59,21 +59,21 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()
->arrayNode(self::SECTION_ERRORS)
->arrayNode(self::SECTION_EXIT_CODE)
->addDefaultsIfNotSet()
->children()
->scalarNode(self::PARAMETER_ERROR_ENABLED)
->info('Enable logging for errors (non zero exit codes).')
->scalarNode(self::PARAMETER_EXIT_CODE_ENABLED)
->info('Enable logging for non-zero exit codes.')
->defaultTrue()
->end()
->append($this->createLogLevelNode(
self::PARAMETER_ERROR_LOG_LEVEL,
'Log level with which errors should be logged (accepts string or integer values).',
self::DEFAULT_ERROR_LOG_LEVEL
self::PARAMETER_EXIT_CODE_LOG_LEVEL,
'Log level with which exit codes should be logged (accepts string or integer values).',
self::DEFAULT_EXIT_CODE_LOG_LEVEL
))
->integerNode(self::PARAMETER_ERROR_LISTENER_PRIORITY)
->integerNode(self::PARAMETER_EXIT_CODE_LISTENER_PRIORITY)
->info('Priority with which the listener will be registered.')
->defaultValue(self::DEFAULT_ERROR_LISTENER_PRIORITY)
->defaultValue(self::DEFAULT_EXIT_CODE_LISTENER_PRIORITY)
->end()
->end()
->end()
Expand Down
16 changes: 8 additions & 8 deletions src/DependencyInjection/ConsoleErrorsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
class ConsoleErrorsExtension extends \Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension
{

public const CONTAINER_PARAMETER_ERROR_LISTENER_PRIORITY = 'vasek_purchart.console_errors.error.listener_priority';
public const CONTAINER_PARAMETER_ERROR_LOG_LEVEL = 'vasek_purchart.console_errors.error.log_level';
public const CONTAINER_PARAMETER_EXIT_CODE_LISTENER_PRIORITY = 'vasek_purchart.console_errors.error.listener_priority';
public const CONTAINER_PARAMETER_EXIT_CODE_LOG_LEVEL = 'vasek_purchart.console_errors.error.log_level';
public const CONTAINER_PARAMETER_EXCEPTION_LISTENER_PRIORITY = 'vasek_purchart.console_errors.exception.listener_priority';
public const CONTAINER_PARAMETER_EXCEPTION_LOG_LEVEL = 'vasek_purchart.console_errors.exception.log_level';

Expand All @@ -23,12 +23,12 @@ class ConsoleErrorsExtension extends \Symfony\Component\HttpKernel\DependencyInj
public function loadInternal(array $mergedConfig, ContainerBuilder $container): void
{
$container->setParameter(
self::CONTAINER_PARAMETER_ERROR_LISTENER_PRIORITY,
$mergedConfig[Configuration::SECTION_ERRORS][Configuration::PARAMETER_ERROR_LISTENER_PRIORITY]
self::CONTAINER_PARAMETER_EXIT_CODE_LISTENER_PRIORITY,
$mergedConfig[Configuration::SECTION_EXIT_CODE][Configuration::PARAMETER_EXIT_CODE_LISTENER_PRIORITY]
);
$container->setParameter(
self::CONTAINER_PARAMETER_ERROR_LOG_LEVEL,
$mergedConfig[Configuration::SECTION_ERRORS][Configuration::PARAMETER_ERROR_LOG_LEVEL]
self::CONTAINER_PARAMETER_EXIT_CODE_LOG_LEVEL,
$mergedConfig[Configuration::SECTION_EXIT_CODE][Configuration::PARAMETER_EXIT_CODE_LOG_LEVEL]
);
$container->setParameter(
self::CONTAINER_PARAMETER_EXCEPTION_LISTENER_PRIORITY,
Expand All @@ -44,8 +44,8 @@ public function loadInternal(array $mergedConfig, ContainerBuilder $container):
if ($mergedConfig[Configuration::SECTION_EXCEPTIONS][Configuration::PARAMETER_EXCEPTION_ENABLED]) {
$loader->load('exception_listener.yml');
}
if ($mergedConfig[Configuration::SECTION_ERRORS][Configuration::PARAMETER_ERROR_ENABLED]) {
$loader->load('error_listener.yml');
if ($mergedConfig[Configuration::SECTION_EXIT_CODE][Configuration::PARAMETER_EXIT_CODE_ENABLED]) {
$loader->load('exit_code_listener.yml');
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
vasek_purchart.console_errors.console.console_error_listener:
class: VasekPurchart\ConsoleErrorsBundle\Console\ConsoleErrorListener
vasek_purchart.console_errors.console.console_exit_code_listener:
class: VasekPurchart\ConsoleErrorsBundle\Console\ConsoleExitCodeListener
arguments:
- '@vasek_purchart.console_errors.console.logger'
- '%vasek_purchart.console_errors.error.log_level%'
Expand Down
4 changes: 2 additions & 2 deletions tests/Console/ConsoleExceptionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Psr\Log\LogLevel;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -33,7 +33,7 @@ public function testLogError(): void
$command = new Command($commandName);
$input = $this->createMock(InputInterface::class);
$output = $this->createMock(OutputInterface::class);
$event = new ConsoleExceptionEvent($command, $input, $output, $exception, 1);
$event = new ConsoleErrorEvent($input, $output, $exception, $command);

$listener = new ConsoleExceptionListener($logger, $logLevel);
$listener->onConsoleException($event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ConsoleErrorListenerTest extends \PHPUnit\Framework\TestCase
class ConsoleExitCodeListenerTest extends \PHPUnit\Framework\TestCase
{

public function testLogError(): void
Expand All @@ -34,7 +34,7 @@ public function testLogError(): void
$output = $this->createMock(OutputInterface::class);
$event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);

$listener = new ConsoleErrorListener($logger, $logLevel);
$listener = new ConsoleExitCodeListener($logger, $logLevel);
$listener->onConsoleTerminate($event);
}

Expand All @@ -58,7 +58,7 @@ public function testLogErrorExitCodeMax255(): void
$output = $this->createMock(OutputInterface::class);
$event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);

$listener = new ConsoleErrorListener($logger, $logLevel);
$listener = new ConsoleExitCodeListener($logger, $logLevel);
$listener->onConsoleTerminate($event);
}

Expand All @@ -77,7 +77,7 @@ public function testZeroExitCodeDoesNotLog(): void
$output = $this->createMock(OutputInterface::class);
$event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);

$listener = new ConsoleErrorListener($logger, LogLevel::DEBUG);
$listener = new ConsoleExitCodeListener($logger, LogLevel::DEBUG);
$listener->onConsoleTerminate($event);
}

Expand Down
Loading

0 comments on commit 46d1fa9

Please sign in to comment.