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.

  • level (str or int) – Minimum logging level.

  • 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:

logging.Logger

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.

  • level (str or int) – Minimum logging level.

  • 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:

logging.Logger

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:
  • name (str, optional) – Logger name.

  • show_icon (bool) – Show emoji icons.

  • icon_first (bool) – Place icon before timestamp.

  • show_background (bool) – Enable background colors.

  • level (int) – Logging level.

Returns:

Simple configured logger.

Return type:

logging.Logger

Example:

from richcolorlog import getLoggerSimple

# In Jupyter notebook
logger = getLoggerSimple('notebook')
logger.info("Works in Jupyter!")

get_def

get_def() str

Get current function/class definition name for logging context.

Returns:

Name of current function or class.

Return type:

str

Example:

from richcolorlog import get_def

def my_function():
    logger.info(f"{get_def()}: Starting operation")

# Output: my_function: Starting operation

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