[PYTHON] I tried django-slack

I wondered if there was a handy library to notify slack when an error occurred in Django, so I tried using it. Eventually, I'm planning to aggregate the logs with Fluentd and notify them, but I'm currently developing it, but if I can implement it quickly in the situation where it is open to the company, I feel like this is the way to go for the time being. Is the introduction of.

What is django-slack?

↓ This is django-slack. http://django-slack.readthedocs.io/

What can i do

Roughly speaking, the following two.

  1. You can post a message to slack with slack_message ().
    http://django-slack.readthedocs.io/#richly-formatted-messages
  2. Post a message to slack when an Exception occurs.
    http://django-slack.readthedocs.io/#logging-exceptions-to-slack

So, this time I will use it for the latter.

Preparation

Get a token to post to slack. Whether you use test tokens or set bots properly is up to you. https://api.slack.com/custom-integrations/legacy-tokens https://slack.com/apps/A0F7YS25R-bots

install pip install django-slack

Implementation

Just change the settings.

Addition of INSTALLED_APPS

Added django_slack to INSTALLED_APPS

INSTALLED_APPS = [
    # ... some apps
    'django_slack',
]

logger settings

Add SlackExceptionHandler to django logger handlers.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse',
        },
    },
    'handlers': {
        'slack_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django_slack.log.SlackExceptionHandler',
        },
    },
    'loggers': {
        'django': {
            'level': 'ERROR',
            'handlers': ['slack_admins'],
        },
    },
}

Setting options

Specify token as an option.

SLACK_TOKEN = 'SOME_TOKEN'
SLACK_CHANNEL = '#PUBLIC_CHANNEL'
SLACK_USERNAME = 'USER-NAME'
SLACK_ICON_EMOJI = ':bangbang:'
SLACK_FAIL_SILENTLY = True

If you set SLACK_FAIL_SILENTLY to False and you make a mistake in specifying the channel and an error occurs in SlackExceptionHandler, "Error occurred"-> "Notify slack"-> "Error occurred"-> "Notify slack" It falls into an infinite loop.

See below for other options. http://django-slack.readthedocs.io/#configuration

Communication confirmation

Execute the following with python manage.py shell.

from django_slack import slack_message
slack_message('django_slack/exception.slack', {'text': 'hoge'})

If'hoge'is posted on the specified channel, it is successful. By the way, add SLACK_BACKEND ='django_slack.backends.UrllibBackend' to check communication in the environment of DEBUG = True. If the communication check fails, set SLACK_FAIL_SILENTLY to True and an error will be displayed.

version

Django==1.11 django-slack==5.8.0

Impressions

I like how easy it is to use. However, if you set SLACK_FAIL_SILENTLY to True, it will be really silent, but at least I want you to spit log with logger ('django_slack'). https://github.com/lamby/django-slack/blob/24ce48de96f561518e81aa3adef89c4f5f287707/django_slack/api.py#L132

Basically, I don't think it will be used in the production environment, but if you use it in the production environment, it seems that there is also a Celery Backend, so it seems better to do asynchronous processing there.

that's all!

Recommended Posts

I tried django-slack
I tried scraping
I tried PyQ
I tried papermill
I tried Django
I tried spleeter
I tried cgo
I tried using parameterized
I tried using argparse
I tried using mimesis
I tried using anytree
I tried competitive programming
I tried running pymc
I tried ARP spoofing
I tried using Summpy
I tried Python> autopep8
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using ESPCN
I tried using openpyxl
I tried deep learning
I tried AWS CDK!
I tried using Ipython
I tried to debug.
I tried using PyCaret
I tried using cron
I tried using ngrok
I tried using face_recognition
I tried to paste
I tried using Jupyter
I tried using PyCaret
I tried moving EfficientDet
I tried using Heapq
I tried Python> decorator
I tried running TensorFlow
I tried Auto Gluon
I tried using folium
I tried using jinja2
I tried AWS Iot
I tried Bayesian optimization!
I tried using folium
I tried using time-window
I tried Value Iteration Networks
I tried scraping with Python
I tried AutoGluon's Image Classification
I tried to learn PredNet
[I tried using Pythonista 3] Introduction
I tried using easydict (memo).
I tried face recognition using Face ++
I tried using Random Forest
I tried clustering with PyCaret
I tried using BigQuery ML
I tried "K-Fold Target Encoding"
I tried to implement PCANet
[I tried] Nand2 Tetris Chapter 6
I tried the changefinder library!
I tried Python C extension
[Python] I tried using OpenPose