Home > Net >  Separate log files for each time a job is run laravel
Separate log files for each time a job is run laravel

Time:11-03

I have a Laravel 8 application where I run my jobs against specific accounts. For each account, I need a separate log file in a format such as "logFileName_122323123_2021-01-01" (logFileName__) and so on.

Similar question how to customize file name of log in Laravel 8.0? but this does not help me create a new file each time I run the job.

I tried in my job constructor but since I'm calling the Log statically, it doesn't retain its value.

Config::set(['logging.channels.processPbsNormalCampaignJob.path' => storage_path('logs/processPbsNormalCampaignJob_'.$this->accountId.'_' . date('Y-m-d') . '.log')]);

What would be the best approach?

CodePudding user response:

Starting from Laravel v8.66.0 you can create a log channel on the fly. You can check the PR as Docs as well

So Based on Docs you can do

Illuminate\Support\Facades\Log::build([
        'driver' => 'daily',
        'path' => storage_path('logs/processPbsNormalCampaignJob_' . $this->accountId . '_' . date('Y-m-d') . '.log'),
    ])->info('Your Log data here !!');
  • Related