Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Homegenize exception logging between web and console #29138

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
tkleinhakisa opened this issue Nov 8, 2018 · 6 comments
Closed

Homegenize exception logging between web and console #29138

tkleinhakisa opened this issue Nov 8, 2018 · 6 comments

Comments

@tkleinhakisa
Copy link

Description
Hi, currently when an exception is thrown it is handled either by \Symfony\Component\HttpKernel\EventListener\ExceptionListener in a web context or \Symfony\Component\Console\EventListener\ErrorListener in console context.

In the web context, the logging happens depending on the exception code and type

if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
    $this->logger->critical($message, array('exception' => $exception));
} else {
    $this->logger->error($message, array('exception' => $exception));
}

In the console context, exception are always logged as errors:

$this->logger->error('Error thrown while running command "{command}". Message: "{message}"', array('error' => $error, 'command' => $inputString, 'message' => $error->getMessage()));

If you configure a logger to log only CRITICAL and above, the exception will be logged in the web context but not in a console context

Is there a particular reason for this difference ? do you think the console ErrorListener could be improved to have a similar behavoir like ExceptionListener ?

Thank you

@Tobion
Copy link
Contributor

Tobion commented Nov 17, 2018

The reason is that Http exception is console context would be pretty strange. I can totally see that it might happen due to people throwing http exceptions even in places that are not necessarily run in http context at all. But IMO we should not explicitly support this just to make the logging more similar.

So 👎

@Tobion
Copy link
Contributor

Tobion commented Nov 17, 2018

Btw, why do you not want to log error logs?

@tkleinhakisa
Copy link
Author

Hello and thanks for the answer, i agree about http exception in console context, i am not suggesting to check for them in console ErrorListener. I was more thinking about raising the log level to critical in console but it probably has some consequences i don't measure.

I use monolog for logging and defined a stack of handlers including a mailer which i want to report only critical (errors are still logged by other handlers in files)
If a service that throws a custom exception is used in a web context i will get my email, if it is used in console context i will not

@javiereguiluz
Copy link
Member

@nicolas-grekas do you think is there something we could do to improve this ... or should we close it as "won't change"? Thanks.

@nicolas-grekas
Copy link
Member

No opinion here, maybe we can close because this didn't get enough traction?

@javiereguiluz
Copy link
Member

Yes, let's close. My own opinion was 👎 but not very strong ... so let's close. Thanks.

jderusse added a commit that referenced this issue Jan 28, 2021
This PR was merged into the 4.4 branch.

Discussion
----------

[Console] Fix console logger according to PSR-3

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #39050, #29138
| License       | MIT

`Symfony\Component\HttpKernel\EventListener\ErrorListener` logs non-HTTP exceptions at `LogLevel::CRITICAL`.
`Symfony\Component\Messenger\Worker` logs unrecoverable exceptions at `LogLevel::CRITICAL`.
`Symfony\Component\Console\EventListener\ErrorListener` logs exceptions at `LogLevel::ERROR`.

As per PSR-3, unexpected and unrecoverable exceptions should be logged at `LogLevel::CRITICAL`.

Commits
-------

69fcd07 Fix console logger according to PSR-3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants