-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathcleo_handler.py
More file actions
81 lines (64 loc) · 2.5 KB
/
cleo_handler.py
File metadata and controls
81 lines (64 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from __future__ import annotations
import logging
from logging import LogRecord
from typing import TYPE_CHECKING
from typing import ClassVar
from typing import cast
from cleo.exceptions import CleoUserError
from cleo.io.outputs.output import Verbosity
from cleo.ui.exception_trace.component import ExceptionTrace
if TYPE_CHECKING:
from cleo.io.outputs.output import Output
class CleoFilter:
def __init__(self, output: Output):
self.output = output
@property
def current_loglevel(self) -> int:
verbosity_mapping: dict[Verbosity, int] = {
Verbosity.QUIET: logging.CRITICAL, # Nothing gets emitted to the output anyway
Verbosity.NORMAL: logging.WARNING,
Verbosity.VERBOSE: logging.INFO,
Verbosity.VERY_VERBOSE: logging.DEBUG,
Verbosity.DEBUG: logging.DEBUG,
}
return verbosity_mapping[self.output.verbosity]
def filter(self, record: LogRecord) -> bool:
return record.levelno >= self.current_loglevel
class CleoHandler(logging.Handler):
"""
A handler class which writes logging records, appropriately formatted,
to a Cleo output stream.
"""
tags: ClassVar[dict[str, str]] = {
"CRITICAL": "<error>",
"ERROR": "<error>",
"WARNING": "<warning>",
"DEBUG": "<debug>",
}
def __init__(self, output: Output):
super().__init__()
self.output = output
self.addFilter(CleoFilter(output))
def emit(self, record: logging.LogRecord) -> None:
"""
Emit a record.
If a formatter is specified, it is used to format the record.
The record is then written to the output with a trailing newline. If
exception information is present, it is formatted using
traceback.print_exception and appended to the stream. If the stream
has an 'encoding' attribute, it is used to determine how to do the
output to the stream.
"""
try:
msg = self.tags.get(record.levelname, "") + self.format(record) + "</>"
self.output.write(msg, new_line=True)
if record.exc_info:
_type, error, traceback = record.exc_info
simple = not self.output.is_verbose() or isinstance(
error, CleoUserError
)
error = cast("Exception", error)
trace = ExceptionTrace(error)
trace.render(self.output, simple)
except Exception:
self.handleError(record)