Everything works correctly locally.
But suddenly the users of your project start to find errors in production.
And you find it difficult to fix them without additional information.
It's happened to you?
Today we are going to see how to use Logs and Alerts, to be aware of errors in your Laravel project, and you can fix them in time.
Laravel Logging
Laravel offers several drivers for logging channels.
That is, several alternative ways to write logs.
Among the channel drivers we have more used:
daily.- Driver based on Monolog. He writes about files, and rotates every day.single.- He writes about archives, but always about the same one.null.- Simply discard all log messages.slack.- Allows you to send logs to a Slack channel.papertrail.- Allows you to send logs to Papertrail. We'll see more about this below.stack.- Acts as a wrapper to send logs to multiple channels.
Papertrail
Papertrail is a service that allows us to manage logs in a very simple way.
We send our logs to Papertrail and then we will be able to search logs based on filters, and define alerts.
How to set up a Papertrail channel
To write logs on a Papertrail channel we need only 2 pieces of data: host y port.
To get this configuration data you need to create a Papertrail account.
You can [sign up from here] to get extra storage space for free.
Set up alerts in Papertrail
Papertrail allows us to view incoming log messages in Real-time.
However, we are not going to be aware of the event viewer all the time.
That's why we can also set alerts in Papertrail:
To create an alert we indicate:
- What logs are we interested in (in this case our query alerts us to error logs)
- How often we want to assess our alertness (e.g., every day, every hour, etc)
- The condition that will trigger the alert (if no matches were found, if at least X matches were found)
- And finally our time zone
Send logs over multiple channels
If we use the driver stack we can define multiple logging channels.
For example, in the video tutorial above, we defined a "stack" to send logs to 2 channels: daily y papertrail, as follows.
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['syslog', 'slack'],
],
// ...
],
Log levels
You can Write log messages using the following methods:
use Illuminate\Support\Facades\Log;
Log::emergency($message);
Log::alert($message);
Log::critical($message);
Log::error($message);
Log::warning($message);
Log::notice($message);
Log::info($message);
Log::debug($message);
Each of them corresponds to a log level determined.
Background information
The methods also accept a second argument, to add contextual information.
That is, you can send an associative array, so that when reviewing logs the context in which they occurred is better understood.
For example, if a user failed to sign in, we'll be interested in knowing what their ID is:
Log::info('User failed to login.', ['id' => $user->id]);
If an error occurs when processing a payment, the contextual information would be, for example, the amount to be paid, the concept of the payment (product or service purchased), in whose name the payment is made, and what the transaction ID is.
Conclusion
As you can see, Setting up logs and alerts in a project Laravel is very simple.
In the example video we see how to use the driver stack, to send our logs to both daily as a papertrail.
- In this way our messages are written to files locally,
- and additionally they are sent to Papertrail, so that it sends us alerts in case they are frequent.
We actually did tests, and we got this Mail alert electronic:
