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