Categories: 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…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago