Search code examples
logginglaravel-5queuejobs

Logging not working in laravel queue job


I have the below settings in my supervisor/conf.d/myconf.conf file :

[program:my-worker]    
process_name=%(program_name)s_%(process_num)02d    
command=php /var/www/html/artisan queue:work sqs --queue=my_queue_name --tries=3 --daemon    
autostart=true    
autorestart=true    
user=root    
numprocs=1    
redirect_stderr=true    
stdout_logfile=/var/www/html/storage/logs/mylogfile.log

I have give all permissions to storage/logs/mylogfile.log file. Not able to figure out why is it still not logging,


Solution

  • I guess you are using Daily logs.

    When we have single log file then we have one log file : "laravel.log" which we give full permission [777], so we are able to log and everything works as expected.

    But when we have daily file selected then each day the laravel app will create a new file whenever it needs to log something. Now this file's owner is the web server (daemon/www-root) because laravel is run by the user daemon/www-root.

    When the queue is getting processed the user in action is "cli", and it does not have permission to write to this file. so it throws an exception and the processing stops.

    What I would suggest to do if you are using daily logs is change monolog settings so that for different users different log files will be created.

    Add this code to bootstrap/app.php

    /**
     * Configure Monolog.
     */
    $app->configureMonologUsing( function( Monolog\Logger $monolog) {
        $processUser = posix_getpwuid( posix_geteuid() );
        $processName= $processUser[ 'name' ];
    
        $filename = storage_path( 'logs/laravel-' . php_sapi_name() . '-' . $processName . '.log' );
        $handler = new Monolog\Handler\RotatingFileHandler( $filename );
        $monolog->pushHandler( $handler );
    });
    

    Just before retrurning the app.

    Now you will have log files for cli too, and everything related to queue will be logged in that file.

    This also keeps log files clean. HTTP app logs will be in different file and the Queue processing logs will be in different file.

    Also please don't forget to run following commands.

    To make sure new config is loaded correctly

    php artisan config:clear
    

    As you are using supervisor daemon queue worker, so you will need to broadcast the queue restart signal.

    php artisan queue:restart
    

    Hope this helps.