amazon web services - Sending PHP application logs to AWS App Runner container application log (stdout, stderr) - Stack Overflow

I have a container running in App Runner with a PHP application on it. It has no outbound internet acce

I have a container running in App Runner with a PHP application on it. It has no outbound internet access nor Endpoint to log directly to CloudWatch and so want to utilise the application logs provided by App Runner.

Nginx logs are posted there which include the request details etc however every way I've tried to post the PHP application logs to it it fails:

  • Logging to /proc/1/fd/1 fails due to permission issues
  • Logging directly fails due to network connectivity
  • Running a background task via tail -f /var/log/file > /proc/1/fd/1 & in the startup script works locally however doesn't work in App Runner and I have no logging information why.

EDIT

Also tried supervisord and tailing the log as a process:

[program:logging]
command=/bin/tail -f /app/log/file.log
stdout_logfile=/proc/1/fd/1
stdout_logfile_maxbytes=0
stderr_logfile=/proc/1/fd/1
stderr_logfile_maxbytes=0
priority=300

Once again this works on local containers but not in App Runner

Is there a way to send the logs to the App Runner Application Logs for a PHP app?

I have a container running in App Runner with a PHP application on it. It has no outbound internet access nor Endpoint to log directly to CloudWatch and so want to utilise the application logs provided by App Runner.

Nginx logs are posted there which include the request details etc however every way I've tried to post the PHP application logs to it it fails:

  • Logging to /proc/1/fd/1 fails due to permission issues
  • Logging directly fails due to network connectivity
  • Running a background task via tail -f /var/log/file > /proc/1/fd/1 & in the startup script works locally however doesn't work in App Runner and I have no logging information why.

EDIT

Also tried supervisord and tailing the log as a process:

[program:logging]
command=/bin/tail -f /app/log/file.log
stdout_logfile=/proc/1/fd/1
stdout_logfile_maxbytes=0
stderr_logfile=/proc/1/fd/1
stderr_logfile_maxbytes=0
priority=300

Once again this works on local containers but not in App Runner

Is there a way to send the logs to the App Runner Application Logs for a PHP app?

Share Improve this question edited Mar 29 at 7:45 Olivier 18.4k1 gold badge11 silver badges31 bronze badges asked Mar 21 at 11:34 RudigerRudiger 6,70913 gold badges53 silver badges105 bronze badges 11
  • Are you using non-root permission to logging to /proc/1/fd/1? – vht981230 Commented Mar 29 at 4:26
  • Yeah using it in this case, removed for space but yes I am. – Rudiger Commented Mar 29 at 4:33
  • 1 I think there could be some permission block from AppRunner to prevent root from redirecting to /proc/1/fd/1. Have you tried creating a periodic log cron job as root RUN echo "* * * * * root tail -f /app/log/file.log > /proc/1/fd/1" >> /etc/crontab in the container dockerfile to see if it is allowed – vht981230 Commented Mar 30 at 1:46
  • I have supervisors running an nginx task as root that directs logs successfully to /proc/1/fd/1. I've also just echod a random string in the startup script and that successfully gets put into the log. Just can't seem to run tail or a long running task (not sure which) in App Runner, can local. – Rudiger Commented Mar 30 at 10:58
  • 1 I've got a crappy work around using DynamoDB (which runs as a gateway endpoint rather than a service endpoint so free) that calls lambda to write the logs. – Rudiger Commented Mar 30 at 11:00
 |  Show 6 more comments

1 Answer 1

Reset to default 1 +100

I believe you can redirect your logs to /dev/stderr as mentioned in docker's official documentation.

Sample code:

$stderr = fopen( 'php://stderr', 'w' );
fwrite($stderr, "Written through the PHP error stream" );
fclose($stderr);

This will output in the docker logs like this:

2025-04-02 09:00:27 Written through the PHP error stream
2025-04-02 09:00:27 192.168.65.1 - - [02/Apr/2025:00:00:27 +0000] "GET /log.php HTTP/1.1" 200 68 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36" "-"

And based from this Q&A, I was able to verify in my sample App Runner that the application logs are being written from PHP.

For my exact setup, I'm using serversideup/php:8.3-fpm-nginx as the base image and I've placed log.php to /var/www/html/public folder. My App Runner is configured to build the app from my private ECR.

Here is the code for log.php

<?php

echo '<h1>PHP: Hello World!</h1>';
error_log('Hello World from plain PHP');

$stderr = fopen( 'php://stderr', 'w' );
fwrite($stderr, "Written through the PHP error stream" );
fclose($stderr);

?>

<h1>Hello World HTML</h1>

Here is the full Dockerfile:

FROM --platform=linux/amd64 serversideup/php:8.3-fpm-nginx

# Copy Source Code
COPY --chown=www-data . /var/www/html

RUN composer install \
    --no-dev \
    --optimize-autoloader \
    --prefer-dist && \
    php artisan optimize

Take note, I'm using the laravel framework. But when I testing the logging, I'm using public/log.php which can be accessible by http://localhost:8080/log.php

Hope this helps,

Regards

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744356931a4570276.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信