├── app-log-php-fpm-via-unix-pipe ├── bootstrap-php-log-tail.sh ├── Dockerfile-php-log-tail ├── nginx.conf ├── bootstrap-php-fpm.sh ├── index.php ├── Dockerfile-nginx ├── nginx-vhost.conf ├── docker-compose.yml └── Dockerfile-php ├── app-log-php-fpm-via-catch-workers-output ├── nginx.conf ├── index.php ├── Dockerfile-nginx ├── docker-compose.yml ├── nginx-vhost.conf └── Dockerfile-php └── README.md /app-log-php-fpm-via-unix-pipe/bootstrap-php-log-tail.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Wait until the pipe - created by the PHP app container 4 | # and shared via a tmp volume - is created 5 | while [ ! -e /var/log/shared/pipe-from-app-to-stdout ]; do 6 | sleep 0.2 7 | done 8 | 9 | exec tail -f /var/log/shared/pipe-from-app-to-stdout 10 | -------------------------------------------------------------------------------- /app-log-php-fpm-via-unix-pipe/Dockerfile-php-log-tail: -------------------------------------------------------------------------------- 1 | FROM alpine:3.8 2 | 3 | # Create the directory that will hold the (shared) unix pipe 4 | RUN mkdir /var/log/shared 5 | 6 | # Add bootstrap script 7 | ADD bootstrap-php-log-tail.sh /usr/local/bin/bootstrap-php-log-tail.sh 8 | 9 | CMD [ "/usr/local/bin/bootstrap-php-log-tail.sh" ] 10 | -------------------------------------------------------------------------------- /app-log-php-fpm-via-unix-pipe/nginx.conf: -------------------------------------------------------------------------------- 1 | # This is NOT a production-grade nginx vhost config. Do NOT use it in 2 | # production. It's just the minimal setup to test PHP logging. 3 | daemon off; 4 | 5 | error_log /dev/stderr error; 6 | 7 | events { 8 | worker_connections 1024; 9 | } 10 | 11 | http { 12 | access_log /dev/stdout; 13 | include sites-enabled/*.conf; 14 | } 15 | -------------------------------------------------------------------------------- /app-log-php-fpm-via-catch-workers-output/nginx.conf: -------------------------------------------------------------------------------- 1 | # This is NOT a production-grade nginx vhost config. Do NOT use it in 2 | # production. It's just the minimal setup to test PHP logging. 3 | daemon off; 4 | 5 | error_log /dev/stderr error; 6 | 7 | events { 8 | worker_connections 1024; 9 | } 10 | 11 | http { 12 | access_log /dev/stdout; 13 | include sites-enabled/*.conf; 14 | } 15 | -------------------------------------------------------------------------------- /app-log-php-fpm-via-unix-pipe/bootstrap-php-fpm.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # Create a pipe used by the PHP app to write logs 4 | if [ ! -p /var/log/shared/pipe-from-app-to-stdout ]; then 5 | mkfifo /var/log/shared/pipe-from-app-to-stdout 6 | chmod 777 /var/log/shared/pipe-from-app-to-stdout 7 | fi 8 | 9 | exec php-fpm7 --fpm-config /etc/php7/php-fpm.conf --php-ini /etc/php7/php.ini 10 | -------------------------------------------------------------------------------- /app-log-php-fpm-via-catch-workers-output/index.php: -------------------------------------------------------------------------------- 1 | 11 | [PHP on Kubernetes: application logging via unix pipe](https://pracucci.com/php-on-kubernetes-application-logging-via-unix-pipe.html) 12 | 13 | ### Option 1: log to `php://stdout` or `php://stderr` and enable `catch_workers_output` in php-fpm 14 | 15 | ![](https://pracucci.com/assets/2019-01-24-php-on-kubernetes-php-fpm-catch-workers-output-b470128db4fbe35b4b68b0980076dec9e95be49c1b3dae929ec5ddb9f92aecb3.png) 16 | 17 | - **Pro**: easy. 18 | - **Con**: application logs are wrapped by php-fpm and it makes parsing more complicated. 19 | - **Con**: application logs are truncated to 1024 bytes and splitted into multiple messages. Long logs are not unusual if you use structured logging and you log contextual information on errors (ie. stack trace). 20 | - **Con**: application logs are mixed with php-fpm logs into the same stream. 21 | 22 | See a working example at [`app-log-php-fpm-via-catch-workers-output/`](app-log-php-fpm-via-catch-workers-output/). 23 | 24 | ### Option 2: log to unix pipe and tail it in a sidecar container
25 | 26 | ![](https://pracucci.com/assets/2019-01-24-php-on-kubernetes-unix-pipe-148f44c75d4bf6babed6a4effbd2f3f2a542a62563d3288ac5e2fd67c407c85a.png) 27 | 28 | See a working example at [`app-log-php-fpm-via-unix-pipe/`](app-log-php-fpm-via-unix-pipe/). 29 | 30 | - **Pro**: application logs are not wrapped by php-fpm. 31 | - **Pro**: application logs are not limited by length (no splitting / truncating). 32 | - **Pro**: application logs and php-fpm error logs are not mixed together in the same stream. 33 | --------------------------------------------------------------------------------