8000 [Debug] Better error handling by lyrixx · Pull Request #19568 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Debug] Better error handling #19568

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

Merged
merged 1 commit into from
Aug 17, 2016

Conversation

lyrixx
Copy link
Member
@lyrixx lyrixx commented Aug 8, 2016
Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? -
Fixed tickets -
License MIT
Doc PR symfony/symfony-docs#6870
  1. Send the raw exception in the log context instead of custom formatting
  2. Add config option to log/throw in Symfony all PHP errors
  3. Always use an exception when a PHP error occurs
  4. Expand exception in the log context in the web developer toolbar
  5. Use the dumper to dump log context in the web developer toolbar

I used the following code to produce screenshots:

public function indexAction(Request $request)
    {
        $this->get('logger')->info('A log message with an exception', ['exception' => new \Exception('this exception will be logged')]);

        error_reporting(0);
        for ($i=0; $i < 15; $i++) {
            if ($i == 5) {
                error_reporting(E_ALL);
            }
            if ($i == 10) {
                error_reporting(0);
            }

            trigger_error("Trigger error avec E_USER_NOTICE", E_USER_NOTICE);
        }

        error_reporting(E_ALL);

        @trigger_error("trigger_error avec E_USER_DEPRECATED", E_USER_DEPRECATED);
        trigger_error("trigger_error avec E_USER_DEPRECATED (not silent)", E_USER_DEPRECATED);
// ...

screenshot16

screenshot17

screenshot18

@lyrixx lyrixx force-pushed the better-error-logging branch 3 times, most recently from c17f213 to 64b6a4e Compare August 8, 2016 15:13
@lyrixx lyrixx force-pushed the better-error-logging branch 8 times, most recently from 816e296 to 29cb0a7 Compare August 10, 2016 16:38
@@ -373,24 +376,28 @@ public function handleError($type, $message, $file, $line, array $context, array
if (null !== $backtrace && $type & E_ERROR) {
// E_ERROR fatal errors are triggered on HHVM when
// hhvm.error_handling.call_user_handler_on_fatals=1
// which is the way to get their backtrace.
// which is the way to get their backtraces.
Copy link
Member

Choose a reason for hiding this comment

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

Previous sounds correct to me.

@lyrixx lyrixx force-pushed the better-error-logging branch 10 times, most recently from 4d60d08 to 335f47b Compare August 11, 2016 16:00
@lyrixx
Copy link
Member Author
lyrixx commented Aug 11, 2016

Status: Needs review

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Aug 11, 2016

Looks like a really nice improvement to me.
Having a typed exception in the log contexts will allow handling them in more useful ways.
And adding configuration empowers people (see https://twitter.com/nicolasgrekas/status/762915798969098240, such a tie justifies the options).

👍
Status: reviewed

{% if stack %}
<button class="btn-link text-small sf-toggle" data-toggle-selector="#{{ stack_id }}" data-toggle-alt-content="Hide stack trace">Show stack trace</button>
{% if trace %}
<button class="btn-link text-small sf-toggle" data-toggle-selector="#{{ trace_id }}" data-toggle-alt-content="Hide trace trace">Show trace trace</button>
Copy link
Contributor

Choose a reason for hiding this comment

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

Show trace / Hide trace, right ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. Thanks.

@lyrixx lyrixx force-pushed the better-error-logging branch from 335f47b to f19fe82 Compare August 15, 2016 10:55
@nicolas-grekas
Copy link
Member

Unless someone would like to review this PR and needs more time, I plan to merge this one by the end of the day.

@lyrixx
Copy link
Member Author
lyrixx commented Aug 16, 2016

👍

@fabpot
Copy link
Member
fabpot commented Aug 16, 2016

I understand the need for the throw configuration, but not sure it the other ones make sense.

@lyrixx
Copy link
Member Author
lyrixx commented Aug 16, 2016

log also is very important to me. It allow us to manage logging of PHP errors with PHP itself. So all logs are managed in the same way. ie: there are no need to "capture" the output of php fpm.

The scream option could be interesting for people who want really high performance.

For the record, I did not add the trace option because with the new system we always have the trace.

@fabpot
Copy link
Member
fabpot commented Aug 16, 2016

For log, I think that having all logs managed the same way is the only sensible configuration, and should have been the case since the beginning :) So, the question is more about when would you want it to be false?

For scream, we are only talking about dev, right? If so, performance is not a concern IMHO.

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Aug 16, 2016

I think we can drop scope and scream as config options.
I'd be fine with setting log to true and remove the config, but wouldn't this be a behavioral bc break for people who deal and expect log files generated by php?

@lyrixx
Copy link
Member Author
lyrixx commented Aug 16, 2016

Yes, we can not change the value of log to true as it's a kind of BC. I'm gonna remove the 2 others options.

@lyrixx lyrixx force-pushed the better-error-logging branch from f19fe82 to 89feb7f Compare August 16, 2016 15:38
->booleanNode('log')
->info('Use the app logger instead of the PHP logger for logging PHP errors.')
->defaultValue($this->debug)
->treatNullLike($this->debug)
Copy link
Member

Choose a reason for hiding this comment

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

I don't understand the default value. For me, the value does not depend on the debug flag. It should be true by default (most sensible value) and devs can switch it to false to keep BC. But then, what about having this as a temporary setting to keep BC that needs to be removed in 4.0?

Copy link
Member Author

Choose a reason for hiding this comment

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

But then, what about having this as a temporary setting to keep BC that needs to be removed in 4.0?

👍 It was ou initial idea.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a deprecation notice.

Copy link
Member Author

Choose a reason for hiding this comment

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

And I reverted it. I cause too many troubles with tests that can not be fixed.

Copy link
Member

Choose a reason for hiding this comment

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

You reverted the deprecation, but the value is still at debug, which looks very wrong to me still.

Copy link
Member Author

Choose a reason for hiding this comment

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

the value is now false.

@lyrixx lyrixx force-pushed the better-error-logging branch 3 times, most recently from 20585ae to 8829998 Compare August 17, 2016 13:24
1. Send the raw exception in the log context instead of custom formatting
2. Add config option to log in Symfony all PHP errors
@nicolas-grekas
Copy link
Member

Thank you @lyrixx.

@nicolas-grekas nicolas-grekas merged commit 8f24549 into symfony:master Aug 17, 2016
nicolas-grekas added a commit that referenced this pull request Aug 17, 2016
This PR was merged into the 3.2-dev branch.

Discussion
----------

[Debug] Better error handling

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

1. Send the raw exception in the log context instead of custom formatting
2. Add config option to log/throw in Symfony all PHP errors
3. Always use an exception when a PHP error occurs
4. Expand exception in the log context in the web developer toolbar
5. Use the dumper to dump log context in the web developer toolbar

---

I used the following code to produce screenshots:

```php
public function indexAction(Request $request)
    {
        $this->get('logger')->info('A log message with an exception', ['exception' => new \Exception('this exception will be logged')]);

        error_reporting(0);
        for ($i=0; $i < 15; $i++) {
            if ($i == 5) {
                error_reporting(E_ALL);
            }
            if ($i == 10) {
                error_reporting(0);
            }

            trigger_error("Trigger error avec E_USER_NOTICE", E_USER_NOTICE);
        }

        error_reporting(E_ALL);

        @trigger_error("trigger_error avec E_USER_DEPRECATED", E_USER_DEPRECATED);
        trigger_error("trigger_error avec E_USER_DEPRECATED (not silent)", E_USER_DEPRECATED);
// ...
```

![screenshot16](https://cloud.githubusercontent.com/assets/408368/17582279/2c4239b0-5fab-11e6-8428-2eaa7372cce3.png)

![screenshot17](https://cloud.githubusercontent.com/assets/408368/17582287/30cad1ea-5fab-11e6-9b0b-de0fa9f3913b.png)

![screenshot18](https://cloud.githubusercontent.com/assets/408368/17582291/348bb574-5fab-11e6-83b0-5bfaac080838.png)

Commits
-------

8f24549 [Debug] Better error handling
@lyrixx lyrixx deleted the better-error-logging branch August 17, 2016 16:06
fabpot added a commit to symfony/symfony-standard that referenced this pull request Aug 18, 2016
This PR was merged into the 3.2-dev branch.

Discussion
----------

Log all PHP errors with the default logger

related to symfony/symfony#19568

Commits
-------

ba48afe Log all PHP errors with the default logger
fabpot added a commit that referenced this pull request Aug 22, 2016
… of traces (nicolas-grekas)

This PR was merged into the 3.2-dev branch.

Discussion
----------

[FrameworkBundle][Debug] Fix default config and cleaning of traces

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| Tests pass?   | yes
| Fixed tickets | Follow up #19568
| License       | MIT
| Doc PR        | -

The default value of `framework.php_errors.log` must be `%kernel.debug%` to have deprecations and silenced errors logged in dev as before.

Cleaning the trace was broken because a closure can't be bound to an internal class.

This PR fixes both issues and enhance trace cleaning a bit by removing arguments from traces so that they take less memory when collected as part of the context of log messages.

Commits
-------

f640870 [FrameworkBundle][Debug] Fix default config and cleaning of traces
@fabpot fabpot mentioned this pull request Oct 27, 2016
*
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
class SilencedErrorContext implements \JsonSerializable
Copy link
Member Author
@lyrixx lyrixx Jun 14, 2023

Choose a reason for hiding this comment

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

I don't recall, why is it JsonSerializable? (cc @nicolas-grekas)?

It does not play well with monolog:
https://github.com/Seldaek/monolog/blob/b05bf55097060ec20f49ccec0cf2f8e5aaa468b3/src/Monolog/Formatter/NormalizerFormatter.php#L195-L197

I got this in my index:

{
  "_index": "monolog",
  "_type": "_doc",
  "_id": "sU9quogBWJfLYe0zo1VX",
  "_score": 1,
  "fields": {
    "context.exception.Symfony\\Component\\ErrorHandler\\Exception\\SilencedErrorContext.trace.type.keyword": [
      "->"
    ],
    "context.exception.Symfony\\Component\\ErrorHandler\\Exception\\SilencedErrorContext.trace.class.keyword": [
      "Symfony\\Component\\ErrorHandler\\ErrorHandler"
    ],
    "context.exception.Symfony\\Component\\ErrorHandler\\Exception\\SilencedErrorContext.trace.class": [
      "Symfony\\Component\\ErrorHandler\\ErrorHandler"
    ],
    "context.exception.Symfony\\Component\\ErrorHandler\\Exception\\SilencedErrorContext.trace.function": [
      "handleError"
    ],
    "context.exception.Symfony\\Component\\ErrorHandler\\Exception\\SilencedErrorContext.trace.function.keyword": [
      "handleError"
    ],
    "context.exception.Symfony\\Component\\ErrorHandler\\Exception\\SilencedErrorContext.trace.type": [
      "->"
    ],
    "context.exception.Symfony\\Component\\ErrorHandler\\Exception\\SilencedErrorContext.count": [
      1
    ],
    "context.exception.Symfony\\Component\\ErrorHandler\\Exception\\SilencedErrorContext.severity": [
      2
    ],
    "context.exception.Symfony\\Component\\ErrorHandler\\Exception\\SilencedErrorContext.file.keyword": [
      "Unknown"
    ],
    "context.exception.Symfony\\Component\\ErrorHandler\\Exception\\SilencedErrorContext.line": [
      0
    ],
    "context.exception.Symfony\\Component\\ErrorHandler\\Exception\\SilencedErrorContext.file": [
      "Unknown"
    ]
  }
}

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.

6 participants
0