PKÐ}†H즃Íééslacker_log_handler/__init__.pyimport json import traceback from logging import Handler, CRITICAL, ERROR, WARNING from slacker import Slacker ERROR_COLOR = 'danger' # color name is built in to Slack API WARNING_COLOR = 'warning' # color name is built in to Slack API INFO_COLOR = '#439FE0' COLORS = { CRITICAL: ERROR_COLOR, ERROR: ERROR_COLOR, WARNING: WARNING_COLOR } class SlackerLogHandler(Handler): def __init__(self, api_key, channel, stack_trace=False, username='Python logger', icon_url=None, icon_emoji=None): Handler.__init__(self) self.slack_chat = Slacker(api_key) self.channel = channel self.stack_trace = stack_trace self.username = username self.icon_url = icon_url self.icon_emoji = icon_emoji if (icon_emoji or icon_url) else ':heavy_exclamation_mark:' if not self.channel.startswith('#'): self.channel = '#' + self.channel def emit(self, record): message = str(record.getMessage()) trace = { 'fallback': message, 'color': COLORS.get(self.level, INFO_COLOR) } if record.exc_info: trace['text'] = '\n'.join(traceback.format_exception(*record.exc_info)) attachments = [trace] self.slack_chat.chat.post_message( text=message, channel=self.channel, username=self.username, icon_url=self.icon_url, icon_emoji=self.icon_emoji, attachments=json.dumps(attachments) ) PKy…†HÉÆì3slacker_log_handler-1.2.0.dist-info/DESCRIPTION.rstslacker_log_handler ===================== .. image:: https://img.shields.io/pypi/v/slacker_log_handler.svg?style=flat-square :target: https://pypi.python.org/pypi/slacker_log_handler .. image:: https://img.shields.io/pypi/dm/slacker_log_handler.svg?style=flat-square :target: https://pypi.python.org/pypi/slacker_log_handler .. image:: https://img.shields.io/pypi/wheel/slacker_log_handler.svg?style=flat-square :target: https://pypi.python.org/pypi/slacker_log_handler .. image:: https://img.shields.io/pypi/format/slacker_log_handler.svg?style=flat-square :target: https://pypi.python.org/pypi/slacker_log_handler .. image:: https://img.shields.io/pypi/pyversions/slacker_log_handler.svg?style=flat-square :target: https://pypi.python.org/pypi/slacker_log_handler .. image:: https://img.shields.io/pypi/status/slacker_log_handler.svg?style=flat-square :target: https://pypi.python.org/pypi/slacker_log_handler Python log handler that posts to a Slack channel. Posts to the Slack API using https://github.com/os/slacker. For a different implementation using webhooks instead of Slacker, see https://github.com/claudetech/python-slack-log or read http://www.pythian.com/blog/logging-for-slackers/ Created with the intention of using for a Django project, but some effort has been made to make it generic enough that any Python project could use it. Installation ------------ .. code-block:: bash pip install slacker-log-handler Options ------- api_key (required) ~~~~~~~~~~~~~~~~~~~ Generate a key at https://api.slack.com/ channel (required) ~~~~~~~~~~~~~~~~~~ Set which channel you want to post to, e.g. "#general". username ~~~~~~~~ The username that will post to Slack. Defaults to "Python logger". icon_url ~~~~~~~~~ URL to an image to use as the icon for the logger user icon_emoji ~~~~~~~~~~~ emoji to use as the icon. Overrides icon_url. If neither icon_url nor icon_emoji is set, :heavy_exclamation_mark: will be used. Django configuration ------------------------------ - Add ``slacker_log_handler`` to ``INSTALLED_APPS`` - Set ``SLACK_API_KEY`` Sample Django logging configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Logging reference: https://docs.djangoproject.com/en/stable/topics/logging/ Sends INFO and ERRORS to Slack, as well as errors to admin emails. .. code-block:: python LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'filters': { 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse' } }, 'handlers': { 'mail_admins': { 'level': 'ERROR', 'filters': ['require_debug_false'], 'class': 'django.utils.log.AdminEmailHandler' }, 'slack-error': { 'level': 'ERROR', 'api_key': SLACK_API_KEY, 'class': 'slacker_log_handler.SlackerLogHandler', 'channel': '#general' }, 'slack-info': { 'level': 'INFO', 'api_key': SLACK_API_KEY, 'class': 'slacker_log_handler.SlackerLogHandler', 'channel': '#general' }, 'loggers': { 'django.request': { 'handlers': ['mail_admins', 'slack-error', 'slack-info'], 'level': 'ERROR', 'propagate': True, }, } } } License ------- Apache 2.0 Slacker is also under Apache 2.0. https://api.slack.com/terms-of-service PKy…†HX–êÂjj1slacker_log_handler-1.2.0.dist-info/metadata.json{"classifiers": ["Development Status :: 5 - Production/Stable", "Environment :: Web Environment", "Framework :: Django", "Intended Audience :: Developers", "Intended Audience :: System Administrators", "License :: OSI Approved :: Apache Software License", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 2", "Programming Language :: Python :: 3", "Topic :: Communications :: Chat", "Topic :: Office/Business :: Groupware"], "download_url": "https://github.com/mathiasose/slacker_log_handler/tarball/1.2.0", "extensions": {"python.details": {"contacts": [{"email": "mathias.ose@gmail.com", "name": "Mathias Ose", "role": "author"}], "document_names": {"description": "DESCRIPTION.rst"}, "project_urls": {"Home": "https://github.com/mathiasose/slacker_log_handler"}}}, "extras": [], "generator": "bdist_wheel (0.26.0)", "keywords": ["slack", "logging"], "metadata_version": "2.0", "name": "slacker-log-handler", "run_requires": [{"requires": ["slacker (>=0.7.3,<=0.9.9)"]}], "summary": "Posts log events to Slack via API", "version": "1.2.0"}PKy…†HÚŸÒ91slacker_log_handler-1.2.0.dist-info/top_level.txtslacker_log_handler PKy…†Hìndªnn)slacker_log_handler-1.2.0.dist-info/WHEELWheel-Version: 1.0 Generator: bdist_wheel (0.26.0) Root-Is-Purelib: true Tag: py2-none-any Tag: py3-none-any PKy…†Heœ),slacker_log_handler-1.2.0.dist-info/METADATAMetadata-Version: 2.0 Name: slacker-log-handler Version: 1.2.0 Summary: Posts log events to Slack via API Home-page: https://github.com/mathiasose/slacker_log_handler Author: Mathias Ose Author-email: mathias.ose@gmail.com License: UNKNOWN Download-URL: https://github.com/mathiasose/slacker_log_handler/tarball/1.2.0 Keywords: slack,logging Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Environment :: Web Environment Classifier: Framework :: Django Classifier: Intended Audience :: Developers Classifier: Intended Audience :: System Administrators Classifier: License :: OSI Approved :: Apache Software License Classifier: Natural Language :: English Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 2 Classifier: Programming Language :: Python :: 3 Classifier: Topic :: Communications :: Chat Classifier: Topic :: Office/Business :: Groupware Requires-Dist: slacker (>=0.7.3,<=0.9.9) slacker_log_handler ===================== .. image:: https://img.shields.io/pypi/v/slacker_log_handler.svg?style=flat-square :target: https://pypi.python.org/pypi/slacker_log_handler .. image:: https://img.shields.io/pypi/dm/slacker_log_handler.svg?style=flat-square :target: https://pypi.python.org/pypi/slacker_log_handler .. image:: https://img.shields.io/pypi/wheel/slacker_log_handler.svg?style=flat-square :target: https://pypi.python.org/pypi/slacker_log_handler .. image:: https://img.shields.io/pypi/format/slacker_log_handler.svg?style=flat-square :target: https://pypi.python.org/pypi/slacker_log_handler .. image:: https://img.shields.io/pypi/pyversions/slacker_log_handler.svg?style=flat-square :target: https://pypi.python.org/pypi/slacker_log_handler .. image:: https://img.shields.io/pypi/status/slacker_log_handler.svg?style=flat-square :target: https://pypi.python.org/pypi/slacker_log_handler Python log handler that posts to a Slack channel. Posts to the Slack API using https://github.com/os/slacker. For a different implementation using webhooks instead of Slacker, see https://github.com/claudetech/python-slack-log or read http://www.pythian.com/blog/logging-for-slackers/ Created with the intention of using for a Django project, but some effort has been made to make it generic enough that any Python project could use it. Installation ------------ .. code-block:: bash pip install slacker-log-handler Options ------- api_key (required) ~~~~~~~~~~~~~~~~~~~ Generate a key at https://api.slack.com/ channel (required) ~~~~~~~~~~~~~~~~~~ Set which channel you want to post to, e.g. "#general". username ~~~~~~~~ The username that will post to Slack. Defaults to "Python logger". icon_url ~~~~~~~~~ URL to an image to use as the icon for the logger user icon_emoji ~~~~~~~~~~~ emoji to use as the icon. Overrides icon_url. If neither icon_url nor icon_emoji is set, :heavy_exclamation_mark: will be used. Django configuration ------------------------------ - Add ``slacker_log_handler`` to ``INSTALLED_APPS`` - Set ``SLACK_API_KEY`` Sample Django logging configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Logging reference: https://docs.djangoproject.com/en/stable/topics/logging/ Sends INFO and ERRORS to Slack, as well as errors to admin emails. .. code-block:: python LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'filters': { 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse' } }, 'handlers': { 'mail_admins': { 'level': 'ERROR', 'filters': ['require_debug_false'], 'class': 'django.utils.log.AdminEmailHandler' }, 'slack-error': { 'level': 'ERROR', 'api_key': SLACK_API_KEY, 'class': 'slacker_log_handler.SlackerLogHandler', 'channel': '#general' }, 'slack-info': { 'level': 'INFO', 'api_key': SLACK_API_KEY, 'class': 'slacker_log_handler.SlackerLogHandler', 'channel': '#general' }, 'loggers': { 'django.request': { 'handlers': ['mail_admins', 'slack-error', 'slack-info'], 'level': 'ERROR', 'propagate': True, }, } } } License ------- Apache 2.0 Slacker is also under Apache 2.0. https://api.slack.com/terms-of-service PKy…†Hˆ@­(*slacker_log_handler-1.2.0.dist-info/RECORDslacker_log_handler/__init__.py,sha256=hkweYmOCySrj3aVrOf5J0jClI9SAtSAWk607ilWNZpA,1513 slacker_log_handler-1.2.0.dist-info/DESCRIPTION.rst,sha256=YtXJ9A1cjCJ4Pj9GT79n58of-K4KvYRVu3XZ6ekwy48,3608 slacker_log_handler-1.2.0.dist-info/METADATA,sha256=PUPHe2hGAn-a6ncgMXUvjcU8AYu1ikEtZUrF7k0mxCI,4623 slacker_log_handler-1.2.0.dist-info/RECORD,, slacker_log_handler-1.2.0.dist-info/WHEEL,sha256=GrqQvamwgBV4nLoJe0vhYRSWzWsx7xjlt74FT0SWYfE,110 slacker_log_handler-1.2.0.dist-info/metadata.json,sha256=kOx_RQKYv9dGt6T-uVQi-sFOmxjE3effk6cUkKE1wMY,1130 slacker_log_handler-1.2.0.dist-info/top_level.txt,sha256=dJg8khpoByCRmoo2u2q24MLJv7NSwFZHGsTpsax9VbU,20 PKÐ}†H즃Íééslacker_log_handler/__init__.pyPKy…†HÉÆì3&slacker_log_handler-1.2.0.dist-info/DESCRIPTION.rstPKy…†HX–êÂjj1slacker_log_handler-1.2.0.dist-info/metadata.jsonPKy…†HÚŸÒ91Hslacker_log_handler-1.2.0.dist-info/top_level.txtPKy…†Hìndªnn)«slacker_log_handler-1.2.0.dist-info/WHEELPKy…†Heœ),`slacker_log_handler-1.2.0.dist-info/METADATAPKy…†Hˆ@­(*¹,slacker_log_handler-1.2.0.dist-info/RECORDPKu‘/