Logo

dev-resources.site

for different kinds of informations.

Laravel 10 Send Notifications in Slack Channel

Published at
4/26/2024
Categories
laravel
laravel10
notification
slack
Author
techsolutionstuff
Author
17 person written this
techsolutionstuff
open
Laravel 10 Send Notifications in Slack Channel

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.*"
Enter fullscreen mode Exit fullscreen mode

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'),
        ],
    ],

];
Enter fullscreen mode Exit fullscreen mode

In the log file, there are different options.

'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => env('LOG_LEVEL', 'debug'),
            'days' => 14,
        ],
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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,
],
Enter fullscreen mode Exit fullscreen mode

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");
Enter fullscreen mode Exit fullscreen mode

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

Read Also: Toastr Notification In Laravel 10 Livewire Example

slack Article's
30 articles in total
Favicon
Open-Source Collaboration Progress
Favicon
Collaborating to Slack as an Open-Source Developer: Part 2
Favicon
Collaborating to Slack as an Open-Source Developer
Favicon
How to integrate Alerta into your Business
Favicon
[Apache Superset] Topic #5, Automated Alerts and Reporting via Slack/Email in Superset
Favicon
How to send a message to Slack workspace members using an SQL statement
Favicon
Enable Slack Notifications for Amplify Builds Directly to a Slack Channel
Favicon
How to use Slack for Beginners.
Favicon
How to make Slack Workflow input form
Favicon
Magento 2 Slack Notifier Module
Favicon
We Built our First SaaS - Sweet Kudos
Favicon
How to Customize Slack Fonts Using Stylus for Improved Readability
Favicon
10 Slack emojis that developers should use
Favicon
Format your Slack messages, please!
Favicon
ChatGPT Slack Bot
Favicon
Effortless Bug Tracking: Pcloudy’s Integration with Asana and Monday.com
Favicon
AI-Powered News Summaries for Slack
Favicon
Enhancing Decision-Making: AI In Microsoft Teams & Slack
Favicon
Slack vs. Discord: Choosing Your Tech Community's Playground
Favicon
Laravel 10 Send Notifications in Slack Channel
Favicon
Build a Slack app with SlackKit
Favicon
Automatically create Google Tasks from Slack/Cisco Webex Teams keywords with webMethods.io Integration
Favicon
How can you become an AWS Community Builder?
Favicon
Automate PagerDuty alerts in Slack/Cisco Webex Teams with webMethods.io Integration
Favicon
I developed a no-code tool that integrates Anthropic into Slack, Teams, and HubSpot
Favicon
"Community Communication: Slack vs. Discord - Deciding the Ideal Platform"
Favicon
Easier TypeScript API Testing with Vitest + MSW
Favicon
Further Tips on Slack's files.upload Deprecation
Favicon
New on Zeeve: Slack & Microsoft Teams Integration for Streamlined Deployment Alerts
Favicon
Rapid Bolt app development w/ Slack CLI and sandboxes

Featured ones: