Laravel Daily Logs with Queue or Cron

Queues and task scheduler in laravel works like a charm. But it can fail in a certain case.
In this post, I am going to explain the case where it can fail and how to overcome this.

So let’s go step by step
1. We have setup a simple queue that sends an email to the admin regarding daily activity on the site and log Info before and after sending the mail.
2. If we have Log settings for single log file everything will work fine but if we have the daily file selected then it will break and will not be logged also. So you will have no idea what is going wrong.

So what is the problem here ?

When we had single log file then we had one log file : “laravel.log” which had full permission [777]. so we were able to log and everything was working 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. If you had a catch block also, you will try to log this error and the same exception will be raised as the user is still “cli“.

And this case can occur in the case of Task Scheduler[Cron Jobs] too, because while processing the scheduled task the user will be “cli“.

So, to overcome this, change monolog settings so that for different users/processes 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 returning the app.

Now you will have log files for cli too, and everything started from the command line will be logged in that file.

It will look like

For Http App logs : laravel-apache2handler-daemon-2016-09-04.log
For Command line processes : laravel-cli-username-2016-09-04.log

This also keeps log files clean.
First of all, we will have different files for different days making it much easier to track errors.
Second HTTP app logs will be in a different file and the Queue/Cron logs will be in a different file.

Note : After changing the settings please don’t forget to run

# To make sure new config is loaded correctly

php artisan config:clear

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

php artisan queue:restart

For More on Queue visit : Laravel Queues With Supervisor

Hope this helps. Happy Coding 🙂



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s