Color Modes
RichColorLog automatically detects terminal color capabilities and adapts output accordingly.
Color Support Detection
The library uses the Check class to detect terminal capabilities:
from richcolorlog.logger import Check, ColorSupport
# Detect current terminal support
mode = Check() # Returns one of the ColorSupport values
print(f"Color support: {mode}")
Color Support Levels
Mode |
Description |
|---|---|
|
24-bit color (16.7 million colors) |
|
256 color palette |
|
8/16 basic ANSI colors |
|
No color support |
Detection Logic
The detection follows this priority:
Environment Variables
COLORTERM=truecolororCOLORTERM=24bit→ TrueColorWT_SESSIONset (Windows Terminal) → TrueColorTERMcontains256color→ 256 colorsTERMcontainscolor→ Basic colors
curses/terminfo (Unix-like systems)
Queries terminal capabilities via
curses.tigetnum("colors")
Windows Detection
Enables ANSI processing via Windows API
Windows 10+ typically supports TrueColor
TTY Check
Non-TTY output (pipes, files) defaults to no colors
Forcing Color Mode
Override automatic detection:
from richcolorlog.logger import Check, ColorSupport
# Force specific mode
mode = Check(force=ColorSupport.TRUECOLOR)
mode = Check(force=ColorSupport.COLOR_256)
mode = Check(force=ColorSupport.BASIC)
mode = Check(force=ColorSupport.NONE)
Or use environment variables:
# Force colors in pipe
FORCE_COLOR=1 python myapp.py | cat
# Disable colors
NO_COLOR=1 python myapp.py
Color Schemes
The Colors class provides color schemes for different modes:
TrueColor (24-bit)
Full RGB color support with exact color values:
from richcolorlog.logger import Colors
# With background
colors = Colors(color_type='ansi', show_background=True).check()
# Example: debug color
# "\x1b[38;2;0;0;0;48;2;255;170;0m" (black on orange)
# Without background
colors = Colors(color_type='ansi', show_background=False).check()
# Example: debug color
# "\x1b[38;2;255;170;0m" (orange foreground only)
256 Color Mode
Uses 256-color palette approximations:
colors = Colors(color_type='ansi', show_background=True).check()
# On 256-color terminal:
# debug: "\x1b[30;48;5;214m" (black on color 214)
Basic (8/16 Colors)
Falls back to standard ANSI colors:
colors = Colors(color_type='ansi', show_background=True).check()
# On basic terminal:
# debug: "\x1b[30;43m" (black on yellow)
Rich Color Format
For Rich library output:
colors = Colors(color_type='rich', show_background=True).check()
# Returns Rich-compatible style strings:
# debug: "#000000 on #FFAA00"
# error: "white on red"
Default Color Palette
With Background
Level |
TrueColor |
Description |
|---|---|---|
DEBUG |
|
Black on orange |
INFO |
|
Black on green |
NOTICE |
|
Black on cyan |
WARNING |
|
Black on yellow |
ERROR |
|
White on red |
CRITICAL |
|
White on blue |
FATAL |
|
Blue on pink |
ALERT |
|
White on dark green |
EMERGENCY |
|
White on purple |
Without Background
Level |
TrueColor |
Description |
|---|---|---|
DEBUG |
|
Orange |
INFO |
|
Green |
NOTICE |
|
Cyan |
WARNING |
|
Yellow |
ERROR |
|
Red |
CRITICAL |
|
Blue |
FATAL |
|
Pink |
ALERT |
|
Dark green |
EMERGENCY |
|
Purple |
Custom Colors
Override default colors:
from richcolorlog import setup_logging
logger = setup_logging(
# Rich format colors
debug_color='bold yellow',
info_color='bold cyan',
warning_color='black on bright_yellow',
error_color='bold white on red',
critical_color='bold white on dark_red',
notice_color='bold magenta',
alert_color='blink bold white on red',
emergency_color='bold white on bright_red',
fatal_color='bold red',
)
Or for ANSI handler:
from richcolorlog import setup_logging_custom
logger = setup_logging_custom(
# ANSI escape codes
debug_color='\x1b[93m', # Bright yellow
info_color='\x1b[96m', # Bright cyan
error_color='\x1b[91m', # Bright red
)
Windows Support
On Windows, RichColorLog enables ANSI support automatically:
from richcolorlog.logger import Check
# This is called automatically but can be called manually
Check.enable_windows_ansi()
Requirements for Windows:
Windows 10 version 1511 or later
Windows Terminal recommended for best results
Legacy cmd.exe has limited support
Testing Color Support
from richcolorlog import setup_logging
from richcolorlog.logger import Check, ColorSupport
# Check detected mode
mode = Check()
print(f"Detected color mode: {mode}")
# Test all levels
logger = setup_logging(level='DEBUG')
logger.debug("Debug message")
logger.info("Info message")
logger.notice("Notice message")
logger.warning("Warning message")
logger.error("Error message")
logger.critical("Critical message")
logger.fatal("Fatal message")
logger.alert("Alert message")
logger.emergency("Emergency message")