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 | Show 6 more comments1 Answer
Reset to default 1 +100I 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
/proc/1/fd/1
? – vht981230 Commented Mar 29 at 4:26/proc/1/fd/1
. Have you tried creating a periodic log cron job as rootRUN 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:46supervisors
running an nginx task as root that directs logs successfully to/proc/1/fd/1
. I've also justecho
d a random string in the startup script and that successfully gets put into the log. Just can't seem to runtail
or a long running task (not sure which) in App Runner, can local. – Rudiger Commented Mar 30 at 10:58