-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.py
42 lines (28 loc) · 932 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import logging
class MySpecialException(Exception):
"""We won't log this!"""
class SpecialExceptionFilter(logging.Filter):
def filter(self, record):
# Ask the question; "Should I log this?"
if record.exc_info is not None:
exc_type, value, tb = record.exc_info
if exc_type is MySpecialException:
return False
return True
def main():
exc_filter = SpecialExceptionFilter()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
logger.addFilter(exc_filter)
logger.info('Hey, we have some special filters.')
try:
raise Exception()
except Exception:
logger.exception('This will be logged!')
try:
raise MySpecialException()
except MySpecialException:
logger.exception('This will not!!')
if __name__ == '__main__':
main()