Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Structured logs on Cloud Functions gen2 with PHP runtime

Cloud Functions gen2 uses Cloud Run to build and deploy a container. Based on the selected runtime in Cloud Functions, i guess there is a default Dockerfile. For PHP runtimes (php 7.4 + 8.1) there is an issue with passing structured logs to php://stdout en php://stderr. By default, php-fpm decorates all data sent to stdout and stderr. Because of this, the structured log becomes an invalid json string.

Log sent to stdout from code:

 

{"severity":"ERROR","message":"This is a sample error log"}

 

 

After php-fpm decoration:

 

[31-Jul-2022 11:30:51] WARNING: [pool app] child 18 said into stderr: "{"severity":"ERROR","message":"This is a test log"}" 

 

 

This could be fixed by setting decorate_workers_output to off in the php-fpm config. This could be an extra step in the default php runtime Dockerfile used by Cloud Functions.

1 1 887
1 REPLY 1

Yes please! Maybe this explains why there is no tutorial example for structured logging with php (see https://cloud.google.com/functions/docs/monitoring/logging#functions-log-structured-python).  

Is there any workaround that can be used today to make structured logging in a gcf work? Not being able to write logs is a bit of a deal breaker.