Laravel 10 Send Notifications in Slack Channel
Tags: laravel, laravel10, notification, slack
Published: 2024-04-26 04:30:00

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.

> Step 1: Install Laravel 10

If you haven't already, start by installing Laravel 10 by using Composer:

```
composer create-project laravel/laravel my-project "10.*"
``` 

> Step 2: Configure Log File

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.

> Step 3: Create a Slack App and Generate a Webhook URL

To send notifications to your Slack channel, you'll need to create a Slack app and generate a webhook URL. Follow these steps:

1. Go to your Slack workspace and navigate to the Apps section.
2. Click on Create New App, and give your app a name.
3. Navigate to the Incoming Webhooks section and toggle it on.
4. Click on Add New Webhook to Workspace and select the channel where you want to send notifications.
5. Copy the generated webhook URL.
6. Slack will now generate a webhook URL for us. Copy it, head over to your .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,
],
``` 

> Step 4: Test the Log Notification

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:

**[Read Also: How to Create REST API using Node.js and MongoDB](https://techsolutionstuff.com/post/how-to-create-rest-api-using-node-js-and-mongodb)**

**[Read Also: Toastr Notification In Laravel 10 Livewire Example](https://techsolutionstuff.com/post/toastr-notification-in-laravel-10-livewire-example)**