Image created with Canva

How to Send PHP Symfony Application Logs to Slack Using Monolog

Serghei Pogor
5 min readApr 6, 2024

--

Let’s imagine we’ve set up our app to record all its mistakes in log files.

You might wonder…

Why bother sending these errors to Slack if we’ve already got them logged?

We developers often don’t check these logs unless there’s a reported issue. By that time, it’s usually too late — many users have encountered the error, which isn’t great for our app’s reputation.

That’s why I recommend a shift in strategy. Instead of relying solely on log files, let’s forward critical errors directly to Slack. This way, we’ll get notified the moment something serious goes wrong, allowing us to act swiftly to fix the issue.

First off, let’s get our tools ready. We’re going to use Monolog, a fantastic library that acts as a messenger for our app. It takes the errors our app encounters and sends them off to different places. Today, we want those messages to land in our Slack channel, where we can see them ASAP and jump into action.

Installing Monolog in Our Symfony App

Before our app can start chatting with us on Slack, we need to invite Monolog to the party. If you’re using Symfony, there’s a good chance Monolog is already hanging out waiting to be used. But just to be sure, let’s make it official by running:

composer require monolog/monolog

This command is like saying “Hey Composer, can you please get Monolog for our project?” Composer, being the loyal assistant it is, goes out and brings Monolog into our project.

Then we need to install this one too:

composer require symfony/slack-notifier

Guiding Monolog with a Token to Slack

Here’s how we set up our Symfony app to use this token and chat away in Slack channels.

  1. Get Your Slack Token: Head over to your Slack workspace and create a new app. Under the OAuth & Permissions section, you’ll find a Bot User OAuth Access Token. That’s your golden key. Keep it safe but accessible for the next steps. This is a simple guide how to get a token.
  2. Environment Setup: In your Symfony project, let’s be secretive (but smart) by storing our token and other details in environment variables. Open your .env file and add these lines:
###> symfony/slack-notifier ###
SLACK_TOKEN=xoxb-***
###> symfony/slack-notifier ###

3. Configuring Monolog to Use the Token: Now, let’s edit our Monolog configuration to ensure it knows how to use this token to send messages to Slack. Navigate to your config/packages/prod/monolog.yaml (or create it if it doesn’t exist) and paste the following configuration:

when@prod:
monolog:
handlers:
slack_error:
type: slack
token: '%env(SLACK_TOKEN)%'
channel: '%env(APP_ENV)%'
bot_name: '@@%env(APP_ENV)%'
icon_emoji: ':exclamation:'
level: error
include_extra: true

This snippet tells Monolog to use the Slack token for authentication, specifies which channel to post in (based on your environment), sets a bot name and an emoji for the messages, and ensures only errors (or worse) are reported.

So with this code, your message will be sent to #prod channel, you have to create this one. If you want to handle an error from the dev environment — its very easy, just create a new slack channel #dev and add the same config for dev like this

when@dev:
monolog:
handlers:
slack_error:
type: slack
token: '%env(SLACK_TOKEN)%'
channel: '%env(APP_ENV)%'
bot_name: '@@%env(APP_ENV)%'
icon_emoji: ':exclamation:'
level: error
include_extra: true

Customizing Error Severity Levels

In the configuration we discussed, we set the error level to error. This means any error at this level or higher will trigger a notification to your Slack channel. However, you might not want to be notified about every single error, especially if you're only interested in the most severe issues that require immediate attention.

To adjust this, you can change the level setting in your Monolog configuration. If you only want to be alerted about the most severe problems, you can switch from error to critical. Here's how you do it:

monolog:
handlers:
slack_error:
# Other configuration settings...
level: critical
# More settings...

By setting the level to critical, you're telling Monolog, "Hey, only knock on Slack's door if there's a big problem."

This way, you won't be bombarded with messages for every minor issue, but you'll still be in the loop when something major happens. It's all about finding the right balance for you and your team's workflow.

Here are some personal guidelines for effectively sending errors to Slack:

Limit Notifications to Critical Errors

It’s tempting to send every minor error or warning to Slack, but this can quickly lead to notification overload. Before you know it, you’re swamped with messages and start ignoring them, letting important alerts go unnoticed.

From personal experience, it’s better to adjust your settings so that you’re only notified about critical errors. Ideally, you should receive just a few error notifications daily, not hundreds or thousands.

Filter Out Common Non-Critical Errors

A typical example is when someone tries to access a route in your app that doesn’t exist. This triggers an error, but it’s not always useful to send these notifications to Slack.

You might wonder, “But isn’t it good to know about these nonexistent links users are trying to access?

Well, in most cases, regular users aren’t the ones hitting these dead ends — it’s usually bots and crawlers. While crawlers are generally harmless, bots might be probing for vulnerabilities to exploit.

Since bots can operate on multiple threads, they can generate a massive volume of errors in no time. To avoid a flood of notifications from such bot activity, it’s advisable not to send these particular error alerts to Slack.

Every mistake is a chance to learn something new. Every problem is a chance to get better. Let’s keep trying, keep learning, and remember: it’s not just about where we’re going, but also how we get there.

🔔 Click Subscribe to catch more coding fun.
👏🏻 Love it? Give a big clap.
💬 Got a cool idea or funny coding joke? Drop it in the comments.

Share these tips with your fellow developers to help each other succeed together.

Thanks for hanging out and reading. You rock! 🚀

Hold on a sec!!! Want more of my fun stuff in your inbox? Sign up here! 📩

--

--