Exploring the Console Class and Logging
00:00
The Console
class and logging. Rich has a Console
class that encapsulates most of the package’s capabilities. A Console
instance can format text, generate colorized, log output, or pretty print JSON, and it can also handle indentation, horizontal rules, widgets, panels, and tables, as well as interactive prompts and animations.
00:24
You can capture console output and export it as text, SVG, or HTML. The print()
function that comes with rich is a shortcut that supports some of the console features in longer programs.
00:37
You should prefer Console
and its print()
method over rich print()
as it’s more powerful. Console
will interpret console markup to apply colors and attributes to text.
00:49 All of these features are available subject to your terminal’s capabilities, but most modern terminal emulators will handle everything just fine.
01:06 Console markup consists of paired tags within square brackets. Here, a color and style is specified with green underlined text and blue italicized text, which are then displayed when the command is executed.
01:26 The specified styles are applied to the text. Within the tags, you can find a complete list of the names, hex codes, and RGB values of the 255 standard text colors in the rich appendix, but you can also view them from the command line.
01:51 If your terminal supports true color, then you can specify any of the 16 million colors available by their RGB values.
02:06
As a shorthand, you can use a forward slash enclosed in brackets to close the last applied style. You can specify the background color by using on
between the foreground and background colors as seen here,
02:31 along with the full range of text colors, Console markups supports attributes such as bold, blink, reverse, underline, and italic. A combination of color and attributes is called a style, and you can put several styles together in a dictionary to create a theme.
03:02
In a custom theme, you’ve defined styles that you can apply to any Console
instance. Using a Console
object with a theme helps keep your formatting consistent across the application.
03:43 Creating good log statements is an important part of writing maintainable code. Logging helps you during development and testing by confirming that your code is following the expected paths.
03:54
It’s invaluable when things go wrong in production code as well. Placed log statements can provide insight into obscure code misbehavior. The Console
class supports formatted logging and uses a syntax that’s close to that of Python’s standard logging package.
04:10 It can generate nicely formatted tracebacks of any uncaught exceptions in your code while using styles from a defined theme. Python’s built-in tracebacks and error messages get more informative and useful with each new release, but some extra eye candy doesn’t hurt and there’s no substitute for good log messages to give context for a crash.
04:31 Continuing the previous example, you can produce some samples of logging output
04:45 Log messages can use the themes you defined previously.
04:56
With console.log
, you automatically add timestamps, source file names, and source line numbers to your logging statements
05:14 while things are running. Normally, you’ll just get the clean log output as seen, but if there’s a crash during development, then you’ll want to log as much information as possible about the cause.
05:25 Try manually throwing a runtime error and see the difference.
05:38 When an exception happens, the traceback can optionally display all of your local variables as well as the stack trace of the error. Your session may contain more local variables than this, so the output may look different.
05:51 Tools like these can really help to make your development experience more pleasant and productive, which is colorful and nicely presented. Error messages are much more readable than Python’s default tracebacks.
06:02 You may even look forward to getting an exception, but one category of people who will not look forward to exceptions will be your end users, so to keep them happy, in the next section of the course, you’ll look at ways to engage your users with animation.
Become a Member to join the conversation.