Thursday, 20 February 2025

Custom logging in Laravel 11

 It's often difficult to see what's going on in your code. Many people have difficulty setting up debugging or even if they have, following through the break points. Commonly developers try to resolve this through:

dd

or

var_dump

Another feature which can be used to help in this process is logging. Laravel holds a very good feature to support this. Imagine you are adding a feature called "indeed" and you want to log the progress of your application through it's data points.

You can open up the file:

config/logging.php

and add lines like these:

'channels' => [

    // Other channels...

    'indeed' => [

        'driver' => 'single',

        'path' => storage_path('logs/indeed.log'),

        'level' => 'info',

    ],

],

Now you can add a line like this to your code within the "indeed" feature:

Log::channel('indeed')->info('This is a custom log message');

Look inside the directory:

storage/logs

You will see a file called indeed.log containing your output.

No comments:

Post a Comment