slackclient_log_handler
========================
.. image:: https://img.shields.io/pypi/v/slacker_log_handler.svg?style=flat-square
:target: https://pypi.python.org/pypi/slackclient_log_handler
.. image:: https://img.shields.io/pypi/wheel/slacker_log_handler.svg?style=flat-square
:target: https://pypi.python.org/pypi/slackclient_log_handler
.. image:: https://img.shields.io/pypi/format/slacker_log_handler.svg?style=flat-square
:target: https://pypi.python.org/pypi/slackclient_log_handler
.. image:: https://img.shields.io/pypi/pyversions/slacker_log_handler.svg?style=flat-square
:target: https://pypi.python.org/pypi/slackclient_log_handler
.. image:: https://img.shields.io/pypi/status/slacker_log_handler.svg?style=flat-square
:target: https://pypi.python.org/pypi/slackclient_log_handler
Python log handler that posts to a Slack channel. Posts to the Slack API
using https://slack.dev/python-slack-sdk.
Created with the intention of using for a Internal 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 slackclient-log-handler
Options
-------
api_token (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.
fail_silent
~~~~~~~~~~~
Defaults to False.
If your API key is invalid or for some other reason the API call returns an error,
this option will silently ignore the API error.
If you enable this setting, **make sure you have another log handler** that will also handle the same log events,
or they may be lost entirely.
Django configuration
--------------------
Logging reference: https://docs.djangoproject.com/en/stable/topics/logging/
This example will send INFO and ERRORS to Slack, as well as errors to admin emails.
- Set ``SLACK_API_KEY`` in your settings module.
.. 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_token': SLACK_API_KEY,
'class': 'slackclient_log_handler.SlackclientLogHandler',
'channel': '#general'
},
'slack-info': {
'level': 'INFO',
'api_token': SLACK_API_KEY,
'class': 'slackclient_log_handler.SlackclientLogHandler',
'channel': '#general'
},
'loggers': {
'django.request': {
'handlers': ['mail_admins', 'slack-error', 'slack-info'],
'level': 'ERROR',
'propagate': True,
},
}
}
}
Example Python logging handler
------------------------------
This is how you use `slackclient_log_handler` as a regular Python logging handler.
This example will send a error message to a slack channel.
.. code-block:: python
import logging
from slackclient_log_handler import SlackclientLogHandler, NoStacktraceFormatter
# Create slack handler
slack_handler = SlackclientLogHandler('my-channel-token', 'my-channel-name')
# Create logger
logger = logging.getLogger('debug_application')
logger.addHandler(slack_handler)
# OPTIONAL: Define a log message formatter.
# If you have set stack_trace=True, any exception stack traces will be included as Slack message attachments.
# You therefore need to use NoStacktraceFormatter as a base to exclude the trace from the main message text.
formatter = NoStacktraceFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
slack_handler.setFormatter(formatter)
# Define the minimum level of log messages you want to send to Slack
slack_handler.setLevel(logging.DEBUG)
# Test logging
logger.error("Debug message from slack!")
Slack message formatting
------------------------
This example use a subclass that will send a formatted message to a slack channel.
Reference: https://api.slack.com/docs/message-formatting
.. code-block:: python
class CustomLogHandler(SlackclientLogHandler):
def build_msg(self, record):
message = "> New message :\n" + record.getMessage()
return message
License
-------
Apache 2.0
Slack-sdk is also under MIT.
See also: https://api.slack.com/terms-of-service
Raw data
{
"_id": null,
"home_page": "https://github.com/search5/slackclient_log_handler",
"name": "slackclient-log-handler",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.9,<4.0",
"maintainer_email": "",
"keywords": "slack,logging",
"author": "Ji-Ho Lee",
"author_email": "search5@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/b8/40/1f12bb28bdfc726cb8af283f7d51c029ff60ff8265b1b17763099db8d9eb/slackclient_log_handler-0.1.2.tar.gz",
"platform": null,
"description": "slackclient_log_handler\n========================\n\n.. image:: https://img.shields.io/pypi/v/slacker_log_handler.svg?style=flat-square\n :target: https://pypi.python.org/pypi/slackclient_log_handler\n\n.. image:: https://img.shields.io/pypi/wheel/slacker_log_handler.svg?style=flat-square\n :target: https://pypi.python.org/pypi/slackclient_log_handler\n\n.. image:: https://img.shields.io/pypi/format/slacker_log_handler.svg?style=flat-square\n :target: https://pypi.python.org/pypi/slackclient_log_handler\n\n.. image:: https://img.shields.io/pypi/pyversions/slacker_log_handler.svg?style=flat-square\n :target: https://pypi.python.org/pypi/slackclient_log_handler\n\n.. image:: https://img.shields.io/pypi/status/slacker_log_handler.svg?style=flat-square\n :target: https://pypi.python.org/pypi/slackclient_log_handler\n\nPython log handler that posts to a Slack channel. Posts to the Slack API\nusing https://slack.dev/python-slack-sdk.\n\nCreated with the intention of using for a Internal project, but some\neffort has been made to make it generic enough that any Python project\ncould use it.\n\nInstallation\n------------\n\n.. code-block:: bash\n\n pip install slackclient-log-handler\n\nOptions\n-------\n\napi_token (required)\n~~~~~~~~~~~~~~~~~~~~~\n\nGenerate a key at https://api.slack.com/\n\nchannel (required)\n~~~~~~~~~~~~~~~~~~\n\nSet which channel you want to post to, e.g. \"#general\".\n\nusername\n~~~~~~~~\n\nThe username that will post to Slack. Defaults to \"Python logger\".\n\nicon_url\n~~~~~~~~\n\nURL to an image to use as the icon for the logger user\n\nicon_emoji\n~~~~~~~~~~\n\nemoji to use as the icon. Overrides icon_url. If neither icon_url nor\nicon_emoji is set, :heavy_exclamation_mark: will be used.\n\nfail_silent\n~~~~~~~~~~~\nDefaults to False.\nIf your API key is invalid or for some other reason the API call returns an error,\nthis option will silently ignore the API error.\nIf you enable this setting, **make sure you have another log handler** that will also handle the same log events,\nor they may be lost entirely.\n\n\nDjango configuration\n--------------------\nLogging reference: https://docs.djangoproject.com/en/stable/topics/logging/\n\nThis example will send INFO and ERRORS to Slack, as well as errors to admin emails.\n\n- Set ``SLACK_API_KEY`` in your settings module.\n\n.. code-block:: python\n\n LOGGING = {\n 'version': 1,\n 'disable_existing_loggers': False,\n 'filters': {\n 'require_debug_false': {\n '()': 'django.utils.log.RequireDebugFalse'\n }\n },\n 'handlers': {\n 'mail_admins': {\n 'level': 'ERROR',\n 'filters': ['require_debug_false'],\n 'class': 'django.utils.log.AdminEmailHandler'\n },\n 'slack-error': {\n 'level': 'ERROR',\n 'api_token': SLACK_API_KEY,\n 'class': 'slackclient_log_handler.SlackclientLogHandler',\n 'channel': '#general'\n },\n 'slack-info': {\n 'level': 'INFO',\n 'api_token': SLACK_API_KEY,\n 'class': 'slackclient_log_handler.SlackclientLogHandler',\n 'channel': '#general'\n },\n 'loggers': {\n 'django.request': {\n 'handlers': ['mail_admins', 'slack-error', 'slack-info'],\n 'level': 'ERROR',\n 'propagate': True,\n },\n }\n }\n }\n\nExample Python logging handler\n------------------------------\n\nThis is how you use `slackclient_log_handler` as a regular Python logging handler.\nThis example will send a error message to a slack channel.\n\n.. code-block:: python\n\n import logging\n from slackclient_log_handler import SlackclientLogHandler, NoStacktraceFormatter\n\n # Create slack handler\n slack_handler = SlackclientLogHandler('my-channel-token', 'my-channel-name')\n\n # Create logger\n logger = logging.getLogger('debug_application')\n logger.addHandler(slack_handler)\n\n # OPTIONAL: Define a log message formatter.\n # If you have set stack_trace=True, any exception stack traces will be included as Slack message attachments.\n # You therefore need to use NoStacktraceFormatter as a base to exclude the trace from the main message text.\n formatter = NoStacktraceFormatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n slack_handler.setFormatter(formatter)\n\n # Define the minimum level of log messages you want to send to Slack\n slack_handler.setLevel(logging.DEBUG)\n\n # Test logging\n logger.error(\"Debug message from slack!\")\n\nSlack message formatting\n------------------------\n\nThis example use a subclass that will send a formatted message to a slack channel.\nReference: https://api.slack.com/docs/message-formatting\n\n.. code-block:: python\n\n class CustomLogHandler(SlackclientLogHandler):\n def build_msg(self, record):\n message = \"> New message :\\n\" + record.getMessage()\n return message\n\nLicense\n-------\n\nApache 2.0\n\nSlack-sdk is also under MIT.\n\nSee also: https://api.slack.com/terms-of-service\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Posts log events to Slack via API",
"version": "0.1.2",
"project_urls": {
"Homepage": "https://github.com/search5/slackclient_log_handler",
"Repository": "https://github.com/search5/slackclient_log_handler"
},
"split_keywords": [
"slack",
"logging"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "aafbca8f3a454da63cbd2ae8d33c91aba44fef4ffd08d0c3430117c9ede7025c",
"md5": "33446875ce144d503e9b3e1515273904",
"sha256": "e464d40e3c7d95956cee23234e0ee2bd9b30b7fd94f45221564402fc1e5cd3a7"
},
"downloads": -1,
"filename": "slackclient_log_handler-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "33446875ce144d503e9b3e1515273904",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9,<4.0",
"size": 8195,
"upload_time": "2023-12-28T22:40:15",
"upload_time_iso_8601": "2023-12-28T22:40:15.110138Z",
"url": "https://files.pythonhosted.org/packages/aa/fb/ca8f3a454da63cbd2ae8d33c91aba44fef4ffd08d0c3430117c9ede7025c/slackclient_log_handler-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b8401f12bb28bdfc726cb8af283f7d51c029ff60ff8265b1b17763099db8d9eb",
"md5": "a8c52a8945d12994eed155fb2d8b4a52",
"sha256": "72b8fa1f64992e43b5bfe2d60ce1b6b232f41d54232dd5df104b605abea6ad4a"
},
"downloads": -1,
"filename": "slackclient_log_handler-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "a8c52a8945d12994eed155fb2d8b4a52",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9,<4.0",
"size": 8390,
"upload_time": "2023-12-28T22:40:16",
"upload_time_iso_8601": "2023-12-28T22:40:16.898089Z",
"url": "https://files.pythonhosted.org/packages/b8/40/1f12bb28bdfc726cb8af283f7d51c029ff60ff8265b1b17763099db8d9eb/slackclient_log_handler-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-28 22:40:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "search5",
"github_project": "slackclient_log_handler",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "slackclient-log-handler"
}