DEV Community

Oras Al-Kubaisi
Oras Al-Kubaisi

Posted on

Send Monolog critical logs directly to Slack!

This post was originally posted in my blog PHP Symfony Tips & Tricks

By using Monolog in Symfony applications, we can log to files, send emails, log to console and many other channels. In this article, I will show the step by step on how to integrate Monolog with Slack.
The good thing is integration is just a matter of some configuration in config without writing any line of code. So it could be easily integrated into any project.

Steps:
1) Create a slack channel.


2) Go to https://api.slack.com/web scroll down to Authentication and create authentication token.

3) In your symfony app, go to config_prod.yml and add the following config in monolog section.

   slack:
     type: slack
     token: slack api token
     channel: channel name
     [bot_name]: defaults to Monolog
     [icon_emoji]: defaults to null
     [use_attachment]: bool, defaults to true
     [use_short_attachment]: bool, defaults to false
     [include_extra]: bool, defaults to false
     [level]: level name or int value, defaults to DEBUG
     [bubble]: bool, defaults to true
Enter fullscreen mode Exit fullscreen mode

Please note that debug will flood your channel with lots of logs, so use critical

channel should start with # just like how it does appear on Slack.


Full configuration example:

monolog:
    handlers:
        main:
            type:         fingers_crossed
            action_level: error
            handler:      nested
        nested:
            type:  stream
            path:  "%kernel.logs_dir%/%kernel.environment%.log"
            level: debug
        console:
            type:  console
        slack:
            type:       slack
            token:       xxxx-xxxxxxxxxxx-xxxxxxxxx-xxxxxxxxxx-xxxxxx
            channel:     "#name-of-channel"
            bot_name:    ChooseName
            icon_emoji: :ghost:
            level:      critical
Enter fullscreen mode Exit fullscreen mode

icon_emoji you can use any emoji from Slack. Checkout the full list on this page:

EMOJI CHEAT SHEET

Further reading about Monolog, how does it work .. checkout the following links:

Top comments (1)

Collapse
 
egorzot profile image
Egor

Thanks!