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.
↓ This is django-slack. http://django-slack.readthedocs.io/
Roughly speaking, the following two.
So, this time I will use it for the latter.
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
Just change the settings.
Added django_slack
to INSTALLED_APPS
INSTALLED_APPS = [
# ... some apps
'django_slack',
]
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'],
},
},
}
require_debug_false
to filters.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
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.
Django==1.11 django-slack==5.8.0
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