Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
slackclient-log-handler
Advanced tools
.. 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.
.. code-block:: bash
pip install slackclient-log-handler
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
FAQs
Posts log events to Slack via API
We found that slackclient-log-handler demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.