Passed
Pull Request — master (#297)
by
unknown
01:57
created

LogManager.new_logger()   A

Complexity

Conditions 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 4.3145

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 1
cts 6
cp 0.1666
rs 9.4285
cc 2
crap 4.3145
1
"""Module with classes used to handle logs displayed by Kytos SDN Platform."""
2 1
import logging
3 1
from logging.handlers import SysLogHandler
4
5 1
__all__ = ('LogManager')
6
7
8 1
class LogManager:
9
    """Class responsible to handle all logs send by kytos and the Napps."""
10
11 1
    fmt = '%(asctime)s - %(levelname)s [%(name)s] (%(threadName)s) %(message)s'
12 1
    handlers_enabled = []
13 1
    loggers = {}
14
15 1
    def __init__(self, mode='INFO'):
16
        """Contructor of LogManager.
17
18
        This method will create a LogManager instance with the mode given.
19
20
        Args:
21
            mode(string): Type of log that will be registered by kytos.
22
        """
23
        logging.basicConfig(format=self.fmt, level=getattr(logging, mode))
24
25 1
    def new_logger(self, logger_name):
26
        """Method used to instantiate a new logger.
27
28
        Args:
29
            logger_name(string): logger name that will be instantiated.
30
        Returns:
31
            logger(`logging.Logger`): instance of logging.Logger.
32
        """
33
        logger = logging.getLogger(logger_name)
34
        if logger_name not in self.loggers.keys():
35
            self.loggers[logger.name] = logger
36
            self.__add_handlers_enabled_to_logger(logger)
37
38
        return logger
39
40 1
    def __add_handlers_enabled_to_logger(self, logger):
41
        """Private method to add all handlers in a logger instance.
42
43
        Args:
44
            logger(`logging.Logger`): instance that will receive the handlers.
45
        """
46
        for handler in self.handlers_enabled:
47
            logger.addHandler(handler)
48
49 1
    def __add_handler_to_loggers(self, handler):
50
        """"Private method to add a new handlers into all loggers instances.
51
52
        Args:
53
            handler(Handler): instance of Handler
54
        """
55
        for logger in self.loggers.values():
56
            logger.addHandler(handler)
57
58
        self.handlers_enabled.append(handler)
59
60 1
    def enable_syslog(self):
61
        """Method used to enable the syslog."""
62
        syslog_handler = logging.handlers.SysLogHandler(address='/dev/log')
63
        syslog_handler.setFormatter(logging.Formatter(self.fmt))
64
        self.__add_handler_to_loggers(syslog_handler)
65
        self.syslog = True
66
67 1
    def enable_websocket_log(self, websocket):
68
        """Method used used to enable the websocket log.
69
70
        This method get the stream from websocket given and add this into the
71
        handlers registered.
72
73
        Args:
74
            websocket(LogWebSocket): instance of LogWebSocket.
75
        """
76
        stream_handler = logging.StreamHandler(websocket.stream)
77
        stream_handler.setFormatter(logging.Formatter(self.fmt))
78
        self.__add_handler_to_loggers(stream_handler)
79
80 1
    def __hide_unused_logs(self):
81
        """Privated method used to hide unused logs."""
82
        engineio_logs = logging.getLogger('engineio')
83
        engineio_logs.setLevel(logging.NOTSET)
84
        socketio_logs = logging.getLogger('socketio')
85
        socketio_logs.setLevel(logging.NOTSET)
86