[Console][FrameworkBundle] Log console exceptions #21003

Merged
merged 2 commits into from Jan 23, 2017

Projects

None yet

7 participants

@chalasr
Contributor
chalasr commented Dec 20, 2016 edited
Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #10895
License MIT
Doc PR symfony/symfony-docs#7373

Continues #19382, fixing some issues including:

  • ability to display the input string for any InputInterface implementation (cast to string if possible, use the command name otherwise)
  • if the input can be casted as string, cleanup the result (from command "'command:name' --foo=bar" to command "command:name --foo=bar")
  • made ExceptionLister::$logger private instead of protected
  • changed methods name from onKernel* to onConsole* (e.g. onConsoleException) and removed unnecessary doc blocks
  • Added more tests

Log for an exception:

[2016-12-22 00:34:42] app.ERROR: Exception thrown while running command: "cache:clear -vvv". Message: "An error occured!" {"exception":"[object] (RuntimeException(code: 0): An error occured! at /Volumes/HD/Sites/tests/sf-demo-3.2/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php:61)","command":"cache:clear -vvv","message":"An error occured!"} []

@chalasr chalasr changed the title from Automatic console exception logging to [Console][FrameworkBundle] Log console exceptions Dec 20, 2016
@nicolas-grekas nicolas-grekas added this to the 3.x milestone Dec 26, 2016
+ <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>
@lyrixx
lyrixx Jan 15, 2017 Member

I think it could be useful to add a channel. for example console.

@chalasr
chalasr Jan 15, 2017 Contributor

It makes sense, console channel added

@lyrixx
Member
lyrixx commented Jan 15, 2017

👍

@@ -27,7 +27,7 @@
"psr/log": "~1.0"
},
"suggest": {
- "symfony/event-dispatcher": "",
+ "symfony/event-dispatcher": "For using the console events and exception listener",
@fabpot
fabpot Jan 15, 2017 Member

I would not change this.

@chalasr
chalasr Jan 15, 2017 Contributor

Reverted

@chalasr chalasr referenced this pull request in symfony/symfony-docs Jan 16, 2017
Merged

[Console] Update the doc for automatic console logging #7373

@xabbuh
Member
xabbuh commented Jan 17, 2017

What about adding an entry to the changelog?

+
+ <services>
+
+ <service id="console.exception_listener" class="Symfony\Component\Console\EventListener\ExceptionListener">
@xabbuh
xabbuh Jan 17, 2017 Member

could be private

@chalasr
chalasr Jan 17, 2017 Contributor

Since it is an event listener, it needs #21312 to be merged first (see https://travis-ci.org/symfony/symfony/jobs/192665057#L2971)

+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+
+/**
+ * Console exception listener.
@xabbuh
xabbuh Jan 17, 2017 Member

this description doesn't add much value IMO

@chalasr
chalasr Jan 17, 2017 Contributor

I agree, removed

@chalasr
Contributor
chalasr commented Jan 17, 2017

Changelog updated

@jameshalsall @chalasr jameshalsall Add basic support for automatic console exception logging 9896547
@chalasr
Contributor
chalasr commented Jan 18, 2017

@xabbuh The listener is private now

@chalasr
Contributor
chalasr commented Jan 18, 2017 edited

The event-dispatcher does not allow private listeners before 3.3... Can we let it public, or shall I update the framework's event-dispatcher requirement to ~3.3?
Edit: Reverted to public, it's not worth it IMHO.

@xabbuh
Member
xabbuh commented Jan 18, 2017

Why not updating the version and make use of the new features?

@chalasr
Contributor
chalasr commented Jan 18, 2017 edited

@xabbuh To be honest, I'm often asking myself when is it better to update the requirement and I'm never sure. Isn't it too bad to forbid using the FrameworkBundle with older versions than 3.3 just to avoid one more built-in listener being public? When do we consider that it's legit to increase the required version of a component?

Anyway I made the change, thanks

Edit: Got it. It is acceptable to have a conflict for a component in a bundle (here framework), but should be avoided as possible for component-to-component dependencies.

@fabpot
Member
fabpot commented Jan 18, 2017

@chalasr I think most people upgrade all Symfony components from one version to the next, especially as many just use symfony/symfony, which does not provide any flexibility. Having broader constraints help with compatibility with other libs using Symfony though.

@chalasr
Contributor
chalasr commented Jan 18, 2017

Makes sense, thank you for the hint @fabpot

@xabbuh
Member
xabbuh commented Jan 18, 2017

👍

Status: Reviewed

@chalasr
Contributor
chalasr commented Jan 23, 2017

ping @fabpot

@@ -81,6 +81,7 @@ public function load(array $configs, ContainerBuilder $container)
}
$loader->load('fragment_renderer.xml');
+ $loader->load('console.xml');
@fabpot
fabpot Jan 23, 2017 Member

I would only registered this when the Console component is actually available.

@chalasr
chalasr Jan 23, 2017 Contributor

Fixed

@@ -82,6 +83,10 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('fragment_renderer.xml');
+ if (class_exists(Application::class)) {
@fabpot
fabpot Jan 23, 2017 Member

You should add a FileExistenceResource resource so that if the console component is installed, the cache is automatically rebuilt.

@chalasr
chalasr Jan 23, 2017 Contributor

Sorry but I don't understand. We are checking a class existence here, FileExistenceResource is about the filesystem, right?

@chalasr
chalasr Jan 23, 2017 Contributor

Got it! You mean ClassExistenceResource :) done

@chalasr
Contributor
chalasr commented Jan 23, 2017

Build failure is unrelated.

@chalasr chalasr Add Console ExceptionListener
Handle non string-castable inputs

Cleanup input for display

Naming changes

InputInterface doesnt have a toString()

Logger must be private

Remove useless doc blocks

Tweak tests
919041c
@fabpot
Member
fabpot commented Jan 23, 2017

Thank you @chalasr.

@fabpot fabpot merged commit 919041c into symfony:master Jan 23, 2017

2 of 3 checks passed

continuous-integration/travis-ci/pr The Travis CI build failed
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
fabbot.io Your code looks good.
Details
@fabpot fabpot added a commit that referenced this pull request Jan 23, 2017
@fabpot fabpot feature #21003 [Console][FrameworkBundle] Log console exceptions (jam…
…eshalsall, chalasr)

This PR was merged into the 3.3-dev branch.

Discussion
----------

[Console][FrameworkBundle] Log console exceptions

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #10895
| License       | MIT
| Doc PR        | symfony/symfony-docs#7373

Continues #19382, fixing some issues including:
- ability to display the input string for any `InputInterface` implementation (cast to string if possible, use the command name otherwise)
- if the input can be casted as string, cleanup the result (from `command "'command:name' --foo=bar" ` to `command "command:name --foo=bar"`)
- made `ExceptionLister::$logger` private instead of protected
-  changed methods name from `onKernel*` to `onConsole*` (e.g. `onConsoleException`) and removed unnecessary doc blocks
- Added more tests

Log for an exception:

> [2016-12-22 00:34:42] app.ERROR: Exception thrown while running command: "cache:clear -vvv". Message: "An error occured!" {"exception":"[object] (RuntimeException(code: 0): An error occured! at /Volumes/HD/Sites/tests/sf-demo-3.2/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php:61)","command":"cache:clear -vvv","message":"An error occured!"} []

Commits
-------

919041c Add Console ExceptionListener
9896547 Add basic support for automatic console exception logging
c2a6ddc
@chalasr chalasr deleted the chalasr:automatic-console-exception-logging branch Jan 23, 2017
@arno14 arno14 added a commit to arno14/symfony that referenced this pull request Jan 26, 2017
@arno14 arno14 Display file and line on Exception
When an exception occured:
If you have not set a verbose mode when running the command, you don't have information about the file and the line on which the exception occured.
So you must run a second time the command but the exception may not occured this time (if based on some databases datas which have changed) and it's a waste of time if you process is very long and the exception occured at the end.

The feature #21003 solve the problem if you are in a standard Symfony edition, if you leave the default settings and if you are in dev mode
d07a171
@fabpot fabpot added a commit to symfony/symfony-standard that referenced this pull request Feb 1, 2017
@fabpot fabpot minor #1044 Don't log console errors in console output (chalasr)
This PR was merged into the 3.3-dev branch.

Discussion
----------

Don't log console errors in console output

Since symfony/symfony#21003, console errors are logged, and since the `console` channel is not ignored by the monolog console handler, the log is written to stderr.

IMHO it's useless since the ran command already write the message into, it's just having two traces for the same error, which doesn't make sense to me.

I propose to fix that.

__Before__
![before](http://image.prntscr.com/image/16a2773542ee4a0caeaf626b119d5c18.png)

__After__
![after](http://image.prntscr.com/image/facb6641ffdf4df5af90d203c6327fb5.png)

If one wants the trace, then one can run the command in verbose mode or look at its log file.

Commits
-------

2bf1249 Don't log console errors in console output
a9c4e32
@xabbuh xabbuh added a commit to symfony/symfony-docs that referenced this pull request Feb 3, 2017
@xabbuh xabbuh feature #7373 [Console] Update the doc for automatic console logging …
…(chalasr)

This PR was merged into the master branch.

Discussion
----------

[Console] Update the doc for automatic console logging

In symfony/symfony#21003 we are going to provide automatic error/exception logging for the console, similar to what is done in an HTTP context.

In the docs, there is a whole chapter dedicated to this topic. Right now I just removed it, maybe it should be done differently or mentioned somewhere that error/exceptions are automatically logged on the `console` channel?

Thanks

Commits
-------

76d2c91 Update the doc for automatic console logging
76de734
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment