DEV Community

Morcos Gad
Morcos Gad

Posted on

Start Logging With Laravel

You will learn how to log the behaviour of your Laravel application. This will help you troubleshoot your application much more easily and quickly. Finding bugs can be frustrating and time-consuming. Luckily, Laravel comes with out of the box logging tools that will help a lot.

In application development, logging is crucial regardless of the platform or language. In this tutorial, we will take a look at one of the most popular PHP frameworks - Laravel.

  • Configuration

All the configuration options of your application's logging behaviour are located in the config/logging.php

'default' => env('LOG_CHANNEL', 'stack')
Enter fullscreen mode Exit fullscreen mode

The second key is channels. This key contains the array of channels that can be used for logging

'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['single'],
            'ignore_exceptions' => false,
        ],

        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
        ],

        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
            'days' => 14,
        ],

        'slack' => [
            'driver' => 'slack',
            'url' => env('LOG_SLACK_WEBHOOK_URL'),
            'username' => 'Laravel Log',
            'emoji' => ':boom:',
            'level' => env('LOG_LEVEL', 'critical'),
        ],

                ...

    ],

Enter fullscreen mode Exit fullscreen mode
  • Writing Log Messages

You can send messages to the log using the Log facade. Let's take a look at the following example

use Illuminate\Support\Facades\Log;

$message = "This is my first log";
Log::debug($message);
Log::warning($message);
Enter fullscreen mode Exit fullscreen mode

Given the current un-edited configuration that comes out of the box with Laravel, this code will create two entries in the storage/logs/laravel.log

Output:
[2021-04-30 09:48:54] local.DEBUG: This is my first log  
[2021-04-30 09:48:54] local.WARNING: This is my first log
Enter fullscreen mode Exit fullscreen mode
  • Writing To Specific Channels

By default, all logs are directed to the stack channel. If you want to send a message to a specific channel that is not the default channel, you can do that by calling channel() method on the Log facade.

use Illuminate\Support\Facades\Log;

$message = "This is my first log";
Log::channel('daily')->debug($message);
Enter fullscreen mode Exit fullscreen mode

Given the current configuration, this will create a new daily log storage/logs/laravel-YYY-MM-DD.log

Output:
[2021-04-30 10:07:40] local.DEBUG: This is my first log
Enter fullscreen mode Exit fullscreen mode
  • Contextual Information

You can also provide contextual data in the form of an array. This data will be formatted and printed alongside the log message

use Illuminate\Support\Facades\Log;

Log::error("Error happend :(", ["id" => "12345", "username" => "some_guy"]);

Output:
[2021-04-30 10:44:56] local.ERROR: Error happend :( {"id":"12345","username":"some_guy"}
Enter fullscreen mode Exit fullscreen mode
  • Building Log Stack

As mentioned in the Channel drivers section, stack driver allows us to stack multiple channels together and send one message to all the channels at once. We can make a stack of multiple channels where each has a different driver therefore we can create a simple hierarchy. Let's take a look at the following example

'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['debug-channel','error-channel'],
            'ignore_exceptions' => false,
        ],

        'debug-channel' => [
            'driver' => 'single',
            'path' => storage_path('logs/debug.log'),
            'level' => env('LOG_LEVEL', 'error'),
        ],

        'error-channel' => [
            'driver' => 'single',
            'path' => storage_path('logs/error.log'),
            'level' => env('LOG_LEVEL', 'error'),
        ],
]
Enter fullscreen mode Exit fullscreen mode

In the code above, we have created a stack of channels consisting of debug-channel and error-channel

use Illuminate\Support\Facades\Log;

Log::debug("Just a debug message ...");

try{
    throw new Exception('Something terrible happened!');
}
catch(Exception $e){
    Log::error($e->getMessage());
}
Enter fullscreen mode Exit fullscreen mode

Code above created two entries in the storage/logs/debug.log file

Output:
[2021-04-30 10:20:00] local.DEBUG: Just a debug message ...  
[2021-04-30 10:20:00] local.ERROR: Something terrible happened!
Enter fullscreen mode Exit fullscreen mode

And one entry in the storage/logs/error.log file

Output:
[2021-04-30 10:20:00] local.ERROR: Something terrible happened!
Enter fullscreen mode Exit fullscreen mode

I hope you listened to the code, but to go deeper, please add these links
https://laravel.com/docs/8.x/logging
https://betterstack.com/community/guides/logging/php/how-to-start-logging-with-laravel

Top comments (0)