8000 [WIP] [Console] Add basic support for automatic console exception logging by jameshalsall · Pull Request #19382 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[WIP] [Console] Add basic support for automatic console exception logging #19382

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

Closed
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
Next Next commit
Add basic support for automatic console exception logging
  • Loading branch information
jameshalsall authored and James Halsall committed Aug 17, 2016
commit 4284161d9c3683dd893116a9ec9581167ee75bf5
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('web.xml');
$loader->load('services.xml');
$loader->load('fragment_renderer.xml');
$loader->load('console.xml');

// A translator must always be registered (as support is included by
// default in the Form component). If disabled, an identity translator
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/console.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="console.exception_listener" class="Symfony\Component\Console\EventListener\ExceptionListener">
<tag name="kernel.event_subscriber" />
<argument type="service" id="logger" on-invalid="null" />
</service>
</services>
</container>
91 changes: 91 additions & 0 deletions src/Symfony/Component/Console/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Symfony\Component\Console\EventListener;

use Psr\Log\LoggerInterface;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Console exception listener.
*
* Attempts to log exceptions or abnormal terminations of console commands.
*
* @package Symfony\Component\Console\EventListener;
* @author James Halsall <james.t.halsall@googlemail.com>
*/
class ExceptionListener implements EventSubscriberInterface
{
protected $logger;

/**
* Constructor.
*
* @param LoggerInterface $logger A logger
*/
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
}

/**
* Handles console command exception.
*
* @param ConsoleExceptionEvent $event Console event
*/
public function onKernelException(ConsoleExceptionEvent $event)
{
if (null === $this->logger) {
return;
}

$exception = $event->getException();

$message = sprintf(
'%s: %s (uncaught exception) at %s line %s while running console command `%s`',
get_class($exception),
$exception->getMessage(),
$exception->getFile(),
$exception->getLine(),
$event->getCommand()->getName()
);

$this->logger->error($message, array('exception' => $exception));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about a much simpler:
$this->logger->error($exception->getMessage(), array('exception' => $exception));
?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

/**
* Handles termination of console command.
*
* @param ConsoleTerminateEvent $event Console event
*/
public function onKernelTerminate(ConsoleTerminateEvent $event)
{
if (null === $this->logger) {
return;
}

$exitCode = $event->getExitCode();

if ($exitCode === 0) {
return;
}

if ($exitCode > 255) {
$event->setExitCode(255);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, this should not be done here.
And BTW, it's already done there https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Console/Application.php#L141-L147

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point, I will remove this

}

$message = sprintf('Command `%s` exited with status code %d');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks something else than what the title of this PR suggests. moreover, I'm not sure it's forth logging a non zero status code: it's the command's responsibility to know if it's worth logging of not, don't you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea of this PR is to add automatic exception logging "out of the box" with zero configuration. Maybe I'm misunderstanding but surely anything other than a 0 exit code would result in some kind of log entry, unless there's a fatal PHP error before Symfony kernel bootstraps

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. something is missing in the sprintf
  2. I would add the current argument/options

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated


$this->logger->warning($message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put error.

}

public static function getSubscribedEvents()
{
1E0A return array(
ConsoleEvents::EXCEPTION => array('onKernelException', -128),
ConsoleEvents::TERMINATE => array('onKernelTerminate', -128)
);
}
}
0