Utilities API

Helper classes and functions.

Icon

class Icon

Emoji icon mappings for different log levels.

debug = "🐛"
info = "🔔"
notice = "📢"
warning = "⛔"
error = "❌"
critical = "💥"
alert = "🚨"
emergency = "🆘"
fatal = "💀"

Also available as uppercase (DEBUG, INFO, etc.) and short aliases (DEB, INF, WARN, ERR, etc.).

Example:

from richcolorlog.logger import Icon

print(f"{Icon.error} An error occurred")
# Output: ❌ An error occurred

IconFilter

class IconFilter(logging.Filter)

Filter to add icons to log records.

Parameters:

icon_first (bool) – Position hint (used by handler).

filter(record) bool

Add icon attribute to record based on level.

Parameters:

record (logging.LogRecord) – Log record.

Returns:

Always True (passes all records).

Return type:

bool

Example:

from richcolorlog.logger import IconFilter

handler.addFilter(IconFilter(icon_first=True))

PerformanceTracker

class PerformanceTracker

Track performance metrics for logging operations.

record(operation: str, duration: float)

Record a performance metric.

Parameters:
  • operation (str) – Operation name.

  • duration (float) – Duration in seconds.

get_stats() dict

Get performance statistics.

Returns:

Statistics by operation.

Return type:

dict

Example:

from richcolorlog.logger import _performance

stats = _performance.get_stats()
# {'format': {'count': 100, 'avg': 0.0001, 'min': 0.00005, 'max': 0.001}}

performance_monitor

performance_monitor(func)

Decorator to monitor function performance.

Parameters:

func (callable) – Function to monitor.

Returns:

Wrapped function.

Return type:

callable

Example:

from richcolorlog.logger import performance_monitor

@performance_monitor
def my_function():
    # ... do work
    pass

# Stats recorded in global _performance tracker

Syslog Mappings

SYSLOG_SEVERITY_MAP

Mapping from log levels to RFC 5424 syslog severities.

SYSLOG_SEVERITY_MAP = {
    EMERGENCY_LEVEL: 0,  # Emergency
    ALERT_LEVEL: 1,      # Alert
    FATAL_LEVEL: 1,      # Fatal (maps to Alert)
    CRITICAL_LEVEL: 2,   # Critical
    ERROR_LEVEL: 3,      # Error
    WARNING_LEVEL: 4,    # Warning
    NOTICE_LEVEL: 5,     # Notice
    INFO_LEVEL: 6,       # Informational
    DEBUG_LEVEL: 7,      # Debug
}
LEVEL_TO_TABLE

Mapping from log levels to database table names.

LEVEL_TO_TABLE = {
    EMERGENCY_LEVEL: "log_emergency",
    ALERT_LEVEL: "log_alert",
    FATAL_LEVEL: "log_fatal",
    CRITICAL_LEVEL: "log_critical",
    ERROR_LEVEL: "log_error",
    WARNING_LEVEL: "log_warning",
    NOTICE_LEVEL: "log_notice",
    INFO_LEVEL: "log_info",
    DEBUG_LEVEL: "log_debug",
}
LOGGING_LEVELS_LIST

All logging levels for iteration.

LOGGING_LEVELS_LIST = [
    DEBUG_LEVEL,      # 10
    INFO_LEVEL,       # 20
    NOTICE_LEVEL,     # 25
    WARNING_LEVEL,    # 30
    ERROR_LEVEL,      # 40
    logging.CRITICAL, # 50
    CRITICAL_LEVEL,   # 58
    FATAL_LEVEL,      # 55
    ALERT_LEVEL,      # 59
    EMERGENCY_LEVEL,  # 60
]

IPython Utilities

_is_ipython() bool

Check if running in IPython/Jupyter.

Returns:

True if in IPython environment.

Return type:

bool

_configure_ipython_logging()

Configure logging for IPython compatibility.

Suppresses async warnings and adjusts Rich detection.

suppress_async_warning()

Suppress async warnings in Jupyter/IPython.

Example:

from richcolorlog.logger import suppress_async_warning

suppress_async_warning()

Environment Checking

_check_logging_disabled() bool

Check if logging is disabled via environment variables.

Checks NO_LOGGING=1 and LOGGING=0.

Returns:

True if logging is disabled.

Return type:

bool

Test Functions

test()

Run comprehensive tests for the logger.

test_brokers()

Test message broker handlers.

test_lexer()

Test lexer functionality.

run_test()

Run all tests including examples.