DEV Community

Cover image for Create Custom Logs in Laravel
Chetan Rohilla
Chetan Rohilla

Posted on • Edited on • Originally published at w3courses.org

Create Custom Logs in Laravel

Log is a way which we can use to keep track of input data, output data, requests made through our applications, log system error messages, notify about informations to teams. In our laravel applications we can use logging services to create logs. Here we will create custom logs in laravel also. Laravel logging is based on “channels”. Each channel represents a specific way of writing log information.

Laravel uses the config/logging.php file for log’s configuration. To log the information laravel uses the channel name from this file based on environment production or local. By default, Laravel use the stack channel when logging messages. You can read in detail about Laravel logs here.

Creating Logs in Laravel

If you are using the controller or model or custom class then you have add this line at top. In blade templates you can directly use Log.

use Log;

$message = 'Custom String'; //string logs
$message = ['key' => 'value']; //array logs

Log::emergency('Emergency message : ', $message);
Log::alert('Alert message : ', $message);
Log::critical('Critical message : ', $message);
Log::error('Error message : ', $message);
Log::warning('Warning message : ', $message);
Log::notice('Notice message : ', $message);
Log::info('Info message : ', $message);
Log::debug('Debug message : ', $message);
Enter fullscreen mode Exit fullscreen mode

Now, open storage\logs\laravel.log and you will see your logs.

Create Custom Logs in Laravel

To create custom logs just add the below code in your config/logging.php file inside the channels array.

'channels' => [

        'custom_log_channel' => [
            'driver' => 'single',
            'path' => storage_path('custom_logs/laravel.log'),
        ],

    ]
Enter fullscreen mode Exit fullscreen mode

Here we have used the driver single (A single file or path) and our path. Now log using the below code.

Log::channel('custom_log_channel')->info('Custom LOGS => ',['name' => 'Custom Logs Working..']);
Enter fullscreen mode Exit fullscreen mode

Now, open the file storage\custom_logs\laravel.log and you will see your logs.


Please like share and give positive feedback to motivate me to write more.

For more tutorials visit my website.

Thanks:)
Happy Coding:)

Top comments (1)

Collapse
 
afheisleycook profile image
technoshy

Great job!