In Laravel logs are written to storage/logs/laravel.log file by default. You can tag your laravel log entries with different log types such as INFO, DEBUG and ERROR though.
But sometime you may require to create 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.
Hence in this article on Laravel custom log I will show you in detail how to create custom log file in Laravel. In this article I will show you a simple example of configuring and implementing custom log file. Using this example of custom log file you can create custom log file in laravel 6, laravel 7, laravel 8 and laravel 9 version.
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 |
A 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 |
A SyslogUdpHandler based Monolog driver |
single |
A single file or path based logger channel (StreamHandler ) |
slack |
A SlackWebhookHandler based Monolog driver |
stack |
A wrapper to facilitate creating “multi-channel” channels |
syslog |
A SyslogHandler based Monolog driver |
How to create custom log in Laravel?
In order to create custom log file add a channel with a new key name as shown below:
... 'channels' => [ ... 'invoicing' => [ 'driver' => 'single', 'path' => storage_path('logs/invoicing.log'), ], ],
Now in the code use
\Log::channel('invoicing')->info('Something happened in invoicing!'); dd('log testing done!');
I hope it helps someone!
One thought on “How to Create Custom Log File in Laravel”