Laravel Daily Logs with Queue or Cron
by Prabhat Rai

Posted on September 05, 2016 at 05:03 PM


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 🙂


 



Share this

Search

  Recent Tips

See All »

  Recent Posts

  • Creating Installer for Laravel Project

    When you are working on a laravel project there is some basic setup that needs to be done for each install of the project. Like Permissions for ...

  • Laravel Queues With Supervisor

    In this blog, we are going to take a look into Laravel Queues. Queues allow you to defer the processing of a time-consuming task, such as sending a...

  • Partials & Utils in Handlebars

    PARTIALS Let’s say we have a structure like the following : data = { students : [{ name : ‘ABC’, roll : 1 }, { name : ‘DEF&...

See All »

  Recent Seminars

  • PHP 8 - Features

    PHP 8 has been officially released in November 2020! This new major update brings many optimizations and powerful features to the language. This...

  • ProxySQL

    ProxySQL is a high-performance SQL proxy. ProxySQL runs as a daemon watched by a monitoring process. The process monitors the daemon and restarts i...

  • Unix Commands & Shell Scripts to help save our time

    Time is important. We should not waste it doing trivial things. Automate things wherever possible.

See All »