Total Complexity | 6 |
Total Lines | 28 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import logging |
||
2 | import os |
||
3 | import sys |
||
4 | |||
5 | |||
6 | class RelpathFormatFilter(logging.Filter): |
||
7 | """Adds '%(relpath)s' as a 'LogRecord' attribute.""" |
||
8 | |||
9 | def filter(self, record): |
||
10 | pathname = record.pathname |
||
11 | record.relpath = None |
||
12 | abs_sys_paths = [os.path.abspath(p) for p in sys.path] |
||
13 | for path in sorted(abs_sys_paths, key=len, reverse=True): |
||
14 | if not path.endswith(os.sep): |
||
15 | path += os.sep |
||
16 | if pathname.startswith(path): |
||
17 | record.relpath = os.path.relpath(pathname, path) |
||
18 | break |
||
19 | return True |
||
20 | |||
21 | |||
22 | relpath_format_filter = RelpathFormatFilter() |
||
23 | |||
24 | |||
25 | def install(logger): |
||
26 | for handler in logger.handlers: |
||
27 | handler.addFilter(relpath_format_filter) |
||
28 |