Development

How to create and use a custom log file in Laravel

Creating and using a custom log file in Laravel, this can help you have a more direct approach to debugging and understanding you application

Open the config/logging.php file and in the channels array add in your new log and its settings.

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

This custom logger is going to be called “logins” (channel name) and its driver being “single” means one filepath (file).

Now to write logs to this log file:

\Log::channel('logins')->info("User {$user->username} logged in");

Output in logs/logins.log:

[2023-04-07 22:36:58] local.INFO: User chief logged in

There 8 different logging levels as per the RFC 5424 specification:

  • emergency
  • alert
  • critical
  • error
  • warning
  • notice
  • info
  • debug

These are ordered from most severe to less severe, as the custom log file created above has its base level as debug it can receive all these log message types.

An example for critical level:

\Log::channel('logins')->critical("This is a critical log!");

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

1 year ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

1 year ago

FTP getting array of file details such as size using PHP

Using FTP and PHP to get an array of file details such as size and…

2 years ago

Creating Laravel form requests

Creating and using Laravel form requests to create cleaner code, separation and reusability for your…

2 years ago

Improving the default Laravel login and register views

Improving the default Laravel login and register views in such a simple manner but making…

2 years ago

Laravel validation for checking if value exists in the database

Laravel validation for checking if a field value exists in the database. The validation rule…

2 years ago