Custom Log Levelsο
RichColorLog extends Pythonβs standard logging with additional syslog-compatible log levels.
Level Hierarchyο
From lowest to highest severity:
Level |
Value |
Icon |
Description |
|---|---|---|---|
DEBUG |
10 |
π |
Detailed diagnostic information |
INFO |
20 |
π |
General informational messages |
NOTICE |
25 |
π’ |
Normal but significant conditions |
WARNING |
30 |
β |
Warning conditions |
ERROR |
40 |
β |
Error conditions |
CRITICAL |
50 |
π₯ |
Critical conditions |
FATAL |
55 |
π |
Fatal errors |
ALERT |
59 |
π¨ |
Action must be taken immediately |
EMERGENCY |
60 |
π |
System is unusable |
Using Custom Levelsο
All custom levels are available as logger methods:
from richcolorlog import setup_logging
logger = setup_logging()
# Standard Python levels
logger.debug("Debugging information")
logger.info("Informational message")
logger.warning("Warning message")
logger.error("Error occurred")
logger.critical("Critical error")
# Custom levels
logger.notice("Notice: User logged in")
logger.fatal("Fatal: Cannot recover from error")
logger.alert("Alert: Disk space critical")
logger.emergency("Emergency: System failure")
Level Constantsο
Import level constants for programmatic use:
from richcolorlog import (
DEBUG_LEVEL,
INFO_LEVEL,
NOTICE_LEVEL,
WARNING_LEVEL,
ERROR_LEVEL,
CRITICAL_LEVEL,
FATAL_LEVEL,
ALERT_LEVEL,
EMERGENCY_LEVEL,
)
# Use in configuration
logger = setup_logging(level=NOTICE_LEVEL)
# Check if level is enabled
if logger.isEnabledFor(ALERT_LEVEL):
logger.alert("This will be logged")
Syslog Severity Mappingο
Custom levels map to RFC 5424 syslog severities:
RichColorLog Level |
Syslog Severity |
Syslog Name |
|---|---|---|
EMERGENCY |
0 |
Emergency |
ALERT |
1 |
Alert |
FATAL |
1 |
Alert (no standard) |
CRITICAL |
2 |
Critical |
ERROR |
3 |
Error |
WARNING |
4 |
Warning |
NOTICE |
5 |
Notice |
INFO |
6 |
Informational |
DEBUG |
7 |
Debug |
This mapping ensures compatibility when using the Syslog handler.
Filtering by Levelο
Set minimum level for handlers:
from richcolorlog import setup_logging, NOTICE_LEVEL
# Console shows NOTICE and above
# File logs everything including DEBUG
logger = setup_logging(
level='DEBUG', # Logger accepts all
log_file=True,
log_file_level='DEBUG', # File gets everything
)
# The console handler level is set by 'level' parameter
Per-Handler Levelsο
Different handlers can have different levels:
logger = setup_logging(
level='DEBUG',
log_file=True,
log_file_level='DEBUG', # File: all messages
syslog=True,
syslog_level='WARNING', # Syslog: warnings and above
rabbitmq=True,
rabbitmq_level='ERROR', # RabbitMQ: errors only
)
Level-Based Table Names (Database)ο
When using the database handler, logs are stored in separate tables by level:
Level |
Table Name |
|---|---|
EMERGENCY |
|
ALERT |
|
FATAL |
|
CRITICAL |
|
ERROR |
|
WARNING |
|
NOTICE |
|
INFO |
|
DEBUG |
|
All logs are also stored in log_syslog table for combined querying.
Icon Customizationο
Icons are provided by the Icon class:
from richcolorlog.logger import Icon
# Access icons
print(Icon.debug) # π
print(Icon.info) # π
print(Icon.notice) # π’
print(Icon.warning) # β
print(Icon.error) # β
print(Icon.critical) # π₯
print(Icon.fatal) # π
print(Icon.alert) # π¨
print(Icon.emergency) # π
# Uppercase aliases also work
print(Icon.DEBUG) # π
print(Icon.EMERGENCY) # π
Best Practicesο
Use the right level for the right situation:
# DEBUG: Detailed info for debugging
logger.debug(f"Processing item {item_id} with data: {data}")
# INFO: Normal operations
logger.info(f"User {user_id} logged in successfully")
# NOTICE: Noteworthy but normal
logger.notice(f"Configuration reloaded from {config_path}")
# WARNING: Something unexpected but recoverable
logger.warning(f"Rate limit approaching: {current}/{limit}")
# ERROR: Operation failed
logger.error(f"Failed to save file: {filename}")
# CRITICAL: Serious problem
logger.critical(f"Database connection pool exhausted")
# FATAL: Unrecoverable error
logger.fatal(f"Cannot initialize required service: {service}")
# ALERT: Needs immediate attention
logger.alert(f"Security breach detected from IP: {ip}")
# EMERGENCY: System unusable
logger.emergency("System memory exhausted, shutting down")