This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from logging.handlers import QueueListener | |
| from django.conf import settings | |
| def start_queue_listener(): | |
| alerts_queue_listener = QueueListener( | |
| settings.SLACK_QUEUE, | |
| SlackHandler(), # Notice that this needs to be an instance. | |
| respect_handler_level=True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import queue | |
| SLACK_QUEUE = queue.Queue() | |
| LOGGING = { | |
| ... | |
| 'formatters': {...}, | |
| 'handlers': { | |
| ... | |
| 'slack': { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| LOGGING = { | |
| 'version': 1, | |
| 'disable_existing_loggers': False, | |
| 'formatters': { | |
| 'default': { | |
| 'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}', | |
| 'style': '{' | |
| } | |
| }, | |
| 'handlers': { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import logging | |
| logger = logging.getLogger(__name__) | |
| logger.info("Some message") | |
| logger.error("Some ERROR message") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| LOGGING = { | |
| ... | |
| }, | |
| 'loggers': { | |
| 'django': { | |
| ... | |
| }, | |
| 'my_app': { | |
| 'handlers': [ | |
| 'console', # We want to see this logs on the console as well. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| LOGGING = { | |
| ... | |
| 'handlers': { | |
| ... | |
| 'slack': { | |
| 'class': 'mymodule.slack.SlackHandler', | |
| 'formatter': 'default' | |
| }, | |
| ... | |
| }, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ... | |
| class SlackInterface(...): | |
| ... | |
| class SlackHandler(logging.Handler): | |
| def emit(self, record): | |
| if not settings.SLACK_ALERTS_URL: | |
| # Don't try to notify in case we do not have Slack's api configured. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SlackInterface: | |
| def _send(self, payload): | |
| requests.request( | |
| method="POST", | |
| url=settings.SLACK_ALERTS_URL, | |
| data=payload, | |
| headers={'content-type': 'application/json'} | |
| ) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pip install requests | |
| # or if you use Pipenv (Recommended) | |
| pipenv install requests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX |