Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Slack handler for the logging package for Python.
"""Slack handler for the logging package for Python."""
import json
from logging import DEBUG
from logging import Formatter
from logging import getLogger
from logging.handlers import HTTPHandler
class SlackHandler(HTTPHandler):
"""Logging handler which sends records to your Slack team."""
def mapLogRecord(self, record):
"""Set POST parameters.
POST payload: quote('payload={"text": "MESSAGE"}')
"""
return {"payload": json.dumps({"text": self.format(record)})}
formatter = Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logger = getLogger(__name__)
handler = SlackHandler("hooks.slack.com", "/services/HOGE/FUGA/PIYO", "POST", secure=True)
handler.setLevel(DEBUG)
handler.setFormatter(formatter)
logger.setLevel(DEBUG)
logger.addHandler(handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment