Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Add PromptToolkitLogHandler. #1600

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions examples/logging/colored_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Example of a custom logging handler that uses prompt_toolkit to render nicely
colored logs.
"""
import logging
import time

from prompt_toolkit.shortcuts.logging import PromptToolkitLogHandler


def main() -> None:
logging.basicConfig(level=logging.DEBUG, handlers=[PromptToolkitLogHandler()])

for i in range(10):
logging.debug(f"test debug {i}")
logging.info(f"test info {i}")
logging.warning(f"test warning {i}")
logging.error(f"test error {i}")
time.sleep(0.5)


if __name__ == "__main__":
main()
74 changes: 74 additions & 0 deletions src/prompt_toolkit/shortcuts/logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
"""
Utility for printing colored logs.
"""
import datetime
import logging
from typing import Callable, Optional, TextIO, Union

from prompt_toolkit.formatted_text import HTML
from prompt_toolkit.layout.containers import AnyContainer, VSplit, Window
from prompt_toolkit.layout.controls import FormattedTextControl
from prompt_toolkit.styles import Style

from .utils import print_container, print_formatted_text

__all__ = ["PromptToolkitLogHandler"]


def default_template(record: logging.LogRecord) -> HTML:
now = datetime.datetime.now()
return HTML(
f"<{record.levelname}>"
"<time>[{now}]</time> <level>{level:10}</level> <message>{msg}</message>"
f"</{record.levelname}>"
).format(
now=now.strftime("%m/%d/%y %H:%M:%S"),
level=record.levelname,
msg=record.msg,
)


def default_style() -> Style:
return Style.from_dict(
{
"time": "ansimagenta",
"warning": "#aa8888",
"warning level": "reverse",
"debug": "#888888",
"error": "ansired",
"error level": "reverse",
"info": "#00aa00",
}
)


class PromptToolkitLogHandler(logging.StreamHandler):
"""
Print all logging messages using the given template.

Usage::

import logging
logging.basicConfig(
level=logging.DEBUG,
handlers=[PromptToolkitHandler()]
)
"""
# NOTE: Previously, I tried using `print_container` instead of
# `print_formatted_text`. This doesn't work unfortunately, because
# we'd try to create/access an event loop, which causes asyncio to
# log new messages. We can't log anything while printing a log
# message.

def __init__(
self,
template: Callable[[logging.LogRecord], HTML] = default_template,
style: Style = default_style(),
stream: Optional[TextIO] = None,
) -> None:
self.template = template
self.style = style
super().__init__(stream=stream)

def emit(self, record) -> None:
print_formatted_text(self.template(record), style=self.style, file=self.stream)