Skip to content

Diagnostics Logging

Structured logging utilities.

Logging Module

Logging utilities with file, console, structured, and contextual output.

This submodule provides various logging configurations for different use cases: file logging with rotation, console logging with colors, structured JSON logging, and context-aware logging.

Examples

from rite.diagnostics.logging import ( ... logging_to_file, ... logging_to_console, ... logging_structured ... ) file_logger = logging_to_file("app", "app.log") console_logger = logging_to_console("debug", colorize=True) struct_logger = logging_structured("api")

Modules

logging_structured

Structured Logger

Logger with structured output (JSON, key-value pairs).

Examples

from rite.diagnostics.logging import logging_structured logger = logging_structured("app") logger.info("User logged in", user_id=123, ip="192.168.1.1")

Functions

logging_structured(name: str, level: int = logging.INFO, json_format: bool = True) -> logging.Logger

Create logger with structured output.

Parameters:

Name Type Description Default
name str

Logger name.

required
level int

Logging level (default INFO).

INFO
json_format bool

If True, output as JSON; else key=value format.

True

Returns:

Type Description
Logger

Configured logger with structured formatter.

Examples:

>>> logger = logging_structured("app")
>>> logger.info("Event occurred", user_id=123)
>>> logger = logging_structured("api", json_format=False)
>>> logger.warning("Slow query", duration_ms=500)

logging_to_console

Console Logger

Create logger that writes to console/stdout.

Examples

from rite.diagnostics.logging import logging_to_console logger = logging_to_console("app") logger.info("Application started")

Functions

logging_to_console(name: str, level: int = logging.INFO, format_string: str | None = None, colorize: bool = False) -> logging.Logger

Create logger that writes to console.

Parameters:

Name Type Description Default
name str

Logger name.

required
level int

Logging level (default INFO).

INFO
format_string str | None

Custom format string.

None
colorize bool

If True, colorize output by level.

False

Returns:

Type Description
Logger

Configured console logger.

Examples:

>>> logger = logging_to_console("app")
>>> logger.info("Hello world")
>>> logger = logging_to_console("debug", level=logging.DEBUG)
>>> logger.debug("Debug message")

logging_to_file

File Logger

Create logger that writes to a file with rotation support.

Examples

from rite.diagnostics.logging import logging_to_file logger = logging_to_file("app", "app.log") logger.info("Application started")

Functions

logging_to_file(name: str, filepath: str, level: int = logging.INFO, max_bytes: int = 10485760, backup_count: int = 5, format_string: str | None = None) -> logging.Logger

Create logger that writes to a file with rotation.

Parameters:

Name Type Description Default
name str

Logger name.

required
filepath str

Path to log file.

required
level int

Logging level (default INFO).

INFO
max_bytes int

Max file size before rotation (default 10MB).

10485760
backup_count int

Number of backup files to keep (default 5).

5
format_string str | None

Custom format string.

None

Returns:

Type Description
Logger

Configured file logger.

Examples:

>>> logger = logging_to_file("app", "logs/app.log")
>>> logger.info("Starting application")
>>> logger = logging_to_file(
...     "api",
...     "logs/api.log",
...     max_bytes=5242880,
...     backup_count=3
... )

logging_with_context

Context Logger

Logger that includes contextual information in all messages.

Examples

from rite.diagnostics.logging import logging_with_context logger = logging_with_context("app", request_id="abc123") logger.info("Processing request")

Functions

logging_with_context(name: str, level: int = logging.INFO, **context: Any) -> logging.LoggerAdapter

Create logger that includes context in all messages.

Parameters:

Name Type Description Default
name str

Logger name.

required
level int

Logging level (default INFO).

INFO
**context Any

Context key-value pairs to include.

{}

Returns:

Type Description
LoggerAdapter

Logger adapter with context.

Examples:

>>> logger = logging_with_context(
...     "api",
...     request_id="req123",
...     user_id=456
... )
>>> logger.info("Request processed")
>>> logger = logging_with_context(
...     "worker",
...     worker_id="w1",
...     queue="default"
... )

options: show_root_heading: true show_source: false heading_level: 2