Last active
March 15, 2017 03:39
-
-
Save amane-katagiri/fba4fc4236cee478ab1ad20575f910c5 to your computer and use it in GitHub Desktop.
Slack handler for the logging package for Python.
This file contains 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
"""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