Test Failed
Push — master ( e380d0...f5671d )
by W
02:58
created

st2client/st2client/utils/logging.py (1 issue)

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
from __future__ import absolute_import
17
18
import logging
19
20
__all__ = [
21
    'LogLevelFilter',
22
    'set_log_level_for_all_handlers',
23
    'set_log_level_for_all_loggers'
24
]
25
26
27
class LogLevelFilter(logging.Filter):
28
    """
29
    Filter which excludes log messages which match the provided log levels.
30
    """
31
32
    def __init__(self, log_levels):
0 ignored issues
show
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...
33
        self._log_levels = log_levels
34
35
    def filter(self, record):
36
        level = record.levelno
37
        if level in self._log_levels:
38
            return False
39
40
        return True
41
42
43
def set_log_level_for_all_handlers(logger, level=logging.DEBUG):
44
    """
45
    Set a log level for all the handlers on the provided logger.
46
    """
47
    logger.setLevel(level)
48
49
    handlers = logger.handlers
50
    for handler in handlers:
51
        handler.setLevel(level)
52
53
    return logger
54
55
56
def set_log_level_for_all_loggers(level=logging.DEBUG):
57
    """
58
    Set a log level for all the loggers and handlers to the provided level.
59
    """
60
    root_logger = logging.getLogger()
61
    loggers = list(logging.Logger.manager.loggerDict.values())
62
    loggers += [root_logger]
63
64
    for logger in loggers:
65
        if not isinstance(logger, logging.Logger):
66
            continue
67
68
        set_log_level_for_all_handlers(logger=logger)
69