Logging

Logging is a way to save information about what happens in your application. It helps you to find errors, check user actions, or debug problems.

Default Logging

By default, Laravel saves logs in the storage/logs/laravel.log file. But you can also create your own custom logs.

How to create a custom log

1. Edit config/logging.php

Open config/logging.php and add a new log channel. For example, let's create a log for authentication actions:

'channels' => [
  // other channels...

  'authlog' => [
    'driver' => 'single',
    'path' => storage_path('logs/authlog.log'),
    'level' => 'info',
  ],
],

This will create a file storage/logs/authlog.log where all auth-related logs are saved.

2. Use the custom log in a Controller

In your controller, you can write to the authlog like this:

use Illuminate\Support\Facades\Log;

public function login(Request $request)
{
  // Log a message to authlog
  Log::channel('authlog')->info('User tried to login: ' . $request->email);

  // normal login code...
}

3. Check the log file

After the user tries to login, the log will be saved in:

storage/logs/authlog.log

Inside authlog.log, you will see something like:

[2025-03-19 12:34:56] local.INFO: User tried to login: test@example.com