Sending Laravel Logs to Mattermost Channels

Search for a command to run...

No comments yet. Be the first to comment.
Recently, I switched to another bank, and after setting up the online banking credentials and trying to log in for the first time, I found a form similar to this one. The system requests characters

Introduction Attributes were introduced in PHP 8 around four years ago. As outlined in the RFC, attributes provide a structured and syntactic way to add metadata to declarations such as classes, properties, functions, methods, parameters, and constan...

If you are a web applications builder with Laravel and happens to use PHPStan for static code analysis, you will start seeing new errors when you upgrade to Laravel 11.x. In a fresh Laravel install with PHPStan, the first time running ./vendor/bin/ph...

Laravel Sanctum is a lightweight authentication package for SPA applications and APIs. It was released in 2020 and became available out of the box since Laravel 8. Unlike JWT self-contained tokens, Sanctum uses a reference token. What is a Reference ...

In a previous post, we talked about how ULID can be a better choice for primary keys in MySQL than UUIDs. In this tutorial, we will go over how to use ULID in Laravel Framework. ULID has been supported in Laravel since v9.30.1 and has received some i...

Mattermost is an open-source platform for communication and collaboration with integrations with many tools. It is mostly considered as an open-source alternative to Slack and Microsoft Teams.
Although Laravel does not have an official log channel driver for Mattermost, we can build a custom Monolog handler that can be easily configured in Laravel apps.
I have created a small package for this purpose that, unlike the existing Mattermost handlers, format the message according to Mattermost docs
$ composer require muhamadhhassan/laramost
In your config/logging.php file, add the mattermost channel to the channels array:
use LaraMost\Formatter\MattermostFormatter;
use LaraMost\Handler\MattermostWebhookHandler;
'channels' => [
'mattermost' => [
'driver' => 'monolog',
'handler' => MattermostWebhookHandler::class,
'formatter' => MattermostFormatter::class,
'with' => [
'hook' => 'https://your-mattermost.com/hooks/random-string',
],
'level' => 'error'
],
],
You can follow the steps here to create an incoming webhook for your channel.
Monolog levels are used to set the message color and icon
| Level Name | Level Value | Color | Emoji |
| DEBUG | 100 | #91C4EB | 🔍 |
| INFO | 200 | #91C4EB | ℹ |
| NOTICE | 250 | #99cc33 | 📝 |
| WARNING | 300 | #ffcc00 | ⚠ |
| ERROR | 400 | #cc3300 | 🐛 |
| CRITICAL | 500 | #cc3300 | ❌ |
| EMERGENCY | 600 | #cc3300 | 🚨 |
Simply, using Laravel Log facade
Log::channel('mattermost')->error('Something went wrong', ['user_id' => 5]);
Will send the following message to your mattermost channel:

⚠ Warning: When you log to the
mattermostchannel make sure that the level is greater than or equals the one defined inconfig/logging.php
And there you have it! A simple implementation to send log records to a Mattermost channel.