PHP "StdOut" Logs are Actually Delivered to Host as StdErr
jimdelois opened this issue · comments
Summary: When using behance/docker-php:7.4
, any logging or stream writing within a PHP application that is configured for stdout
(e.g., php://stdout
) actually arrives at the docker host on stderr
.
To reproduce:
git clone git@github.com:jimdelois/behance-docker-php-stdout-bug
cd behance-docker-php-stdout-bug
docker-compose up -d
Enter the Docker Machine Name for your local
DOCKER_MACHINE_NAME=""
Send a request to the container:
curl -XGET http://$(docker-machine ip $DOCKER_MACHINE_NAME):$(docker port badboy 8080 | awk '{split($0,a,":"); print a[2]}')
Examine the Docker logging by examining the raw container logs from the docker host
(e.g., docker-machine ssh $DOCKER_MACHINE_NAME
, thence tail -F /var/lib/docker/containers/$CONTAINER_ID/$CONTAINER_ID-json.log
):
- Actual:
{"log":"I want a Bad Boy Sandwich\n","stream":"stderr","time":"2020-11-19T06:18:08.672226893Z"}
- Expected:
{"log":"I want a Bad Boy Sandwich\n","stream":"stdout","time":"2020-11-19T06:18:08.672226893Z"}
Notes:
- Observe baseline intended functionality in the repo file
jimdelois/behance-docker-php-stdout-bug/public/index.php
- Raw PHP stream explicitly set to
php://stdout
; Loggers (e.g., Monolog) in production applications typically use this stream - The unexpected outcome is that containers are sending "normal" PHP output to STDERR, which affects metrics in AWS, FluentD routing, Sumo queries, etc.
@jimdelois call that semi-deliberate. You can separate nginx
logs of stdout
and php-fpm
logs on stderr
.
Confirm with this:
PHP-only:
docker run --rm -p 8080:8080 behance/docker-php:7.4 1>/dev/null
Nginx-only:
docker run --rm -p 8080:8080 behance/docker-php:7.4 2>/dev/null
@bryanlatten Hmmm.... My issue isn't that I need PHP and NGinx separated from one another (I can do that downstream based on a regex or something), it's that I would desire to separate the PHP logs themselves between errors and regular log entries, or at least to have control of that from within the app.
I'll look into this more to see if I can correct it for my own use cases but, given that it's "semi-deliberate," can you give me a head start and point me to where that decision is configured or do you have any tips on where I might look to change it? No worries if not, I'll dig in eventually.
@bryanlatten I see now that it's essentially a product of the FPM spec itself. Sadness. This article spells it out for me https://pracucci.com/php-on-kubernetes-application-logging-via-unix-pipe.html
Closing, as there is no viable resolution in the domain of this project.