|
29 | 29 |
|
30 | 30 | log = logging.getLogger(__name__)
|
31 | 31 |
|
32 |
| - |
| 32 | +class FilterSet(object): |
| 33 | + def __init__(self, filter_components): |
| 34 | + self.inclusive, self.exclusive = self._partition(filter_components) |
| 35 | + |
| 36 | + @staticmethod |
| 37 | + def _partition(components): |
| 38 | + inclusive, exclusive = [], [] |
| 39 | + for component in components: |
| 40 | + if component.startswith('-'): |
| 41 | + exclusive.append(component[1:]) |
| 42 | + else: |
| 43 | + inclusive.append(component) |
| 44 | + return inclusive, exclusive |
| 45 | + |
| 46 | + def allow(self, record): |
| 47 | + """returns whether this record should be printed""" |
| 48 | + if not self: |
| 49 | + # nothing to filter |
| 50 | + return True |
| 51 | + return self._allow(record) and not self._deny(record) |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def _any_match(matchers, record): |
| 55 | + """return the bool of whether `record` starts with |
| 56 | + any item in `matchers`""" |
| 57 | + def record_matches_key(key): |
| 58 | + return record == key or record.startswith(key + '.') |
| 59 | + return any(map(record_matches_key, matchers)) |
| 60 | + |
| 61 | + def _allow(self, record): |
| 62 | + if not self.inclusive: |
| 63 | + return True |
| 64 | + return self._any_match(self.inclusive, record) |
| 65 | + |
| 66 | + def _deny(self, record): |
| 67 | + if not self.exclusive: |
| 68 | + return False |
| 69 | + return self._any_match(self.exclusive, record) |
| 70 | + |
33 | 71 | class MyMemoryHandler(BufferingHandler):
|
34 | 72 | def __init__(self, capacity, logformat, logdatefmt, filters):
|
35 | 73 | BufferingHandler.__init__(self, capacity)
|
36 | 74 | fmt = logging.Formatter(logformat, logdatefmt)
|
37 | 75 | self.setFormatter(fmt)
|
38 |
| - self.filters = filters |
| 76 | + self.filterset = FilterSet(filters) |
39 | 77 | def flush(self):
|
40 | 78 | pass # do nothing
|
41 | 79 | def truncate(self):
|
42 | 80 | self.buffer = []
|
43 | 81 | def filter(self, record):
|
44 |
| - """Our custom record filtering logic. |
45 |
| -
|
46 |
| - Built-in filtering logic (via logging.Filter) is too limiting. |
47 |
| - """ |
48 |
| - if not self.filters: |
49 |
| - return True |
50 |
| - matched = False |
51 |
| - rname = record.name # shortcut |
52 |
| - for name in self.filters: |
53 |
| - if rname == name or rname.startswith(name+'.'): |
54 |
| - matched = True |
55 |
| - return matched |
| 82 | + return self.filterset.allow(record.name) |
56 | 83 | def __getstate__(self):
|
57 | 84 | state = self.__dict__.copy()
|
58 | 85 | del state['lock']
|
@@ -109,7 +136,9 @@ def options(self, parser, env):
|
109 | 136 | " verbose,\nuse this option to filter out needless output.\n"
|
110 | 137 | "Example: filter=foo will capture statements issued ONLY to\n"
|
111 | 138 | " foo or foo.what.ever.sub but not foobar or other logger.\n"
|
112 |
| - "Specify multiple loggers with comma: filter=foo,bar,baz." |
| 139 | + "Specify multiple loggers with comma: filter=foo,bar,baz.\n" |
| 140 | + "If any logger name is prefixed with a minus, eg filter=-foo,\n" |
| 141 | + "it will be excluded rather than included." |
113 | 142 | " [NOSE_LOGFILTER]\n")
|
114 | 143 | parser.add_option(
|
115 | 144 | "--logging-clear-handlers", action="store_true",
|
|
0 commit comments