ホーム > Laravel > Output Error Logs to Slack with Laravel
Laravel

Output Error Logs to Slack with Laravel

Thank you for your continued support.
This article contains advertisements that help fund our operations.

⇨ Click here for the table of contents for Laravel articles

Let's see how to send error logs from Laravel to Slack.

Setting up to output logs to Slack makes it easy to receive notifications on your phone when an error occurs, so I recommend it.

Since it's mostly free, you can use it for personal development, and I think it's also used in small venture companies.

Environment

MacOS

Laravel6

Having a Slack workspace

Get SlackAPI Webhook

First, go to the SlackAPI page.

=> SlackAPI page

Login here.

If you don't have a workspace, please create one.

1 After logging in, click on "Your Apps" in the upper right.

2 Click on "Create New App" on the next page.

3 Enter the App Name and select the workspace.

You can give the App Name any name you want. You can write it in Japanese, so give it a clear and easy-to-manage name for yourself.

4 There is an item called Incomming Webhooks in the position in the image below, so click on it.

Image of SlackAPI management screen

There, Activate Incoming Webhooks is OFF and in the initial state, so turn it ON.

Various things are displayed.

Image of SlackAPI management screen 2

5 Click on Add New Webhook To Workspace.

An error occurred....

"It does not have bot users to install for (AppName)." This message appeared.

Image of SlackAPI management screen 3

It seems that this error occurs because the Bot needed to install Slack's feature is not in the workspace.

I rarely touch it, so I didn't remember this part.

6 Configure Features/AppHome "Your App’s Presence in Slack" in the left menu

A Bot has entered Slack's workspace.

Then, go back again and click on Incoming Webhooks => Add New Webhook To Workspace in the SlackAPI management screen.

Specify the channel to post to.

This time, I created a private channel called "Error Logs" in Slack's workspace and specified that.

Once you specify it, a WebhookURL is issued, and using it, you can send logs to that channel.

Implementation in Laravel

In Laravel, error logs are configured in config/logging.php.

In this file, you can specify error logs in the development environment or production environment.

This time, we will send error logs from the development environment to Slack.

First, write the WebhookURL prepared in the SlackAPI management page to the .env file.

In the .env file, there should be an item called LOG_CHANNEL on the 7th line, so change it and add it near there.

# .env
LOG_CHANNEL=slack
LOG_SLACK_WEBHOOK_URL=Copy and paste the WebhookURL prepared in the SlackAPI management page here. You don't need single quotes.

After editing the env file

php artisan config:clear

Run the command.

Then edit config/logging.php.

Change the slack item in channels to debug.

        'slack' => [
            'driver' => 'slack',
            'url' => env('LOG_SLACK_WEBHOOK_URL'),
            'username' => 'Laravel Log',
            'emoji' => ':boom:',
            'level' => 'debug',// Change this to debug
        ],

That's it!

Let's trigger an error.

Yes! An error has been sent!

Image of SlackAPI management screen 4

Configuring Laravel was super easy.

Customize Error Log Settings

During the process earlier, we set the level to debug for error logs to be output more easily, but this will result in an overwhelming number of error log messages.

Therefore, you can configure when error logs are output.

There are 8 levels, and as you go to the left, the error becomes more critical.

emergency, alert, critical, error, warning, notice, info, debug

Please try various options. The default setting is critical, but in the early stages of a service, having a lot of information is helpful, so I think warning or error is good (I don't know the specific differences in which errors will come, but I feel that error is just right).

That's it!

Since receiving error logs in Slack during development is quite annoying, let's change LOG_CHANNEL in .env to

LOG_CHANNEL=stack

Summary

How was it?

This time, I wrote about how to output error logs to Slack.

There are various places and methods to output error logs and notify them, but sending error logs to Slack is quite useful at the personal development level because it is the easiest and can be easily viewed on smartphones.

Using the method to get the Webhook this time, you can also send a chat to Slack at any time, so if you are interested, please check it out.

Please send complaints or point out mistakes in the article to my DM on Twitter.

Finally, here is a recommendation for a book for beginners.

Read more popular article "Implementing Email Address Verification in Laravel6 Auth (VerifyEmail)"

Please Provide Feedback
We would appreciate your feedback on this article. Feel free to leave a comment on any relevant YouTube video or reach out through the contact form. Thank you!