Skip to content

Commit 0621187

Browse files
authored
Merge pull request #2 from browniebroke/patch-1
Add SlackLogFilter fixes #1
2 parents 1d8e380 + 33158ca commit 0621187

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

README.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,18 @@ Using logger
4747
logger.warn('warn message')
4848
logger.error('error message')
4949
logger.critical('critical message')
50+
51+
Using filter
52+
''''''''''''
53+
54+
You can also filter some messages only
55+
56+
.. code-block:: python
57+
58+
from slack_logger import SlackLogFilter
59+
60+
sf = SlackLogFilter()
61+
sh.addFilter(sf)
62+
63+
logger.info('info message') # Not posted to slack
64+
logger.info('info message to slack', extra={'notify_slack': True}) # Posted to slack

slack_logger.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,15 @@ def format(self, record):
5858
ret['ts'] = record.created
5959
ret['text'] = super(SlackFormatter, self).format(record)
6060
return ret
61+
62+
63+
class SlackLogFilter(logging.Filter):
64+
"""
65+
Logging filter to decide when logging to Slack is requested, using
66+
the `extra` kwargs:
67+
68+
`logger.info("...", extra={'notify_slack': True})`
69+
"""
70+
71+
def filter(self, record):
72+
return getattr(record, 'notify_slack', False)

0 commit comments

Comments
 (0)