Formatters API
Formatter classes for formatting log records.
CustomFormatter
- class CustomFormatter(logging.Formatter)
Custom formatter with ANSI color codes for different log levels.
- Parameters:
show_background (bool) – Enable background colors.
format_template (str, optional) – Custom format string.
show_time (bool) – Include timestamp.
show_name (bool) – Include logger name.
show_pid (bool) – Include process ID.
show_level (bool) – Include log level.
show_path (bool) – Include file path.
show_icon (bool) – Enable icons.
icon_first (bool) – Place icon before timestamp.
lexer (str, optional) – Pygments lexer for highlighting.
use_colors (bool) – Enable ANSI colors.
- format(record) str
Format the log record with colors.
- Parameters:
record (logging.LogRecord) – Log record to format.
- Returns:
Formatted string with ANSI codes.
- Return type:
Example:
from richcolorlog.logger import CustomFormatter formatter = CustomFormatter( show_background=True, format_template="[%(levelname)s] %(message)s" )
CustomRichFormatter
- class CustomRichFormatter(logging.Formatter)
Enhanced Rich formatter with syntax highlighting support.
- Parameters:
- LEVEL_STYLES
Dictionary mapping log levels to Rich style strings.
- format(record) str
Format log record with Rich styling.
- Parameters:
record (logging.LogRecord) – Log record to format.
- Returns:
Formatted string with Rich markup.
- Return type:
RichColorLogFormatter
- class RichColorLogFormatter(CustomRichFormatter)
Adapter formatter for compatibility with standard logging.Formatter.
- Parameters:
fmt (str, optional) – Format string with
%(log_color)sand%(reset)s.datefmt (str, optional) – Date format string.
show_background (bool) – Enable background colors.
show_time (bool) – Include timestamp.
show_name (bool) – Include logger name.
show_pid (bool) – Include process ID.
show_level (bool) – Include log level.
show_path (bool) – Include file path.
lexer (str) – Pygments lexer.
theme (str) – Pygments theme.
icon_first (bool) – Place icon before timestamp.
Example:
from richcolorlog.logger import RichColorLogFormatter formatter = RichColorLogFormatter( fmt="%(log_color)s[%(levelname)s]%(reset)s %(message)s", datefmt="%H:%M:%S" ) handler = logging.StreamHandler() handler.setFormatter(formatter)
LevelBasedFileFormatter
- class LevelBasedFileFormatter(logging.Formatter)
Formatter with different formats based on log level.
Uses detailed format for DEBUG, simpler format for INFO and above.
- info_format = "%(asctime)s - %(levelname)s - %(name)s - %(message)s (%(filename)s:%(lineno)d)"
- debug_format = "%(asctime)s - %(levelname)s - %(name)s - %(process)d - %(thread)d - %(funcName)s - %(message)s (%(pathname)s:%(lineno)d)"
- format(record) str
Format record using level-appropriate format.
- Parameters:
record (logging.LogRecord) – Log record to format.
- Returns:
Formatted string.
- Return type:
Example:
from richcolorlog.logger import LevelBasedFileFormatter formatter = LevelBasedFileFormatter() file_handler = logging.FileHandler('app.log') file_handler.setFormatter(formatter)
Formatter Color Attributes
CustomFormatter exposes these color dictionaries:
CustomFormatter.COLORS = {
'debug': "...", # ANSI code for debug
'info': "...", # ANSI code for info
'notice': "...", # ANSI code for notice
'warning': "...", # ANSI code for warning
'error': "...", # ANSI code for error
'critical': "...", # ANSI code for critical
'fatal': "...", # ANSI code for fatal
'alert': "...", # ANSI code for alert
'emergency': "...", # ANSI code for emergency
'reset': "\x1b[0m", # Reset code
}
CustomRichFormatter exposes:
CustomRichFormatter.LEVEL_STYLES = {
logging.DEBUG: "bold #FFAA00",
logging.INFO: "bold #00FFFF",
logging.WARNING: "black on #FFFF00",
logging.ERROR: "white on red",
logging.CRITICAL: "bright_white on #550000",
# ... custom levels
}