pyrtma.web_manager.RichHandler

class RichHandler(level=0, console=None, *, show_time=True, omit_repeated_times=True, show_level=True, show_path=True, enable_link_path=True, highlighter=None, markup=False, rich_tracebacks=False, tracebacks_width=None, tracebacks_extra_lines=3, tracebacks_theme=None, tracebacks_word_wrap=True, tracebacks_show_locals=False, tracebacks_suppress=(), locals_max_length=10, locals_max_string=80, log_time_format='[%x %X]', keywords=None)[source]

Bases: Handler

A logging handler that renders output with Rich. The time / level / message and file are displayed in columns. The level is color coded, and the message is syntax highlighted.

Note

Be careful when enabling console markup in log messages if you have configured logging for libraries not under your control. If a dependency writes messages containing square brackets, it may not produce the intended output.

Parameters:
  • level (Union[int, str], optional) – Log level. Defaults to logging.NOTSET.

  • console (Console, optional) – Optional console instance to write logs. Default will use a global console instance writing to stdout.

  • show_time (bool, optional) – Show a column for the time. Defaults to True.

  • omit_repeated_times (bool, optional) – Omit repetition of the same time. Defaults to True.

  • show_level (bool, optional) – Show a column for the level. Defaults to True.

  • show_path (bool, optional) – Show the path to the original log call. Defaults to True.

  • enable_link_path (bool, optional) – Enable terminal link of path column to file. Defaults to True.

  • highlighter (Highlighter, optional) – Highlighter to style log messages, or None to use ReprHighlighter. Defaults to None.

  • markup (bool, optional) – Enable console markup in log messages. Defaults to False.

  • rich_tracebacks (bool, optional) – Enable rich tracebacks with syntax highlighting and formatting. Defaults to False.

  • tracebacks_width (Optional[int], optional) – Number of characters used to render tracebacks, or None for full width. Defaults to None.

  • tracebacks_extra_lines (int, optional) – Additional lines of code to render tracebacks, or None for full width. Defaults to None.

  • tracebacks_theme (str, optional) – Override pygments theme used in traceback.

  • tracebacks_word_wrap (bool, optional) – Enable word wrapping of long tracebacks lines. Defaults to True.

  • tracebacks_show_locals (bool, optional) – Enable display of locals in tracebacks. Defaults to False.

  • tracebacks_suppress (Sequence[Union[str, ModuleType]]) – Optional sequence of modules or paths to exclude from traceback.

  • locals_max_length (int, optional) – Maximum length of containers before abbreviating, or None for no abbreviation. Defaults to 10.

  • locals_max_string (int, optional) – Maximum length of string before truncating, or None to disable. Defaults to 80.

  • log_time_format (Union[str, TimeFormatterCallable], optional) – If log_time is enabled, either string for strftime or callable that formats the time. Defaults to “[%x %X] “.

  • keywords (List[str], optional) – List of words to highlight instead of RichHandler.KEYWORDS.

Initializes the instance - basically setting the formatter to None and the filter list to empty.

Methods

acquire

Acquire the I/O thread lock.

addFilter

Add the specified filter to this handler.

close

Tidy up any resources used by the handler.

createLock

Acquire a thread lock for serializing access to the underlying I/O.

emit

Invoked by logging.

filter

Determine if a record is loggable by consulting all the filters.

flush

Ensure all logging output has been flushed.

format

Format the specified record.

get_level_text

Get the level name from the record.

get_name

handle

Conditionally emit the specified logging record.

handleError

Handle errors which occur during an emit() call.

release

Release the I/O thread lock.

removeFilter

Remove the specified filter from this handler.

render

Render log for display.

render_message

Render message text in to Text.

setFormatter

Set the formatter for this handler.

setLevel

Set the logging level of this handler.

set_name

Attributes

KEYWORDS

name

HIGHLIGHTER_CLASS

alias of ReprHighlighter

acquire()

Acquire the I/O thread lock.

addFilter(filter)

Add the specified filter to this handler.

close()

Tidy up any resources used by the handler.

This version removes the handler from an internal map of handlers, _handlers, which is used for handler lookup by name. Subclasses should ensure that this gets called from overridden close() methods.

createLock()

Acquire a thread lock for serializing access to the underlying I/O.

emit(record)[source]

Invoked by logging.

Return type:

None

Parameters:

record (LogRecord) –

filter(record)

Determine if a record is loggable by consulting all the filters.

The default is to allow the record to be logged; any filter can veto this and the record is then dropped. Returns a zero value if a record is to be dropped, else non-zero.

Changed in version 3.2: Allow filters to be just callables.

flush()

Ensure all logging output has been flushed.

This version does nothing and is intended to be implemented by subclasses.

format(record)

Format the specified record.

If a formatter is set, use it. Otherwise, use the default formatter for the module.

get_level_text(record)[source]

Get the level name from the record.

Parameters:

record (LogRecord) – LogRecord instance.

Returns:

A tuple of the style and level name.

Return type:

Text

handle(record)

Conditionally emit the specified logging record.

Emission depends on filters which may have been added to the handler. Wrap the actual emission of the record with acquisition/release of the I/O thread lock. Returns whether the filter passed the record for emission.

handleError(record)

Handle errors which occur during an emit() call.

This method should be called from handlers when an exception is encountered during an emit() call. If raiseExceptions is false, exceptions get silently ignored. This is what is mostly wanted for a logging system - most users will not care about errors in the logging system, they are more interested in application errors. You could, however, replace this with a custom handler if you wish. The record which was being processed is passed in to this method.

release()

Release the I/O thread lock.

removeFilter(filter)

Remove the specified filter from this handler.

render(*, record, traceback, message_renderable)[source]

Render log for display.

Parameters:
  • record (LogRecord) – logging Record.

  • traceback (Optional[Traceback]) – Traceback instance or None for no Traceback.

  • message_renderable (ConsoleRenderable) – Renderable (typically Text) containing log message contents.

Returns:

Renderable to display log.

Return type:

ConsoleRenderable

render_message(record, message)[source]

Render message text in to Text.

Parameters:
  • record (LogRecord) – logging Record.

  • message (str) – String containing log message.

Returns:

Renderable to display log message.

Return type:

ConsoleRenderable

setFormatter(fmt)

Set the formatter for this handler.

setLevel(level)

Set the logging level of this handler. level must be an int or a str.