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
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.
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.
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,
},
}
}
}
Example Python logging handler
This is how you use slacker_log_handler as a regular Python logging handler. This example will send a error message to a slack channel.
import logging
from slacker_log_handler import SlackerLogHandler, NoStacktraceFormatter
# Create slack handler
slack_handler = SlackerLogHandler('my-channel-token', 'my-channel-name', stack_trace=True)
# 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
class CustomLogHandler(SlackerLogHandler):
def build_msg(self, record):
message = "> New message :\n" + record.getMessage()
return message
License
Apache 2.0
Slacker is also under Apache 2.0.
See also: https://api.slack.com/terms-of-service
Metadata
Release files for slacker-log-handler 1.8.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| slacker_log_handler-1.8.0.tar.gz | 9.4 kB | Without provenance | Details |
Release files / slacker_log_handler-1.8.0.tar.gz
| Download URL | slacker_log_handler-1.8.0.tar.gz |
|---|---|
| Size | 9.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3fa3ed772fa232c1e5861e18f853ba6ade02525c2a0f801f31a53be99f0ac6b4
|
|
BLAKE2b-256 checksum How to use checksums |
c485ff8160039d217d5fe173be86f2a0168026895382f9ff68959618b5d8d7c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1
|
Security for slacker-log-handler 1.8.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.