feat(logging): implement comprehensive logging infrastructure with loguru#794
Merged
feat(logging): implement comprehensive logging infrastructure with loguru#794
Conversation
Add comprehensive structured logging throughout the application with granular category-based control: - **New logging system**: - Loguru-based logging with automatic rotation, retention, and compression - Category-based control (REQUESTS, AUTH, DATABASE, EMAIL, ERRORS, ADMIN, API_KEYS) - Console and file logging with different formats - Lazy initialization to avoid circular dependencies - **Middleware**: HTTP request/response logging (uvicorn-compatible format) - **Manager logging**: - Auth: Token operations, authentication attempts, password resets - User: Registration, login, CRUD operations, role/ban changes - Email: Sending operations with success/failure tracking - API Keys: Creation, deletion, authentication events - **Configuration**: All settings configurable via .env (path, level, rotation, retention, categories) - **Documentation**: Updated .env.example with detailed logging configuration options
Reverted bootstrap code (helpers.py, main.py) to use standard logging for pytest caplog compatibility. Fixed auth.py to use try/except/else pattern for TRY300 compliance and safe user.id access with getattr for test compatibility.
… complexity warnings Created a CategoryLogger wrapper class that encapsulates the category-enabled check, removing the need for if-statements in calling code. This reduces cyclomatic complexity while maintaining the same logging functionality. All 500+ tests passing. All ruff checks passing (including removal of 4 C901 warnings).
…ture Created new test file test_logging.py with 14 tests covering: - LogCategory Flag enum operations and bitwise combinations - LogConfig category parsing (NONE, comma-separated, invalid, mixed case) - CategoryLogger wrapper methods (info, error, warning, debug) - LoggingMiddleware HTTP request logging Updated test_email_manager.py with exception handling test for simple_send. Updated pyproject.toml to ignore PLE1205 for test files (false positive). Coverage improvements: - app/config/log_config.py: 86% → 100% - app/logs.py: 90% → 100% - app/managers/email.py: 91% → 100% - app/middleware/logging_middleware.py: 94% → 100% All 515 tests passing.
Coverage summary from CodacySee diff coverage on Codacy
Coverage variation details
Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: Diff coverage details
Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: See your quality gate settings Change summary preferences |
Implemented LOG_FILENAME setting to allow customization of log file names, enabling clean separation between test and production logs. Changes: - Added log_filename setting to Settings class (default: "api.log") - Updated LogConfig to read and validate log_filename - Added path separator validation to prevent directory traversal attacks - Configured pytest to use "test_api.log" via environment variable - Updated .env.example with LOG_FILENAME documentation - Updated test mocks to include log_filename attribute Features: - Tests automatically write to ./logs/test_api.log - Production/dev writes to ./logs/api.log (default) - Customizable via LOG_FILENAME environment variable - Security: validates filename doesn't contain path separators - Follows existing settings pattern (log_path, log_level, etc.) All 515 tests passing.
Added tests to cover path separator validation in log_filename setting, improving coverage for app/config/log_config.py from 60% to 80%. Tests added: - test_log_filename_with_forward_slash_raises_error: Validates "/" rejection - test_log_filename_with_backslash_raises_error: Validates "\\" rejection Coverage improvements: - app/config/log_config.py: 60% → 80% (lines 49-53 now covered) All 517 tests passing.
…nt guide Signed-off-by: Grant Ramsay <seapagan@gmail.com>
Made loguru console logging optional and disabled by default to eliminate duplicate console output with FastAPI/Uvicorn's built-in logging. Changes: - Added log_console_enabled setting (default: False) to Settings - Modified setup_logging() to conditionally add console handler - Updated .env.example with LOG_CONSOLE_ENABLED documentation - File logging always enabled regardless of console setting Tests added: - test_console_logging_disabled_by_default: Verifies config reads False default - test_console_logging_can_be_enabled: Verifies config reads True when set - test_setup_logging_skips_console_when_disabled: Verifies console handler NOT added when disabled (default behavior) - test_setup_logging_adds_console_when_enabled: Verifies console handler IS added when enabled, confirming sys.stderr as first handler Coverage improvements: - app/config/log_config.py: Lines 87-93 now covered (conditional console handler) Updated existing test mocks to include log_console_enabled=False for consistency. All 521 tests passing.
…tation Added detailed documentation for all Admin Pages and Logging environment variables to docs/usage/configuration/environment.md. Admin Pages Configuration (5 variables): - ADMIN_PAGES_ENABLED: Enable/disable admin panel - ADMIN_PAGES_ROUTE: Customize admin panel route - ADMIN_PAGES_TITLE: Customize browser/page title - ADMIN_PAGES_ENCRYPTION_KEY: Session encryption key (with security warnings) - ADMIN_PAGES_TIMEOUT: Session timeout configuration Logging Configuration (8 variables): - LOG_PATH: Log output directory - LOG_LEVEL: Logging verbosity (DEBUG/INFO/WARNING/ERROR/CRITICAL) - LOG_ROTATION: When to rotate log files - LOG_RETENTION: How long to keep old logs - LOG_COMPRESSION: Compression format for rotated logs - LOG_CATEGORIES: Fine-grained category control (ALL/NONE/REQUESTS/AUTH/etc.) - LOG_FILENAME: Custom log filename - LOG_CONSOLE_ENABLED: Enable console output (with duplicate warning) Documentation includes: - Clear explanations for each setting - Security warnings for sensitive settings (encryption key) - Multiple practical configuration examples (dev, production, minimal) - Production recommendations for LOG_CATEGORIES - Troubleshooting section for common issues - Cross-references to admin-panel.md Added ~380 lines of comprehensive documentation following existing style.
183037f to
7468186
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implemented a comprehensive logging infrastructure using loguru with categorical logging support, replacing the basic print-based logging system.
Key Features
Changes
New Files
app/config/log_config.py- LogConfig and LogCategory Flag enumapp/logs.py- CategoryLogger wrapper classapp/middleware/logging_middleware.py- HTTP logging middlewaretests/unit/test_logging.py- 14 comprehensive tests for logging infrastructureModified Files
app/main.py- Initialize logging and add middlewareapp/managers/user.py- Add logging for user operations (register, login, delete, update, ban, role changes)app/managers/email.py- Add logging for email operationsapp/managers/auth.py- Add logging for authentication operationsapp/config/settings.py- Add logging configuration settingspyproject.toml- Add loguru dependency, disable PLE1205 for test filestests/unit/test_email_manager.py- Add exception handling testConfiguration
New environment variables:
LOG_PATH- Log file location (default:./logs)LOG_LEVEL- Minimum log level (default:INFO)LOG_ROTATION- When to rotate logs (default:1 day)LOG_RETENTION- How long to keep logs (default:30 days)LOG_COMPRESSION- Compression format (default:zip)LOG_CATEGORIES- Comma-separated categories orALL/NONE(default:ALL)Test Coverage
All logging files now have 100% coverage:
app/config/log_config.py: 86% → 100%app/logs.py: 90% → 100%app/managers/email.py: 91% → 100%app/middleware/logging_middleware.py: 94% → 100%✅ All 515 tests passing
✅ All linting and type checking passing
Example Usage
🤖 Generated with Claude Code