Logger API
Main functions for creating and configuring loggers.
setup_logging
- setup_logging(name=None, level='DEBUG', **kwargs) logging.Logger
Create and configure a logger with Rich formatting.
- Parameters:
name (str, optional) – Logger name. If None, configures root logger.
lexer (str, optional) – Pygments lexer for syntax highlighting.
show_locals (bool) – Show local variables in tracebacks.
show_background (bool) – Enable background colors.
render_emoji (bool) – Render emoji characters.
show_icon (bool) – Show emoji icons for levels.
icon_first (bool) – Place icon before timestamp.
exceptions (list, optional) – List of logger names to suppress.
show (bool) – Enable/disable logging output.
theme (str) – Pygments theme for syntax highlighting.
format_template (str, optional) – Custom format string.
log_file (bool) – Enable file logging.
log_file_name (str, optional) – Path to log file.
log_file_level (str or int) – Minimum level for file logging.
- Returns:
Configured logger instance.
- Return type:
Example:
from richcolorlog import setup_logging logger = setup_logging( name='myapp', level='DEBUG', show_background=True, show_icon=True, log_file=True, ) logger.info("Application started")
setup_logging_custom
- setup_logging_custom(name=__name__, level='DEBUG', **kwargs) logging.Logger
Create a logger with ANSI color formatting (no Rich dependency).
- Parameters:
name (str) – Logger name.
show_background (bool) – Enable background colors.
format_template (str, optional) – Custom format string.
show_time (bool) – Show timestamp.
show_name (bool) – Show logger name.
show_pid (bool) – Show process ID.
show_level (bool) – Show log level.
show_path (bool) – Show file path and line number.
icon_first (bool) – Place icon before timestamp.
use_colors (bool) – Enable ANSI colors.
- Returns:
Configured logger instance.
- Return type:
Example:
from richcolorlog import setup_logging_custom logger = setup_logging_custom( show_background=False, format_template="[%(levelname)s] %(message)s" )
getLogger
- getLogger(*args, **kwargs) logging.Logger
Alias for
setup_logging().Example:
from richcolorlog import getLogger logger = getLogger('myapp', level='DEBUG')
getLoggerSimple
- getLoggerSimple(name=None, show_icon=True, icon_first=False, show_background=True, level=logging.DEBUG) logging.Logger
Create a simple logger optimized for IPython/Jupyter.
- Parameters:
- Returns:
Simple configured logger.
- Return type:
Example:
from richcolorlog import getLoggerSimple # In Jupyter notebook logger = getLoggerSimple('notebook') logger.info("Works in Jupyter!")
get_def
CustomLogger
- class CustomLogger(logging.Logger)
Extended Logger class with custom level methods.
- debug(msg, *args, **kwargs)
Log DEBUG message.
- info(msg, *args, **kwargs)
Log INFO message.
- notice(msg, *args, **kwargs)
Log NOTICE message (custom level).
- warning(msg, *args, **kwargs)
Log WARNING message.
- error(msg, *args, **kwargs)
Log ERROR message.
- critical(msg, *args, **kwargs)
Log CRITICAL message.
- fatal(msg, *args, **kwargs)
Log FATAL message (custom level).
- alert(msg, *args, **kwargs)
Log ALERT message (custom level).
- emergency(msg, *args, **kwargs)
Log EMERGENCY message (custom level).
Passing lexer:
logger.debug(code, extra={'lexer': 'python'})
Level Constants
- DEBUG_LEVEL = 10
- INFO_LEVEL = 20
- NOTICE_LEVEL = 25
- WARNING_LEVEL = 30
- ERROR_LEVEL = 40
- CRITICAL_LEVEL = 58
- FATAL_LEVEL = 55
- ALERT_LEVEL = 59
- EMERGENCY_LEVEL = 60