|
1
|
|
|
import logging |
|
2
|
|
|
|
|
3
|
|
|
from pyprint.ColorPrinter import ColorPrinter |
|
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
from coalib.output.printers.LOG_LEVEL import LOG_LEVEL |
|
6
|
|
|
from coalib.output.printers.LOG_LEVEL import LOG_LEVEL_COLORS |
|
|
|
|
|
|
7
|
|
|
from coalib.processes.communication.LogMessage import LogMessage |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class LogPrinter: |
|
11
|
|
|
""" |
|
12
|
|
|
The LogPrinter class allows to print log messages to an underlying Printer. |
|
13
|
|
|
|
|
14
|
|
|
This class is an adapter, means you can create a LogPrinter from every |
|
15
|
|
|
existing Printer instance. |
|
16
|
|
|
""" |
|
17
|
|
|
|
|
18
|
|
|
def __init__(self, |
|
19
|
|
|
printer, |
|
20
|
|
|
log_level=LOG_LEVEL.INFO, |
|
21
|
|
|
timestamp_format="%X"): |
|
22
|
|
|
""" |
|
23
|
|
|
Creates a new log printer from an existing Printer. |
|
24
|
|
|
|
|
25
|
|
|
:param printer: The underlying Printer where log messages |
|
26
|
|
|
shall be written to. If you inherit from |
|
27
|
|
|
LogPrinter, set it to self. |
|
28
|
|
|
:param log_level: The minimum log level, everything below will |
|
29
|
|
|
not be logged. |
|
30
|
|
|
:param timestamp_format: The format string for the |
|
31
|
|
|
datetime.today().strftime(format) method. |
|
32
|
|
|
""" |
|
33
|
|
|
self._printer = printer |
|
34
|
|
|
self.log_level = log_level |
|
35
|
|
|
self.timestamp_format = timestamp_format |
|
36
|
|
|
|
|
37
|
|
|
@property |
|
38
|
|
|
def printer(self): |
|
39
|
|
|
""" |
|
40
|
|
|
Returns the underlying printer where logs are printed to. |
|
41
|
|
|
""" |
|
42
|
|
|
return self._printer |
|
43
|
|
|
|
|
44
|
|
|
def _get_log_prefix(self, log_level, timestamp): |
|
45
|
|
|
datetime_string = timestamp.strftime(self.timestamp_format) |
|
46
|
|
|
|
|
47
|
|
|
if datetime_string != "": |
|
48
|
|
|
datetime_string = "[" + datetime_string + "]" |
|
49
|
|
|
|
|
50
|
|
|
return '[{}]{}'.format(LOG_LEVEL.reverse.get(log_level, "ERROR"), |
|
51
|
|
|
datetime_string) |
|
52
|
|
|
|
|
53
|
|
|
def debug(self, *messages, delimiter=" ", timestamp=None, **kwargs): |
|
54
|
|
|
logging.debug(self._compile_message(*messages, delimiter=delimiter)) |
|
55
|
|
|
|
|
56
|
|
|
def info(self, *messages, delimiter=" ", timestamp=None, **kwargs): |
|
57
|
|
|
logging.info(self._compile_message(*messages, delimiter=delimiter)) |
|
58
|
|
|
|
|
59
|
|
|
def warn(self, *messages, delimiter=" ", timestamp=None, **kwargs): |
|
60
|
|
|
logging.warning(self._compile_message(*messages, delimiter=delimiter)) |
|
61
|
|
|
|
|
62
|
|
|
def err(self, *messages, delimiter=" ", timestamp=None, **kwargs): |
|
63
|
|
|
logging.error(self._compile_message(*messages, delimiter=delimiter)) |
|
64
|
|
|
|
|
65
|
|
|
def _compile_message(self, *messages, delimiter): |
|
|
|
|
|
|
66
|
|
|
return str(delimiter).join(str(message) |
|
67
|
|
|
for message in messages).rstrip() |
|
68
|
|
|
|
|
69
|
|
|
def log_exception(self, |
|
|
|
|
|
|
70
|
|
|
message, |
|
71
|
|
|
exception: BaseException, |
|
72
|
|
|
log_level=LOG_LEVEL.ERROR, |
|
73
|
|
|
timestamp=None, |
|
74
|
|
|
**kwargs): |
|
75
|
|
|
""" |
|
76
|
|
|
If the log_level of the printer is greater than DEBUG, it prints |
|
77
|
|
|
only the message. If it is DEBUG or lower, it shows the message |
|
78
|
|
|
along with the traceback of the exception. |
|
79
|
|
|
|
|
80
|
|
|
:param message: The message to print. |
|
81
|
|
|
:param exception: The exception to print. |
|
82
|
|
|
:param log_level: The log_level of this message (not used when |
|
83
|
|
|
logging the traceback. Tracebacks always have |
|
84
|
|
|
a level of DEBUG). |
|
85
|
|
|
:param timestamp: The time at which this log occured. Defaults to |
|
86
|
|
|
the current time. |
|
87
|
|
|
:param kwargs: Keyword arguments to be passed when logging the |
|
88
|
|
|
message (not used when logging the traceback). |
|
89
|
|
|
""" |
|
90
|
|
|
if not isinstance(exception, BaseException): |
|
91
|
|
|
raise TypeError("log_exception can only log derivatives of " |
|
92
|
|
|
"BaseException.") |
|
93
|
|
|
|
|
94
|
|
|
logging.exception(message, exc_info=exception) |
|
95
|
|
|
|