ThreadedRichHandler

class gratools.ThreadedRichHandler(*args, **kwargs)[source]

Bases: RichHandler

Custom RichHandler that processes log records in a separate thread to prevent blocking the main application thread, especially during I/O-bound logging operations (like writing to console or complex formatting).

It also includes a feature to trigger a program exit if a log record of ERROR level or higher is emitted through it.

Attributes

runningbool

A flag indicating whether the log processing worker thread should continue running.

log_queuequeue.Queue[logging.LogRecord] # Type hint for clarity

A thread-safe queue used to buffer log records before they are processed by the worker thread.

worker_threadthreading.Thread

The background thread responsible for consuming log records from log_queue.

critical_error_occurredbool

A flag set to True if an ERROR or CRITICAL log has been processed, leading to program termination.

Attributes Summary

KEYWORDS

name

Methods Summary

acquire()

Acquire the I/O thread lock.

addFilter(filter)

Add the specified filter to this handler.

close()

Closes the handler, ensuring the worker thread is stopped and resources are released.

createLock()

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

emit(record)

Queues a log record for processing by the worker thread.

filter(record)

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

flush()

Ensure all logging output has been flushed.

format(record)

Format the specified record.

get_level_text(record)

Get the level name from the record.

get_name()

handle(record)

Conditionally emit the specified logging record.

handleError(record)

Handle errors which occur during an emit() call.

release()

Release the I/O thread lock.

removeFilter(filter)

Remove the specified filter from this handler.

render(*, record, traceback, message_renderable)

Render log for display.

render_message(record, message)

Render message text in to Text.

setFormatter(fmt)

Set the formatter for this handler.

setLevel(level)

Set the logging level of this handler.

set_name(name)

stop_processing()

Signals the worker thread to stop and waits for it to terminate.

Attributes Documentation

KEYWORDS: ClassVar[List[str] | None] = ['GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'OPTIONS', 'TRACE', 'PATCH']
name

Methods Documentation

acquire()

Acquire the I/O thread lock.

addFilter(filter)

Add the specified filter to this handler.

close()[source]

Closes the handler, ensuring the worker thread is stopped and resources are released.

Return type:

None

createLock()

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

emit(record)[source]

Queues a log record for processing by the worker thread.

If the log record’s level is ERROR or higher, this method sets a flag to indicate a critical error and initiates the process to stop the log processing and exit the application.

Parameters

recordlogging.LogRecord

The log record to be emitted.

Parameters:

record (LogRecord)

Return type:

None

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 by returning a false value. If a filter attached to a handler returns a log record instance, then that instance is used in place of the original log record in any further processing of the event by that handler. If a filter returns any other true value, the original log record is used in any further processing of the event by that handler.

If none of the filters return false values, this method returns a log record. If any of the filters return a false value, this method returns a false value.

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

Changed in version 3.12: Allow filters to return a LogRecord instead of modifying it in place.

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)

Get the level name from the record.

Parameters:

record (LogRecord) – LogRecord instance.

Returns:

Text – A tuple of the style and level name.

Return type:

Text

get_name()
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 an instance of the log record that was emitted if it passed all filters, otherwise a false value is returned.

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)

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:

ConsoleRenderable – Renderable to display log.

Return type:

ConsoleRenderable

render_message(record, message)

Render message text in to Text.

Parameters:
  • record (LogRecord) – logging Record.

  • message (str) – String containing log message.

Returns:

ConsoleRenderable – 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.

set_name(name)
stop_processing()[source]

Signals the worker thread to stop and waits for it to terminate.

This ensures that the queue is flushed of pending log records before the thread exits.

Return type:

None