Passed
Push — master ( 8ccc33...a7e74c )
by
unknown
08:52
created

LoggerNameExclusionFilter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 0 7 2
A __init__() 0 2 1
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):
0 ignored issues
show
Bug introduced by
The __init__ method of the super-class Filter is not called.

It is generally advisable to initialize the super-class by calling its __init__ method:

class SomeParent:
    def __init__(self):
        self.x = 1

class SomeChild(SomeParent):
    def __init__(self):
        # Initialize the super class
        SomeParent.__init__(self)
Loading history...
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