Create Custom Logs in Laravel

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);

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'),
        ],

    ]

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..']);

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


Recommended

How to Create Multiple Parameters Dynamic Routes in Laravel

Laravel 8 Multiple Database and Resource Routes with Controllers

Optimize Database Queries in Laravel

Read For more Tutorials about Laravel

If you like this, share this.

Follow us on FacebookTwitterTumblrLinkedIn.