-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create tty log handler to handle user output
- Loading branch information
Showing
3 changed files
with
43 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import logging | ||
|
||
from prompt_toolkit import print_formatted_text | ||
from prompt_toolkit.application import create_app_session_from_tty | ||
from prompt_toolkit.formatted_text import FormattedText | ||
|
||
|
||
class TtyLogHandler(logging.Handler): | ||
def emit(self, record): | ||
if record.levelno >= logging.ERROR: | ||
color = "ansired" | ||
elif record.levelno == logging.WARNING: | ||
color = "ansiyellow" | ||
else: | ||
color = "ansywhite" | ||
log_entry = self.format(record) | ||
formatted_log_entry = FormattedText([(color, f"\n{log_entry}")]) | ||
with create_app_session_from_tty(): | ||
print_formatted_text(formatted_log_entry) |