Reputation: 53
I am running Laravel on php-fpm, docker-composed with Nginx, MySQL.
I want to have all logs from Laravel job to be visible from docker-compose logs
command on Host machine, however, remaining unsuccessful.
Following the official document, I could successfully get logs on STDOUT in Controllers, yet impossible when it comes to Jobs.
Here's my example codes.
Successful example in Controllers
class FormController extends Controller
{
public function index(Request $request)
{
\Log::channel('stderr')->info('LOGGING TEST FROM CONTROLLER'); // This works perfectly as expected, i.e. visible with `docker-compose logs` command on the Host!
}
}
Unsuccessful example in Jobs
class SendMailJob implements ShouldQueue
{
public function handle()
{
\Log::channel('stderr')->info('LOGGING TEST FROM JOB'); // This doesn't work X(
}
}
Supervisor conf file
[program:form]
command=php /usr/local/laravel/artisan queue:work database --sleep=3 --tries=3
user=www-data
environment=HOME="/home/www-data",USER="www-data"
autostart=true
numprocs=2
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
I would add that Controllers' examples are run through browsers(loading pages), while Jobs are through Supervisor process (php artisan queue:work...).
(And, in my understanding, STDOUT inside container would be reflected to Docker logs. ...is this correct?)
What I've tried
checked official document's directions - added \Log::channel('stderr')->info('TEST');
on Controller and accessed corresponding route, which was successful.
tried the same method in Job files, which was unsuccessful.
add exec('>&2 echo "TEST"');
in Job class : also didn't work
stopped Supervisor and manually ran php artisan queue:work
after changing current user from root
to www-data
, which is the user running program when I load pages through browser : did actually put out logs in command line, yet invisible from docker-compose logs
command.
*Process through Supervisor is run with user root
by default, so I changed it to www-data
in conf file. However, as in case with user root
, this didn't solve the problem.
I'm assuming using of Supervisor might be the reason... but actually have no ideas clearly.
It'd be great if I could see every logs from Laravel apps, when I run docker-compose logs
command on Host machine.
Currently, the command shows only logs in Controller files (i.e. programs which are to be run through browser accesses).
Can anyone help me? Any suggestions would be really appreciated.
Thanks!
Upvotes: 1
Views: 7500
Reputation: 53
Problem Solved. Here's what I did.
[program:php-fpm]
process_name=%(program_name)s_%(process_num)02d
command=php-fpm
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
[program:form]
process_name=%(program_name)s_%(process_num)02d
command=php /usr/local/laravel/artisan queue:work database --sleep=3 --tries=3
autostart=true
autorestart=true
numprocs=2
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
Set CMD ['/usr/bin/supervisord']
on Dockerfile and run docker-compose up -d
.
Tried job dispatch, and then docker-compose logs
on Host.
This showed me every logs from either php-fpm or supervisor.
As I've commented, it might be fundamentally un-welcomed strategy to put both php-fpm and supervisor together on the same container, though.
Upvotes: 2