Package: slackclient-log-handler
Python log handler that posts to a Slack channel. Posts to the Slack API
usinghttps://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.Installationpipinstallslackclient-log-handlerOptionsapi_token (required)Generate a key athttps://api.slack.com/channel (required)Set which channel you want to post to, e.g. “#general”.usernameThe username that will post to Slack. Defaults to “Python logger”.icon_urlURL to an image to use as the icon for the logger usericon_emojiemoji to use as the icon. Overrides icon_url. If neither icon_url nor
icon_emoji is set, :heavy_exclamation_mark: will be used.fail_silentDefaults 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 handlerthat will also handle the same log events,
or they may be lost entirely.Django configurationLogging 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.SetSLACK_API_KEYin 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_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 handlerThis is how you useslackclient_log_handleras a regular Python logging handler.
This example will send a error message to a slack channel.importloggingfromslackclient_log_handlerimportSlackclientLogHandler,NoStacktraceFormatter# Create slack handlerslack_handler=SlackclientLogHandler('my-channel-token','my-channel-name')# Create loggerlogger=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 Slackslack_handler.setLevel(logging.DEBUG)# Test logginglogger.error("Debug message from slack!")Slack message formattingThis example use a subclass that will send a formatted message to a slack channel.
Reference:https://api.slack.com/docs/message-formattingclassCustomLogHandler(SlackclientLogHandler):defbuild_msg(self,record):message="> New message :\n"+record.getMessage()returnmessageLicenseApache 2.0Slack-sdk is also under MIT.See also:https://api.slack.com/terms-of-service