diff --git a/FAQ.md b/FAQ.md index 6cb43f1f..01935f67 100644 --- a/FAQ.md +++ b/FAQ.md @@ -3,6 +3,7 @@ - [Why does emoji break alignment in a Table or Panel?](#why-does-emoji-break-alignment-in-a-table-or-panel) - [Why does content in square brackets disappear?](#why-does-content-in-square-brackets-disappear) - [python -m rich.spinner shows extra lines](#python--m-rich.spinner-shows-extra-lines) +- [How do I log a renderable?](#how-do-i-log-a-renderable) - [Strange colors in console output.](#strange-colors-in-console-output.) @@ -31,6 +32,17 @@ The spinner example is know to break on some terminals (Windows in particular). Some terminals don't display emoji with the correct width, which means Rich can't always align them accurately inside a panel. + +## How do I log a renderable? + +Python's logging module is designed to work with strings. Consequently you won't be able to log Rich renderables (Table, Tree, etc) by calling `logger.debug` or other similar method. + +You could use the [capture](https://rich.readthedocs.io/en/latest/console.html#capturing-output) API to convert the renderable to a string and log that. However I would advise against it. + +Logging supports configurable back-ends, which means that a log message could go somewhere other than the terminal -- which may not correctly render the formatting and style produced by Rich. + +If you are only logging with a file-handler to stdout, then you probably don't need to use the logging module at all. Consider using [Console.log](https://rich.readthedocs.io/en/latest/reference/console.html#rich.console.Console.log) which will render anything that you can print with Rich, with a timestamp. + ## Strange colors in console output. diff --git a/questions/highlighting_unexpected.question.md b/questions/highlighting_unexpected.question.md index 958bf3fd..f0c2ad6b 100644 --- a/questions/highlighting_unexpected.question.md +++ b/questions/highlighting_unexpected.question.md @@ -2,6 +2,8 @@ title: "Strange colors in console output." alt_titles: - "Why are numbers in cyan?" + - "Rich print seems to make decimals cyan" + - "Numbers get printed differently" --- Rich will highlight certain patterns in your output such as numbers, strings, and other objects like IP addresses. diff --git a/questions/log_renderables.question.md b/questions/log_renderables.question.md new file mode 100644 index 00000000..902f32c7 --- /dev/null +++ b/questions/log_renderables.question.md @@ -0,0 +1,14 @@ +--- +title: "How do I log a renderable?" +alt_titles: + - "Cannot log Tree() output to file" + - "Log a Panel or Table to a RichHandler" +--- + +Python's logging module is designed to work with strings. Consequently you won't be able to log Rich renderables (Table, Tree, etc) by calling `logger.debug` or other similar method. + +You could use the [capture](https://rich.readthedocs.io/en/latest/console.html#capturing-output) API to convert the renderable to a string and log that. However I would advise against it. + +Logging supports configurable back-ends, which means that a log message could go somewhere other than the terminal -- which may not correctly render the formatting and style produced by Rich. + +If you are only logging with a file-handler to stdout, then you probably don't need to use the logging module at all. Consider using [Console.log](https://rich.readthedocs.io/en/latest/reference/console.html#rich.console.Console.log) which will render anything that you can print with Rich, with a timestamp. diff --git a/questions/logging_color.md b/questions/logging_color.md new file mode 100644 index 00000000..1db3bf0d --- /dev/null +++ b/questions/logging_color.md @@ -0,0 +1,11 @@ +--- +title: "How do I render console markup in RichHandler?" +alt_titles: + - "Color is not applied during logging." + - "Style tags don't work with logging handler" + +--- + +Console markup wont work anywhere else, other than `RichHandler` -- which is why they are disabled by default. + +See the docs if you want to [enable console markup](https://rich.readthedocs.io/en/latest/logging.html#logging-handler) in the logging handler.