Start Logging With Laravel
Tags: php, laravel, programming, beginners
Published: 2022-01-26 14:06:58

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
```php
'default' => env('LOG_CHANNEL', 'stack')
```
The second key is channels. This key contains the array of channels that can be used for logging
```php
'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'),
        ],

                ...

    ],

```
- Writing Log Messages

You can send messages to the log using the Log facade. Let's take a look at the following example
```php
use Illuminate\Support\Facades\Log;

$message = "This is my first log";
Log::debug($message);
Log::warning($message);
```
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
```php
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
```
- 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.
```php
use Illuminate\Support\Facades\Log;

$message = "This is my first log";
Log::channel('daily')->debug($message);
```
Given the current configuration, this will create a new daily log storage/logs/laravel-YYY-MM-DD.log
```php
Output:
[2021-04-30 10:07:40] local.DEBUG: This is my first log
```
- 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
```php
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"}
```
- 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
```php
'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'),
        ],
]
```
In the code above, we have created a stack of channels consisting of debug-channel and error-channel
```php
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());
}
```
Code above created two entries in the storage/logs/debug.log file
```php
Output:
[2021-04-30 10:20:00] local.DEBUG: Just a debug message ...  
[2021-04-30 10:20:00] local.ERROR: Something terrible happened!
```
And one entry in the storage/logs/error.log file
```php
Output:
[2021-04-30 10:20:00] local.ERROR: Something terrible happened!
```


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