logging ¶
Centralized Loguru Configuration for Tux Discord Bot.
This module provides clean, standardized logging setup following loguru best practices: - Single global logger configuration - Environment-based configuration - Structured logging helpers - Third-party library log interception - IDE-clickable file paths
Configuration Priority
Log level is determined in this order (highest to lowest): 1. Explicit level parameter (for testing) 2. CONFIG.LOG_LEVEL from .env file 3. CONFIG.DEBUG=1 sets DEBUG level 4. Default "INFO"
Usage
Call once at application startup:
from tux.shared.config import CONFIG
from tux.core.logging import configure_logging
configure_logging(config=CONFIG)
For debugging specific issues, override the level:
configure_logging(level="DEBUG")
Classes:
-
StructuredLogger–Helper class for structured logging with consistent context.
Functions:
-
configure_testing_logging–Configure logging specifically for testing environment.
-
configure_logging–Configure the global loguru logger for the Tux application.
-
verify_logging_interception–Verify third-party library logging configuration (debug utility).
Classes¶
_LoggingState ¶
Prevents duplicate logging configuration.
StructuredLogger ¶
Helper class for structured logging with consistent context.
Methods:
-
performance–Log performance metrics with structured context.
-
database–Log database operations with structured context.
-
api_call–Log external API calls with structured context.
Functions¶
performance staticmethod ¶
Log performance metrics with structured context.
Parameters:
-
operation(str) –Name of the operation being measured.
-
duration(float) –Duration of the operation in seconds.
-
**context(Any, default:{}) –Additional context to bind to the log entry.
Examples:
>>> StructuredLogger.performance("database_query", 0.123, query="SELECT * FROM users")
database staticmethod ¶
Log database operations with structured context.
Parameters:
-
query(str) –The SQL query being executed.
-
duration(float | None, default:None) –Query execution duration in seconds.
-
**context(Any, default:{}) –Additional context like table names, row counts, etc.
Examples:
>>> StructuredLogger.database("INSERT INTO users", duration=0.045, rows_affected=1)
api_call staticmethod ¶
api_call(
method: str,
url: str,
status: int | None = None,
duration: float | None = None,
**context: Any,
) -> None
Log external API calls with structured context.
Parameters:
-
method(str) –HTTP method (GET, POST, etc.).
-
url(str) –The API endpoint URL.
-
status(int | None, default:None) –HTTP response status code.
-
duration(float | None, default:None) –Request duration in seconds.
-
**context(Any, default:{}) –Additional context like response size, error details, etc.
Examples:
>>> StructuredLogger.api_call("GET", "https://api.github.com/user", status=200, duration=0.234)
Functions¶
configure_testing_logging ¶
configure_testing_logging() -> None
Configure logging specifically for testing environment.
This sets up logging with DEBUG level and testing-appropriate configuration. Call this once at test startup.
configure_logging ¶
configure_logging(
environment: str | None = None,
level: str | None = None,
config: Config | None = None,
) -> None
Configure the global loguru logger for the Tux application.
This is the main entry point for logging configuration. Call once at startup.
Parameters:
-
environment(str | None, default:None) –Deprecated parameter, kept for backward compatibility.
-
level(str | None, default:None) –Explicit log level override (for testing). Highest priority.
-
config(Config | None, default:None) –Config instance with LOG_LEVEL and DEBUG from .env file.
Examples:
Normal usage (respects .env configuration): >>> from tux.shared.config import CONFIG >>> configure_logging(config=CONFIG)
Override for testing: >>> configure_logging(level="DEBUG")
_configure_level_colors ¶
_configure_level_colors() -> None
Configure custom colors for each log level.
_determine_log_level ¶
Determine the log level from multiple sources.
Priority (highest to lowest): 1. Explicit level parameter 2. config.LOG_LEVEL (from .env) 3. config.DEBUG (sets DEBUG) 4. Default "INFO"
Parameters:
Returns:
-
str–The determined log level.
_add_console_handler ¶
_add_console_handler(log_level: str) -> None
Add console handler with custom formatting.
Uses a dynamic stderr sink for robustness against stream wrapping (e.g., by pytest, IDEs, cloud platforms).
Parameters:
-
log_level(str) –The minimum log level to display.
_format_record ¶
Format log record with IDE-clickable file paths and proper escaping.
For tux.* modules: Shows clickable path (src/tux/core/app.py:167) For third-party: Shows module:function (urllib3.connectionpool:_make_request:544)
Parameters:
-
record(Any) –The loguru Record object.
Returns:
-
str–Formatted log message with escaped special characters.
_get_relative_file_path ¶
_escape_format_chars ¶
_configure_third_party_logging ¶
_configure_third_party_logging() -> None
Configure logging interception for third-party libraries.
This sets up an InterceptHandler that routes standard library logging calls to loguru, maintaining proper source attribution for all logs.
Process: 1. Create InterceptHandler to bridge logging -> loguru 2. Replace root logging handler globally 3. Configure specific library loggers with InterceptHandler 4. Set appropriate minimum levels for third-party libraries
verify_logging_interception ¶
verify_logging_interception() -> None
Verify third-party library logging configuration (debug utility).
Logs the configuration of all known third-party loggers, showing which handlers are attached and their current levels.
This is automatically called when DEBUG level is active.