How to Create Custom Log File in Laravel Example

In this article on Laravel custom logs I will show you how to create custom log file in Laravel. With help of simple example I will show you the method for configuring and implementing custom log file. Using this example you can create custom log file in Laravel 6, Laravel 7, Laravel 8, 9 and Laravel 10 version.

Logging in Laravel

In Laravel framework  the application logs are written to storage/logs/laravel.log file by default. You can tag the log entries with different names such as INFO, DEBUG and ERROR though. (Logs can also be stored in database but I won’t cover that in this article.)

Why to create Custom Log File?

Sometimes you may wish to create a different log file for specific task. For example, you are working with invoicing and you need a separate log file for invoicing information so that you can easily figure out issues or events related to invoicing.

How to Create Custom Log File in Laravel

Before creating a custom log file in Laravel I will show you the list of default channel drivers which exist in the Laravel by default.

Name Description
custom A driver that calls a specified factory to create a channel
daily RotatingFileHandler based Monolog driver which rotates daily
errorlog An ErrorLogHandler based Monolog driver
monolog A Monolog factory driver that may use any supported Monolog handler
null A driver that discards all log messages
papertrail SyslogUdpHandler based Monolog driver
single A single file or path based logger channel (StreamHandler)
slack SlackWebhookHandler based Monolog driver
stack A wrapper to facilitate creating “multi-channel” channels
syslog SyslogHandler based Monolog driver

How to Create Custom Log in Laravel?

In order to create a custom log file all you need to do is to add a channel with a new key name as shown below. Open the config/logging.php and add the following code to it. (make necessary changes)

...
'channels' => [
    ...
    'invoicing' => [
        'driver' => 'single',
        'path' => storage_path('logs/invoicing.log'),
    ],
],

Now in the code you can use it like:

\Log::channel('invoicing')->info('Something happened in invoicing!');
dd('log testing done!');

Conclusion

In Laravel, the logs are written to laravel.log file by default. Howerver you can create your own custom log file by adding a new configuration entry to the config/logging.php file.

One thought on “How to Create Custom Log File in Laravel Example

Leave a Reply