laravellogginglaravel-8laravel-jobs

How do I log properly a Laravel Job?


Reading the official documentation I understand that it's necessary to use Illuminate\Support\Facades\Log, but the configuration in logging.php is a bit confusing to me. I basically don't understand how the channel drivers work, it seems to me a bit overcomplicated.

Logging commands, however, is pretty easy in my opinion: you just add ->appendOutputTo('command.log') in the schedule method and the job is pretty much done. Is there a similar method to log jobs? Or is the way in the documentation the only one? If so, could anyone be kind enough to simplify it to me?


Solution

  • It is simple to use the Log facade,

    Go to loggin.php and add a new channel, here is a signle log file example (without rotation)

    'job' =>  [
                'driver' => 'single',
                'path' => storage_path('logs/job.log'),
                'level' => 'info',
            ],
    

    Then use it anywhere with

    use Illuminate\Support\Facades\Log;
    
    Log::channel('job')->info($content);
    //or
    Log::channel('job')->error($content);