Skip to content

log

The module containing PROCESS' custom log handler.

This handler is designed to capture logs during the output phase of a run--when the models are being run for the final time. This ensures captured logs are relevant for the solution that is written to the MFile.

ProcessLogHandler

Bases: Handler

PROCESS' custom log handler that stores captured logs on the handler.

The handler will only store logs when an internal attribute _capturing is True. This can be changed by setting the capturing keyword when instantiating the handler or using the methods start_capturing/stop_capturing.

Source code in process/core/log.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class ProcessLogHandler(Handler):
    """PROCESS' custom log handler that stores captured logs on the handler.

    The handler will only store logs when an internal attribute _capturing is
    True. This can be changed by setting the capturing keyword when instantiating
    the handler or using the methods start_capturing/stop_capturing.
    """

    def __init__(self, capturing=True):
        """Instantiates a ProcessLogHandler.

        :param capturing: capture and store emitted logs?
        :type capturning: bool
        """
        super().__init__()
        self._logs = []
        self._capturing = capturing

    def start_capturing(self):
        """Start capturing logs and storing them within the handler."""
        self._capturing = True

    def stop_capturing(self):
        """Stop capturing logs and storing them within the handler."""
        self._capturing = False

    def emit(self, record):
        """Method called when creating a logging record (e.g. logger.warning)."""
        if self._capturing:
            self._logs.append(record)

    def clear_logs(self):
        """Empties the handler's internal record of previous logs."""
        self._logs = []

    def num_logs(self):
        """The number of logs the handler has stored."""
        return len(self._logs)

    def render_warnings(self):
        """Render the stored warnings for printing to the terminal or OUTFile."""
        return "\n\n".join([
            f"({w.pathname}:{w.lineno}) {w.getMessage()}" for w in self._logs
        ])

start_capturing()

Start capturing logs and storing them within the handler.

Source code in process/core/log.py
33
34
35
def start_capturing(self):
    """Start capturing logs and storing them within the handler."""
    self._capturing = True

stop_capturing()

Stop capturing logs and storing them within the handler.

Source code in process/core/log.py
37
38
39
def stop_capturing(self):
    """Stop capturing logs and storing them within the handler."""
    self._capturing = False

emit(record)

Method called when creating a logging record (e.g. logger.warning).

Source code in process/core/log.py
41
42
43
44
def emit(self, record):
    """Method called when creating a logging record (e.g. logger.warning)."""
    if self._capturing:
        self._logs.append(record)

render_warnings()

Render the stored warnings for printing to the terminal or OUTFile.

Source code in process/core/log.py
54
55
56
57
58
def render_warnings(self):
    """Render the stored warnings for printing to the terminal or OUTFile."""
    return "\n\n".join([
        f"({w.pathname}:{w.lineno}) {w.getMessage()}" for w in self._logs
    ])

show_errors(file_unit)

Write the rendered captured logs to the terminal/OUTFile

Parameters:

Name Type Description Default
file_unit int

a number describing the output medium (terminal, OUTFile)

required
Source code in process/core/log.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def show_errors(file_unit: int):
    """Write the rendered captured logs to the terminal/OUTFile

    Parameters
    ----------
    file_unit : int
        a number describing the output medium (terminal, OUTFile)

    """
    warning_string = (
        "******************************************** Errors and Warnings *********************************************"
        f"\n{logging_model_handler.render_warnings()}"
    )
    print(warning_string)
    process_output.write(file_unit, warning_string)
    process_output.ovarre(
        constants.MFILE,
        "Error status",
        "(error_status)",
        0 if logging_model_handler.num_logs() == 0 else 2,
        "OP",
    )