|
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
|
|
|
|