Package: slack-log-utils
slack_log_utilsA Python logging handler & formatter for Slack integration.How To InstallTo install slack-log-utils, simply:pipinstallslack-log-utilsor usingpipenv:pipenvinstallslack-log-utilsGetting StartedGet an Incoming Webhook URL onSlack.Instantiate manually:importloggingfromslack_log_utilsimportSlackWebhookFormatter,SlackWebhookHandlerformatter=SlackWebhookFormatter()handler=SlackWebhookHandler('https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX')handler.setFormatter(formatter)handler.setLevel(logging.WARNING)logger=logging.getLogger('sample_log')logger.addHandler(handler)Instantiate using thelogging.config.dictConfigmethod:importlogging.configfromslack_log_utilsimportSlackWebhookFormatter,SlackWebhookHandlerlogging_config={'version':1,'formatters':{'slack':{'()':SlackWebhookFormatter,},},'handlers':{'slack':{'()':SlackWebhookHandler,'formatter':'slack','level':logging.WARNING,'url':'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX',},},'loggers':{'sample_log':{'level':logging.DEBUG,'handlers':['slack'],},},}logging.config.dictConfig(logging_config)logger=logging.getLogger('sample_log')Read more about logging configuration in thePython docs.You can then use it just like any logger:logger.info('This will NOT post to Slack')# Since handler's level is set to WARNINGlogger.error('This will post to Slack')logger.error('[%s]: There are%serrors!','CRITICAL',8)Below is the logic contained withinSlackWebhookFormatteron how a logging.LogRecord is formatted:importjsonformatted_msg=super(SlackWebhookFormatter,self).format(record)color='#36a64f'\ifrecord.levelno<logging.WARNING\else'#ff0000'attachment={'author_name':record.name,'title':getattr(record,'title',''),'text':formatted_msg,'fallback':formatted_msg,'color':color,}returnjson.dumps({'attachments':[attachment]})First, it uses thelogging.Formatter's baseformat()method in order to get the %-style format string.
Then, the color is decided based on the record's level.  If it's less thanlogging.WARNING, it will be Green, else Red.
Theauthor_nameis set fromrecord.namewhich is the name of the logger used to log the call ('sample_log' in this case).
Thetitleis optionally set by using the key in theextradict argument for the call to the logger:logger.error('Sample Message Attachment!',extra={'title':'Sample Title'})An example of what this will look like can be seen in theSlack Message Builder.Please see thePython LogRecord docsfor the exact meaning ofrecord.levelno,record.name, and other attrtibutes.For further details on formatting messages, refer to the following Slack API pages:Message FormattingAttachmentsCurrently, all log messages will be sent as an attachment to Slack and it is only possible to send a single attachment at a time.
Future releases may make this more configurable.Release Notes0.1.5 (2018-05-19)Modified .travis.ymlAdded skip_existing flag to deployments. This will stop parallel builds from failing (seehere).Made it so all branches will be built, but keeping that only tagged commits on master branch will be deployed to PyPI.Updated README with badges from shields.io.0.1.4 (2018-05-19)Modified .travis.yml to fix issue with Travis-CI skipping deployment although the commit was tagged (seehere).0.1.3 (2018-05-19)Small code style change in slack_webhook_formatter.py.0.1.2 (2018-05-19)Added .travis.yml file for Travis-CI builds.0.1.1 (2018-05-19)Bumped version for README changes to show on PyPI.0.1.0 (2018-05-19)First release on PyPI.