Completed
Pull Request — master (#2409)
by
unknown
02:05
created

ListLogPrinter.log_level()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 6
rs 9.4285
c 2
b 0
f 0
1
from pyprint.Printer import Printer
2
3
from coalib.output.printers.LOG_LEVEL import LOG_LEVEL
4
from coalib.output.printers.LogPrinter import LogPrinter
5
from coalib.processes.communication.LogMessage import LogMessage
6
7
8
class ListLogPrinter(Printer, LogPrinter):
9
    """
10
    A ListLogPrinter is a log printer which collects all LogMessages to a list
11
    so that the logs can be used at a later time.
12
    """
13
14
    def __init__(self,
15
                 log_level=LOG_LEVEL.WARNING,
16
                 timestamp_format="%X"):
17
        Printer.__init__(self)
18
        LogPrinter.__init__(self, self, log_level, timestamp_format)
19
20
        self.logs = []
21
22
    # We should not in any way modify the underlying self.logger, as it is
23
    # default that is used for all the logging, and modifying it may bring
24
    # unexpected behaviour.
25
    # In future, this class should not depend either on Printer, or LogPrinter.
26
    @property
27
    def log_level(self):
28
        """
29
        Returns current log_level used in logger.
30
        """
31
        return self._log_level
32
33
    @log_level.setter
34
    def log_level(self, log_level):
35
        """
36
        Sets log_level for logger.
37
        """
38
        self._log_level = log_level
39
40
    def log_message(self, log_message, **kwargs):
41
        if not isinstance(log_message, LogMessage):
42
            raise TypeError("log_message should be of type LogMessage.")
43
44
        if log_message.log_level < self.log_level:
45
            return
46
47
        self.logs.append(log_message)
48
49
    def _print(self, output, **kwargs):
50
        self.info(output, **kwargs)
51