|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
|
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
|
3
|
|
|
# this work for additional information regarding copyright ownership. |
|
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
|
6
|
|
|
# the License. You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
import logging |
|
17
|
|
|
|
|
18
|
|
|
__all__ = [ |
|
19
|
|
|
'LoggerNameExclusionFilter', |
|
20
|
|
|
'LoggerFunctionNameExclusionFilter', |
|
21
|
|
|
'LogLevelFilter', |
|
22
|
|
|
] |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
class LoggerNameExclusionFilter(object): |
|
26
|
|
|
""" |
|
27
|
|
|
Filter which excludes log messages whose first component of the logger name matches one of the |
|
28
|
|
|
entries in the exclusions list. |
|
29
|
|
|
""" |
|
30
|
|
|
|
|
31
|
|
|
def __init__(self, exclusions): |
|
32
|
|
|
self._exclusions = set(exclusions) |
|
33
|
|
|
|
|
34
|
|
|
def filter(self, record): |
|
35
|
|
|
if len(self._exclusions) < 1: |
|
36
|
|
|
return True |
|
37
|
|
|
|
|
38
|
|
|
module_decomposition = record.name.split('.') |
|
39
|
|
|
exclude = len(module_decomposition) > 0 and module_decomposition[0] in self._exclusions |
|
40
|
|
|
return not exclude |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
class LoggerFunctionNameExclusionFilter(object): |
|
44
|
|
|
""" |
|
45
|
|
|
Filter which excludes log messages whose function name matches one of the names in the |
|
46
|
|
|
exclusions list. |
|
47
|
|
|
""" |
|
48
|
|
|
|
|
49
|
|
|
def __init__(self, exclusions): |
|
50
|
|
|
self._exclusions = set(exclusions) |
|
51
|
|
|
|
|
52
|
|
|
def filter(self, record): |
|
53
|
|
|
if len(self._exclusions) < 1: |
|
54
|
|
|
return True |
|
55
|
|
|
|
|
56
|
|
|
function_name = getattr(record, 'funcName', None) |
|
57
|
|
|
if function_name in self._exclusions: |
|
58
|
|
|
return False |
|
59
|
|
|
|
|
60
|
|
|
return True |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
class LogLevelFilter(logging.Filter): |
|
64
|
|
|
""" |
|
65
|
|
|
Filter which excludes log messages which match the provided log levels. |
|
66
|
|
|
""" |
|
67
|
|
|
|
|
68
|
|
|
def __init__(self, log_levels): |
|
|
|
|
|
|
69
|
|
|
self._log_levels = log_levels |
|
70
|
|
|
|
|
71
|
|
|
def filter(self, record): |
|
72
|
|
|
level = record.levelno |
|
73
|
|
|
if level in self._log_levels: |
|
74
|
|
|
return False |
|
75
|
|
|
|
|
76
|
|
|
return True |
|
77
|
|
|
|
It is generally advisable to initialize the super-class by calling its
__init__method: