Handlers API Reference¶
Handlers for the LogEverything library.
This module provides specialized log handlers for different output formats and destinations.
- class logeverything.handlers.handlers.AdaptiveColumnManager(max_name_width=30)[source]¶
Bases:
objectGlobal manager for adaptive column widths across all formatters.
This ensures consistent visual alignment across all loggers regardless of their name lengths, while maintaining responsive design that adapts when loggers are created or destroyed. Includes smart truncation to prevent terminal wrapping issues.
- get_display_name(logger_name)[source]¶
Get the display name for a logger, with smart truncation if needed.
- Parameters:
logger_name (
str) – The original logger name- Return type:
str- Returns:
The name to display, potentially truncated with ellipsis
- register_formatter(formatter)[source]¶
Register a formatter to receive column width updates.
- Return type:
None
- remove_logger_name(logger_name)[source]¶
Remove a logger name and potentially resize if it was the longest. Note: This is optional/advanced - for now we’ll keep it simple and not shrink.
- Return type:
None
- class logeverything.handlers.handlers.ConsoleHandler(stream=None, use_colors=True, color_messages=False, level=0, colored=None)[source]¶
Bases:
StreamHandlerEnhanced console handler with color support and formatting.
- COLORS = {'CRITICAL': '\x1b[95m', 'DEBUG': '\x1b[94m', 'ERROR': '\x1b[91m', 'INFO': '\x1b[92m', 'RESET': '\x1b[0m', 'WARNING': '\x1b[93m'}¶
- __init__(stream=None, use_colors=True, color_messages=False, level=0, colored=None)[source]¶
Initialize the ConsoleHandler.
- Parameters:
stream (
Optional[TextIO]) – The output stream (default: sys.stderr)use_colors (
bool) – Whether to use ANSI color codes for log level indicatorscolor_messages (
bool) – Whether to apply colors to message text (default: False)level (
Union[int,str]) – Logging level (default: logging.NOTSET)colored (
Optional[bool]) – Alias for use_colors, for API compatibility (deprecated)
- class logeverything.handlers.handlers.EnhancedConsoleHandler(stream=None, use_colors=True, color_messages=False, use_symbols=True, use_indent=True, align_columns=True, color_theme='default', ascii_only=False, level=0)[source]¶
Bases:
ConsoleHandlerConsole handler with enhanced formatting and color options.
- __init__(stream=None, use_colors=True, color_messages=False, use_symbols=True, use_indent=True, align_columns=True, color_theme='default', ascii_only=False, level=0)[source]¶
Initialize the EnhancedConsoleHandler.
- Parameters:
stream (
Optional[TextIO]) – Output streamuse_colors (
bool) – Whether to use colors for level indicatorscolor_messages (
bool) – Whether to apply colors to message text (default: False)use_symbols (
bool) – Whether to use Unicode symbolsuse_indent (
bool) – Whether to use indent visualizationalign_columns (
bool) – Whether to align columnscolor_theme (
str) – Color theme name (‘default’, ‘pastel’, ‘bold’, ‘monochrome’)ascii_only (
bool) – Whether to use only ASCII characters (no Unicode)level (
Union[int,str]) – Logging level
- class logeverything.handlers.handlers.FileHandler(filename, mode='a', encoding='utf-8', max_size=None, backup_count=5, compress=False, level=0)[source]¶
Bases:
FileHandlerEnhanced file handler with rotation support.
- __init__(filename, mode='a', encoding='utf-8', max_size=None, backup_count=5, compress=False, level=0)[source]¶
Initialize the FileHandler.
- Parameters:
filename (
str) – Path to the log filemode (
str) – File open mode (‘a’ for append, ‘w’ for write)encoding (
Optional[str]) – File encoding (defaults to ‘utf-8’ for Unicode compatibility, preventing Windows cp1252 codec errors with visual formatting)max_size (
Optional[int]) – Maximum file size in bytes before rotation (None for no rotation)backup_count (
int) – Number of backup files to keepcompress (
bool) – If True, gzip the most recent rotated file in a background thread.level (
Union[int,str]) – Logging level (default: logging.NOTSET)
- class logeverything.handlers.handlers.FormattedFileHandler(filename, mode='a', encoding='utf-8', pretty=True, use_symbols=True, use_indent=True, align_columns=True, compact_mode=True, compact_threshold=5, level=0, max_size=None, backup_count=5)[source]¶
Bases:
FileHandlerFile handler with enhanced formatting for improved readability.
- __init__(filename, mode='a', encoding='utf-8', pretty=True, use_symbols=True, use_indent=True, align_columns=True, compact_mode=True, compact_threshold=5, level=0, max_size=None, backup_count=5)[source]¶
Initialize the FormattedFileHandler.
- Parameters:
filename (
str) – Path to the log filemode (
str) – File open mode (‘a’ for append, ‘w’ for write)encoding (
Optional[str]) – File encoding (default: utf-8 for Unicode support)pretty (
bool) – Whether to use enhanced formattinguse_symbols (
bool) – Whether to use Unicode symbols for log levelsuse_indent (
bool) – Whether to use indent visualizationalign_columns (
bool) – Whether to align columnscompact_mode (
bool) – Whether to use compact mode for deep hierarchiescompact_threshold (
int) – Depth at which to start compact modelevel (
Union[int,str]) – Logging levelmax_size (
Optional[int]) – Maximum file size in bytes before rotation (None for no rotation)backup_count (
int) – Number of backup files to keep
- class logeverything.handlers.handlers.JSONHandler(filename, mode='a', encoding=None, flatten=False, indent=None, level=0)[source]¶
Bases:
HandlerLog handler that outputs logs as JSON objects.
- __init__(filename, mode='a', encoding=None, flatten=False, indent=None, level=0)[source]¶
Initialize the JSONHandler.
- Parameters:
filename (
str) – Path to the JSON log filemode (
str) – File open mode (‘a’ for append, ‘w’ for write)encoding (
Optional[str]) – File encoding (defaults to ‘utf-8’)flatten (
bool) – Whether to flatten the output (one JSON object per line)indent (
Optional[int]) – JSON indentation level (None for no pretty-printing)level (
Union[int,str]) – Logging level (default: logging.NOTSET)
- emit(record)[source]¶
Emit a log record as JSON.
- Parameters:
record (
LogRecord) – The log record to emit- Return type:
None
- file_handle: IO[str] | None¶
- class logeverything.handlers.handlers.JSONLineFormatter(include_extras=True, source=None)[source]¶
Bases:
FormatterFormatter that outputs each log record as a single JSON object.
Attach this to any handler (including rotation handlers) to produce dashboard-compatible JSON Lines output. The field layout matches
JSONHandlerso the dashboard can read the files directly.- Parameters:
include_extras (
bool) – Include extra record attributes in anextradict.source (
Optional[str]) – Optional service/source tag written into every record.
- class logeverything.handlers.handlers.PrettyFormatter(fmt=None, datefmt=None, style='%', use_colors=False, color_messages=False, use_symbols=True, use_monospace_symbols=False, use_indent=True, align_columns=True, column_widths=None, compact_mode=True, compact_threshold=5)[source]¶
Bases:
FormatterEnhanced formatter that creates visually appealing logs with Unicode symbols.
Default format string - pipe encapsulation handles level width consistently.
- COLORS = {'BOLD': '\x1b[1m', 'CRITICAL': '\x1b[95m', 'DEBUG': '\x1b[94m', 'ERROR': '\x1b[91m', 'INFO': '\x1b[92m', 'RESET': '\x1b[0m', 'UNDERLINE': '\x1b[4m', 'WARNING': '\x1b[93m'}¶
- COMPACT_SYMBOL = '⋯'¶
- COMPACT_THRESHOLD = 5¶
- CONTINUE_CHAR = '├──'¶
- DEFAULT_DATE_FORMAT = '%Y-%m-%d %H:%M:%S'¶
- DEFAULT_FORMAT = '%(asctime)s | %(levelname)s | %(name)s | %(message)s'¶
- ENTRY_CHAR = '┌─►'¶
- EXIT_CHAR = '└─◄'¶
- INDENT_CHAR = '│'¶
- LEVEL_SYMBOLS = {'CRITICAL': '🔥', 'DEBUG': '🔍', 'ERROR': '❌', 'EXCEPTION': '💥', 'INFO': 'ℹ️', 'WARNING': '⚠️'}¶
- LEVEL_SYMBOLS_MONOSPACE = {'CRITICAL': '[C]', 'DEBUG': '[D]', 'ERROR': '[E]', 'EXCEPTION': '[X]', 'INFO': '[I]', 'WARNING': '[W]'}¶
- TREE_BRANCH = '├─'¶
- TREE_INDENT = ' '¶
- TREE_LAST_BRANCH = '└─'¶
- TREE_SPACE = ' '¶
- TREE_VERTICAL = '│ '¶
- __init__(fmt=None, datefmt=None, style='%', use_colors=False, color_messages=False, use_symbols=True, use_monospace_symbols=False, use_indent=True, align_columns=True, column_widths=None, compact_mode=True, compact_threshold=5)[source]¶
Initialize the PrettyFormatter.
- Parameters:
fmt (
Optional[str]) – Format string (default: class default format)datefmt (
Optional[str]) – Date format string (default: class default date format)style (
Literal['%','{','$']) – Format string style (‘%’, ‘{’, or ‘$’)use_colors (
bool) – Whether to use ANSI color codesuse_symbols (
bool) – Whether to use Unicode symbols for log levelsuse_monospace_symbols (
bool) – Whether to use monospace symbols for perfect alignmentuse_indent (
bool) – Whether to use indent visualizationalign_columns (
bool) – Whether to align columnscolumn_widths (
Optional[Dict[str,int]]) – Custom column widths for alignmentcompact_mode (
bool) – Whether to use compact mode for deep hierarchiescompact_threshold (
int) – Depth at which to start compact mode
- format(record)[source]¶
Format the log record with enhanced hierarchical visual elements.
Modifies the record in-place and restores original values after formatting to avoid the overhead of makeLogRecord(record.__dict__) copy.
- Parameters:
record (
LogRecord) – The log record to format- Return type:
str- Returns:
Formatted log record string with improved tree structure
- class logeverything.handlers.handlers.TimedRotatingFileHandler(filename, when='midnight', interval=1, retention_days=30, compress=False, encoding='utf-8', level=0)[source]¶
Bases:
FileHandlerFile handler with time-based rotation and optional gzip compression.
Rotates log files at time boundaries (midnight, hourly, or weekly) and optionally compresses rotated files with gzip.
- Parameters:
filename (
str) – Path to the log file.when (
str) – Rotation interval type –"midnight","hourly", or"weekly".interval (
int) – Multiplier for when (currently unused, reserved for future use).retention_days (
int) – Delete rotated files older than this many days.compress (
bool) – If True, gzip rotated files in a background thread.encoding (
Optional[str]) – File encoding.level (
Union[int,str]) – Logging level.
- WHEN_MAP = {'hourly': '%Y-%m-%d-%H', 'midnight': '%Y-%m-%d', 'weekly': '%Y-%W'}¶
- doRollover()[source]¶
Perform the file rollover, compressing and cleaning up as configured.
- Return type:
None
- logeverything.handlers.handlers.get_adaptive_column_manager()[source]¶
Get the global adaptive column manager instance.
- Return type:
- logeverything.handlers.handlers.get_current_name_column_width()[source]¶
Get the current adaptive name column width.
- Return type:
int
- logeverything.handlers.handlers.get_display_name(logger_name)[source]¶
Get the display version of a logger name (potentially truncated).
- Parameters:
logger_name (
str) – The original logger name- Return type:
str- Returns:
The display name with smart truncation if needed