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

[HttpKernel] Fix error logger when stderr is redirected to /dev/null #36855

Merged
merged 1 commit into from May 18, 2020

Conversation

fabpot
Copy link
Member

@fabpot fabpot commented May 18, 2020

Q A
Branch? 3.4
Bug fix? yes
New feature? no
Deprecations? no
Tickets n/a
License MIT
Doc PR n/a

The HttpKernel Logger is meant to be used as a last resort logging mechanism when no logger has been explicitly configured (Monolog is not a dependency for instance).

For small apps, that can be more than enough.

But under some circumstances, it does not work. When you are using PHP-FPM, stderr is ignored by default (catch_workers_output is false) and so, logs are ignored as well. There is no issue with the official PHP Docker image as the setting has been explicitly set to true. Not an issue with Symfony CLI as well, as we also change the setting. Not a problem either with the PHP built-in server as it does not use PHP FPM anyway.

But, in many other places, where the setting has its default value, logs are lost (as you can imagine, it happened to me). As this feature is meant to be a fallback, I think it should always work, or at least, we need to make everything possible to make it work out of the box; that's why I've considered it a bug and hence a PR on 3.4.

This PR changes the default value for the output to null, which uses error_log() instead of stderr to log errors. Why is it better? The output of error_log() is controllable by the error_logs PHP ini setting and it is well understood by everyone (the default configuration should always work well); so it should work in most/more cases.

The other change (to be discussed) is to also log messages at the ERROR level and not just the CRITICAL ones.

/cc @dunglas

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

LGTM, here are some minor things.

src/Symfony/Component/HttpKernel/Log/Logger.php Outdated Show resolved Hide resolved
src/Symfony/Component/HttpKernel/Log/Logger.php Outdated Show resolved Hide resolved
Copy link
Member

@dunglas dunglas left a comment

Choose a reason for hiding this comment

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

lgtm

@fabpot fabpot merged commit 7ee33f9 into symfony:3.4 May 18, 2020
@fabpot fabpot deleted the error-logger-fix branch May 18, 2020 19:01
@Naktibalda
Copy link
Contributor

I really don't like this change because php-fpm prepends NOTICE: PHP message: to all lines when error_log is used.

I made logger work the way it did before by adding this section to config/services.yaml

    logger:
        class: Symfony\Component\HttpKernel\Log\Logger
        arguments:
            $output: 'php://stderr'

@stof
Copy link
Member

stof commented Dec 11, 2020

@Naktibalda if your PHP-FPM is configured to preserve stderr, that's a valid configuration (if you still don't want to use a more powerful logger like monolog).
But as explained in the description, the goal of this change is that the fallback logger (when not configuring any logger service) does work whatever the PHP-FPM config.

Having a NOTICE: PHP message: prefix is better than having no logs.

nicolas-grekas added a commit that referenced this pull request Oct 18, 2022
…el Logger (cyve)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[HttpKernel] Remove EOL when using error_log() in HttpKernel Logger

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | maybe
| Tickets       |
| License       | MIT
| Doc PR        |

Hello there !

Since PR #36855, when using `Symfony\Component\HttpKernel\Log\Logger`  with argument `$output = null`. The logger use the `error_log()` method to log messages. But in the log file, messages are separated by an empty line because the default formatter (`Logger::format()`) adds an extra EOL char at the end of the message.

This fix prevents the logger to add an extra EOL char when it uses `error_log()`.

Possible BC : if someone use the logger with a custom formatter that already add a EOL char at the end of the message, there will be a empty line between messages in the log file. In my opinion, this is minor enough not to worry about it ;-)

Thanks for your review :-)

Commits
-------

69cf83e [HttpKernel] Remove EOL when using error_log() in HttpKernel Logger
symfony-splitter pushed a commit to symfony/http-kernel that referenced this pull request Oct 18, 2022
…el Logger (cyve)

This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[HttpKernel] Remove EOL when using error_log() in HttpKernel Logger

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | maybe
| Tickets       |
| License       | MIT
| Doc PR        |

Hello there !

Since PR symfony/symfony#36855, when using `Symfony\Component\HttpKernel\Log\Logger`  with argument `$output = null`. The logger use the `error_log()` method to log messages. But in the log file, messages are separated by an empty line because the default formatter (`Logger::format()`) adds an extra EOL char at the end of the message.

This fix prevents the logger to add an extra EOL char when it uses `error_log()`.

Possible BC : if someone use the logger with a custom formatter that already add a EOL char at the end of the message, there will be a empty line between messages in the log file. In my opinion, this is minor enough not to worry about it ;-)

Thanks for your review :-)

Commits
-------

69cf83ea1a [HttpKernel] Remove EOL when using error_log() in HttpKernel Logger
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

6 participants