hanaro.*#

class ConfigFilter(name='', config=None)#

Bases: Filter

Filter out unwanted logging output via configuration.

Usage#

f = ConfigFilter('my_filter', {'my_logger': {'level': 'WARNING'}})
logger.addFilter(f)

Initialize ConfigFilter.

type name:

str

param name:

A name for identifying the filter, defaults to ‘’.

type config:

Optional[dict[str, dict[str, Any]]]

param config:

Filter configuration settings, defaults to {}.

filter(record)#

Filter the specified Log Record based on configured settings.

Parameters:

record (LogRecord) – The Log Record to filter.

Return type:

bool

Returns:

True if the record should be emitted, False otherwise.

class ContextInjectionFilter(context=None, is_metadata=False, metadata_name='metadata')#

Bases: Filter

Injects context data into Log Records.

This filter can operate in two modes: as a regular context injector that sets individual attributes on each Log Record, or as a metadata injector that consolidates all context values into a single metadata attribute.

Usage#

with ContextInjectionFilter({'request_id': 'abc-123'}) as ctx:
    logger = get_logger()
    logger.info('request processing')

Initialize a ContextInjectionFilter with the specified values.

type context:

Optional[dict[str, str]]

param context:

A baseline context dictionary to be injected into affected Log Records.

type is_metadata:

bool

param is_metadata:

Indicates that the context being injected is a “metadata” context.

type metadata_name:

str

param metadata_name:

For “metadata” contexts, sets the Log Record Attribute Name of the metadata, defaults to “metadata”.

filter(record)#

Mutates record by setting context values as attributes.

If this is a “metadata” context then the values are consolidated into a single “metadata attribute” instead.

Parameters:

record (LogRecord) – The Log Record to mutate.

Return type:

bool

Returns:

True

class QueuedHandler(level=0)#

Bases: Handler

Facilitates moving log output between threaded/async contexts via a Log Queue.

An application that spawns background threads (such as asyncio event loops) eventually shows concurrency problems in logging output (partial stream writes, broken parsing in downstream systems, etc.)

QueuedHandler solves concurrency problems by collecting Log Records to a thread-safe Log Queue, accessible from a single logging context (such as the main thread of an application) where it can be safely written in a way that preserves ordering and avoids multi-threaded clobbering of log output.

Usage#

Use get_queued_logger() in background threads to get a logger that automatically uses a QueuedHandler. Drain those records on the main thread with handle_queued_log_records():

# In a background thread:
logger = hanaro.get_queued_logger()
logger.info('message from background thread')

# On the main thread:
hanaro.handle_queued_log_records()

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

emit(record)#

Emit a Log Record via the Log Queue.

Parameters:

record (LogRecord) – The Log Record to emit.

Return type:

None

static get_log_record()#

Get a Log Record from the Log Queue.

Return type:

LogRecord | None

Returns:

A Log Record, or None if no Log Record is available.

Custom Log Formatters.

class BidiFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)#

Bases: Formatter

A log formatter that applies bidirectional text display to log messages.

This formatter runs log messages through python-bidi to properly display text containing bidirectional characters (such as Arabic or Hebrew text). If python-bidi is not installed, messages are returned unmodified.

Usage#

formatter = BidiFormatter('%(message)s')
handler.setFormatter(formatter)

Initialize a BidiFormatter instance.

type fmt:

Optional[str]

param fmt:

The format string to use, defaults to None.

type datefmt:

Optional[str]

param datefmt:

The date format string to use, defaults to None.

type style:

Literal['%', '{', '$']

param style:

The style of the format string (‘%’, ‘{’, or ‘$’).

type validate:

bool

param validate:

Whether to validate the format string.

type defaults:

Optional[Mapping[str, Any]]

param defaults:

A dictionary of default values for format string keys.

format(record)#

Format the LogRecord, applying bidirectional display behavior.

Parameters:

record (LogRecord) – The LogRecord to format.

Return type:

str

Returns:

The formatted string with bidirectional text applied.