mirror of https://github.com/Textualize/rich.git
logging handler tests
This commit is contained in:
parent
83c4c9a5ce
commit
3f7466357a
|
@ -57,10 +57,7 @@ class LogRender:
|
|||
row.append(level)
|
||||
row.append(Renderables(renderables))
|
||||
if self.show_path and path:
|
||||
if line_no is None:
|
||||
row.append(Text(path))
|
||||
else:
|
||||
row.append(Text(f"{path}:{line_no}"))
|
||||
row.append(Text(f"{path}:{line_no}" if line_no else path))
|
||||
|
||||
output.add_row(*row)
|
||||
return output
|
||||
|
|
|
@ -9,8 +9,6 @@ from rich.highlighter import ReprHighlighter
|
|||
from rich.markup import render
|
||||
from rich.text import Text
|
||||
|
||||
# LOG_LEVELS = ["NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]
|
||||
|
||||
|
||||
class RichHandler(Handler):
|
||||
"""A logging handler that renders output with Rich. The time / level / message and file are displayed in columns.
|
||||
|
@ -23,9 +21,9 @@ class RichHandler(Handler):
|
|||
|
||||
KEYWORDS = ["GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS", "TRACE", "PATCH"]
|
||||
|
||||
def __init__(self, level: int = logging.NOTSET) -> None:
|
||||
def __init__(self, level: int = logging.NOTSET, console: Console = None) -> None:
|
||||
super().__init__(level=level)
|
||||
self.console = Console()
|
||||
self.console = Console() if console is None else console
|
||||
self.highlighter = ReprHighlighter()
|
||||
self._log_render = LogRender(show_level=True)
|
||||
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
import io
|
||||
import logging
|
||||
|
||||
from rich.console import Console
|
||||
from rich.logging import RichHandler
|
||||
|
||||
handler = RichHandler(
|
||||
console=Console(file=io.StringIO(), force_terminal=True, width=80)
|
||||
)
|
||||
logging.basicConfig(
|
||||
level="NOTSET", format="%(message)s", datefmt="[DATE] ", handlers=[handler]
|
||||
)
|
||||
log = logging.getLogger("rich")
|
||||
|
||||
|
||||
def make_log():
|
||||
log.debug("foo")
|
||||
render = handler.console.file.getvalue()
|
||||
return render
|
||||
|
||||
|
||||
def test_log():
|
||||
render = make_log()
|
||||
expected = "\x1b[2;36m[DATE] \x1b[0m\x1b[32mDEBUG\x1b[0m foo \x1b[2mtest_logging.py:17\x1b[0m\n"
|
||||
assert render == expected
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
render = make_log()
|
||||
print(render)
|
||||
print(repr(render))
|
Loading…
Reference in New Issue