Skip to content

Instantly share code, notes, and snippets.

Avatar

Juan Ignacio Sánchez Sampayo jiss2891

  • Montevideo - Uruguay
View GitHub Profile
View start_listener_slack.py
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
View dict_config.py
import queue
SLACK_QUEUE = queue.Queue()
LOGGING = {
...
'formatters': {...},
'handlers': {
...
'slack': {
View FinalLoggingConfExample.py
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
'style': '{'
}
},
'handlers': {
View ExampleUsage.py
import logging
logger = logging.getLogger(__name__)
logger.info("Some message")
logger.error("Some ERROR message")
View LoggersInLoggerConf.py
LOGGING = {
...
},
'loggers': {
'django': {
...
},
'my_app': {
'handlers': [
'console', # We want to see this logs on the console as well.
View HandlerInLoggingConf.py
LOGGING = {
...
'handlers': {
...
'slack': {
'class': 'mymodule.slack.SlackHandler',
'formatter': 'default'
},
...
},
View slack_handler_class.py
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.
View slack_interface_class.py
class SlackInterface:
def _send(self, payload):
requests.request(
method="POST",
url=settings.SLACK_ALERTS_URL,
data=payload,
headers={'content-type': 'application/json'}
)
View install_requests.sh
pip install requests
# or if you use Pipenv (Recommended)
pipenv install requests
View SlackURLExample.py
https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX