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

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

Conversation

jameshalsall
Copy link
Contributor

@jameshalsall jameshalsall commented Jul 18, 2016

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes (still to write tests for new feature)
Fixed tickets #10895
License MIT
Doc PR (TO DO)

This is a quick initial concept for automatic exception logging, it currently takes the 2 listeners outlined in the docs (http://symfony.com/doc/current/cookbook/console/logging.html) and brings them into the standard edition.

I'd like to get some feedback before writing tests and updating the docs in a separate PR.

TODO:

  • Confirm messaging
  • Write tests
  • Update docs in separate PR
  • Prevent console errors from writing to stderr

@jameshalsall
Copy link
Contributor Author

ping @Tobion - be good to get your feedback on this before I do any more

$event->setExitCode(255);
}

$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

@jameshalsall
Copy link
Contributor Author

After thinking about this a little more, I still can't help but think this is not a good idea:

  1. Console exceptions are handled by the Logger service and written to stdout by default
  2. When the user passes verbosity flags to the command, e.g. -vvv, they would expect that this information is still written to stdout
  3. I don't think it offers much value to have an exception listener by default that writes to var/log folder when they would be written to stdout as well

I was against this originally in #10895, but thought it might offer some value for people. Now I'm not so sure.

Curious to see what @Tobion thinks as he was the Symfony member who originally was for this change.

@jameshalsall jameshalsall changed the title Add basic support for automatic console exception logging [WIP] [Console] Add basic support for automatic console exception logging Jul 30, 2016

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

$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.

@lyrixx
Copy link
Member

lyrixx commented Aug 9, 2016

👍
(We have the same code in one of our internal repository)

}

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

@jameshalsall jameshalsall force-pushed the automatic-console-exception-logging branch from 6382bf1 to 6acd944 Compare August 9, 2016 18:57
$exitCode
);

$this->logger->error(
Copy link
Member

Choose a reason for hiding this comment

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

We prefer single line here, same above. And in fact I think the intermediate $message is not required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 my bad

@jameshalsall jameshalsall force-pushed the automatic-console-exception-logging branch from b8209e7 to 2874431 Compare August 17, 2016 15:47
$this->logger->error('Command `{command}` exited with status code {code}', array('command' => implode(' ', $_SERVER['argv']), 'code' => $exitCode));
$input = new ArgvInput(null, $event->getCommand()->getDefinition());

$this->logger->error('Command `{command}` exited with status code {code}', array('command' => (string) $input, 'code' => $exitCode));
Copy link
Member

Choose a reason for hiding this comment

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

Do we really need the backtick ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably not, I'll remove them


$exception = $event->getException();

$this->logger->error($exception->getMessage(), 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.

I'm sorry to be very picky, but the log message is not good. I prefer something like that:
'Exception thrown while running command: "%s". Message: "%s".

It's better because if the RDBMS goes down (for example), many commands or consumers will fail. So you will receive N time the exact same message. It's better to have N messages because all commands are different and you must know what command fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No problem, I'll update this later

@@ -23,7 +23,8 @@
"symfony/event-dispatcher": "~2.8|~3.0",
"symfony/filesystem": "~2.8|~3.0",
"symfony/process": "~2.8|~3.0",
"psr/log": "~1.0"
"psr/log": "~1.0",
"phpunit/phpunit": "~4.0"
Copy link
Member

Choose a reason for hiding this comment

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

We never add phpunit dependency in symfony.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not sure why that's showing, I reverted that change last night - was committed by mistake

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I saw that after commenting. Thanks.

@fabpot
Copy link
Member

fabpot commented Sep 14, 2016

@jameshalsall What's the status of this PR?

@jameshalsall
Copy link
Contributor Author

I will finish this off soon :)

James Halsall

Sent from
https://polymail.io/

On Wed, 14 Sep 2016 at 22:55 Fabien Potencier

<
mailto:Fabien Potencier notifications@github.com

wrote:

a, pre, code, a:link, body { word-wrap: break-word !important; }

https://github.com/jameshalsall
What's the status of this PR?

You are receiving this because you were mentioned.

Reply to this email directly,
#19382 (comment)
, or
https://github.com/notifications/unsubscribe-auth/AAeg3Hf0DrRtq-6gSJR7pksk02gbyXRMks5qqG1IgaJpZM4JO_W1
.

@chalasr
Copy link
Member

chalasr commented Nov 6, 2016

It would be great to see this happen.

@jameshalsall
Copy link
Contributor Author

I'll pick this up again this week, had forgotten about it.

@chalasr
Copy link
Member

chalasr commented Jan 15, 2017

Finished in #21003, thanks @jameshalsall

@fabpot
Copy link
Member

fabpot commented Jan 15, 2017

Closing in favor of #21003

@fabpot fabpot closed this Jan 15, 2017
fabpot added a commit that referenced this pull request Jan 23, 2017
…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
@nicolas-grekas nicolas-grekas modified the milestones: 3.x, 3.3 Mar 24, 2017
@haroldiedema
Copy link

Exit codes are used to communicate with the processes that spawned a process (e.g symfony command). In our case, we use exit codes to communicate back to bash processes to indicate what a spawned process did.

Now our 'error'-logs are being spammed for no reason since exit code 3 is NOT an error.

In short: An exit code other than 0 isn't necessarily an error. This commit makes the assumption that it is.

@chalasr
Copy link
Member

chalasr commented Aug 5, 2017

@haroldiedema Would you mind to open a PR for changing the log level?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants