Hey there! If you're a Laravel enthusiast like me, you probably understand the importance of effective team communication and collaboration. One powerful way to streamline communication within your team is by integrating Slack notifications into your Laravel applications.
In this step-by-step guide, I'll walk you through sending notifications to a Slack channel using Laravel 10.
So, let's see laravel 10 sends notifications in the Slack channel, how to send notifications to the Slack channel in laravel 8/9/10, real-time logging in laravel using Slack, and how to send logs in Slack Laravel 10.
If you haven't already, start by installing Laravel 10 by using Composer:
composer create-project laravel/laravel my-project "10.*"
In Laravel, there's a handy file called config/logging.php where you can tweak how your app logs information. This file gives you the power to set up different channels for logging in your application.
config/logging.php
<?php
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
'default' => env('LOG_CHANNEL', 'stack'),
'deprecations' => [
'channel' => env('LOG_DEPRECATIONS_CHANNEL', 'null'),
'trace' => false,
],
'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'),
],
'papertrail' => [
'driver' => 'monolog',
'level' => env('LOG_LEVEL', 'debug'),
'handler' => env('LOG_PAPERTRAIL_HANDLER', SyslogUdpHandler::class),
'handler_with' => [
'host' => env('PAPERTRAIL_URL'),
'port' => env('PAPERTRAIL_PORT'),
'connectionString' => 'tls://'.env('PAPERTRAIL_URL').':'.env('PAPERTRAIL_PORT'),
],
],
'stderr' => [
'driver' => 'monolog',
'level' => env('LOG_LEVEL', 'debug'),
'handler' => StreamHandler::class,
'formatter' => env('LOG_STDERR_FORMATTER'),
'with' => [
'stream' => 'php://stderr',
],
],
'syslog' => [
'driver' => 'syslog',
'level' => env('LOG_LEVEL', 'debug'),
],
'errorlog' => [
'driver' => 'errorlog',
'level' => env('LOG_LEVEL', 'debug'),
],
'null' => [
'driver' => 'monolog',
'handler' => NullHandler::class,
],
'emergency' => [
'path' => storage_path('logs/laravel.log'),
],
],
];
In the log file, there are different options.
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
'days' => 14,
],
driver
: This tells laravel the driver to use.
path
: This tells Laravel where to write the logs to.
level
: This tells Laravel the type of log to be handled by this channel. It can be debug
, critical
, warning
etc.
days
: The days
option tells Laravel how long the log should be retained before deleting.
To send notifications to your Slack channel, you'll need to create a Slack app and generate a webhook URL. Follow these steps:
.env
file and add it to your app like below.
LOG_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/xxxxxxxxxx
Open up the logging.php
file then under channels array, since the default is set to stack, let's change the channels to slack
from single
and save the file.
'stack' => [
'driver' => 'stack',
'channels' => ['slack'],
'ignore_exceptions' => false,
],
Let's test it out by opening up Tinker and running the command below:
info("This is just a tutorial demo logging");
Congratulations! You've successfully set up and sent notifications to a Slack channel using Laravel 10.
You might also like: