|
| 1 | +# Logging Setup for Pdf2Table |
| 2 | + |
| 3 | +This document describes the logging configuration and usage for the Pdf2Table project. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The project uses a centralized logging system that follows Clean Architecture principles. The logging configuration is located in the frameworks layer (`pdf2table/frameworks/logging_config.py`) as it's an infrastructure concern. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **Centralized Configuration**: Single point for logging setup |
| 12 | +- **Multiple Output Targets**: Console and file output |
| 13 | +- **Log Rotation**: Automatic log file rotation with configurable size limits |
| 14 | +- **Multiple Log Levels**: Support for DEBUG, INFO, WARNING, ERROR, CRITICAL |
| 15 | +- **Flexible Formatting**: Simple, detailed, and JSON-style formats |
| 16 | +- **Context Managers**: Temporary log level changes |
| 17 | +- **Decorators**: Automatic function call and execution time logging |
| 18 | +- **Environment Configuration**: Configure via environment variables |
| 19 | + |
| 20 | +## Quick Start |
| 21 | + |
| 22 | +### Usage |
| 23 | + |
| 24 | +```python |
| 25 | +from pdf2table import get_logger |
| 26 | + |
| 27 | +# Get a logger for your module |
| 28 | +logger = get_logger(__name__) |
| 29 | + |
| 30 | +# Log messages |
| 31 | +logger.info("This is an info message") |
| 32 | +logger.warning("This is a warning") |
| 33 | +logger.error("This is an error") |
| 34 | +``` |
| 35 | + |
| 36 | +## Colored Console Output |
| 37 | + |
| 38 | +The logging system includes colored console output to make log levels easily distinguishable. The entire log header (timestamp, logger name, level, filename, and function) is colored according to the log level: |
| 39 | + |
| 40 | +- **DEBUG**: Cyan header |
| 41 | +- **INFO**: Green header |
| 42 | +- **WARNING**: Yellow header |
| 43 | +- **ERROR**: Red header |
| 44 | +- **CRITICAL**: Magenta header |
| 45 | + |
| 46 | +All headers are displayed in **bold** for better visibility. Only the message portion remains uncolored for readability. |
| 47 | + |
| 48 | +## Log Files |
| 49 | + |
| 50 | +By default, logs are written to the `logs/` directory in the project root: |
| 51 | + |
| 52 | +- `pdf2table.log`: Main application log (all levels) |
| 53 | +- `pdf2table_errors.log`: Error-only log (ERROR and CRITICAL levels) |
| 54 | + |
| 55 | +## Configuration Options |
| 56 | + |
| 57 | +### Log Levels |
| 58 | + |
| 59 | +- `DEBUG`: Detailed information for diagnosing problems |
| 60 | +- `INFO`: General information about program execution |
| 61 | +- `WARNING`: Something unexpected happened, but the software is still working |
| 62 | +- `ERROR`: A serious problem occurred |
| 63 | +- `CRITICAL`: A very serious error occurred |
| 64 | + |
| 65 | +### Format Types |
| 66 | + |
| 67 | +- `simple`: Basic timestamp, logger name, level, and message |
| 68 | +- `detailed`: Includes filename, line number, and function name |
| 69 | +- `json`: Pipe-separated format for easy parsing |
| 70 | + |
| 71 | +### Environment Variables |
| 72 | + |
| 73 | +You can configure logging using environment variables: |
| 74 | + |
| 75 | +```bash |
| 76 | +# Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) |
| 77 | +export PDF2TABLE_LOG_LEVEL=DEBUG |
| 78 | + |
| 79 | +# Log directory (optional, defaults to project_root/logs) |
| 80 | +export PDF2TABLE_LOG_DIR=/path/to/custom/logs |
| 81 | + |
| 82 | +# Enable/disable console output (true/false) |
| 83 | +export PDF2TABLE_CONSOLE_OUTPUT=true |
| 84 | + |
| 85 | +# Enable/disable file output (true/false) |
| 86 | +export PDF2TABLE_FILE_OUTPUT=true |
| 87 | + |
| 88 | +# Log format type (simple, detailed, json) |
| 89 | +export PDF2TABLE_LOG_FORMAT=detailed |
| 90 | + |
| 91 | +# Enable/disable colored console output (true/false) |
| 92 | +export PDF2TABLE_USE_COLORS=true |
| 93 | + |
| 94 | +# Maximum log file size in bytes (default: 10MB) |
| 95 | +export PDF2TABLE_LOG_MAX_BYTES=10485760 |
| 96 | + |
| 97 | +# Number of backup log files to keep (default: 5) |
| 98 | +export PDF2TABLE_LOG_BACKUP_COUNT=5 |
| 99 | +``` |
| 100 | + |
| 101 | +## Usage Examples |
| 102 | + |
| 103 | +### Using Decorators |
| 104 | + |
| 105 | +```python |
| 106 | +from pdf2table.frameworks.logging_config import log_function_call, log_execution_time |
| 107 | + |
| 108 | +@log_function_call(level="INFO") |
| 109 | +@log_execution_time(level="INFO") |
| 110 | +def my_function(): |
| 111 | + # Function automatically logs entry/exit and execution time |
| 112 | + pass |
| 113 | +``` |
| 114 | + |
| 115 | +### Context Managers |
| 116 | + |
| 117 | +```python |
| 118 | +from pdf2table import get_logger |
| 119 | +from pdf2table.frameworks.logging_config import LogLevel |
| 120 | + |
| 121 | +logger = get_logger(__name__) |
| 122 | + |
| 123 | +# Temporarily change log level |
| 124 | +with LogLevel(logger, "DEBUG"): |
| 125 | + logger.debug("This debug message will be shown") |
| 126 | +``` |
| 127 | + |
| 128 | +## Integration with Existing Code |
| 129 | + |
| 130 | +The logging system is automatically initialized when you import the `pdf2table` package. Existing modules can easily add logging by: |
| 131 | + |
| 132 | +1. Import the logger: `from pdf2table import get_logger` |
| 133 | +2. Create a logger instance: `logger = get_logger(__name__)` |
| 134 | +3. Use the logger: `logger.info("Message")` |
| 135 | + |
| 136 | +## Troubleshooting |
| 137 | + |
| 138 | +### Log Files Not Created |
| 139 | + |
| 140 | +- Check that the logs directory exists and is writable |
| 141 | +- Verify file_output is enabled in configuration |
| 142 | +- Check disk space |
| 143 | + |
| 144 | +### Performance Concerns |
| 145 | + |
| 146 | +- Avoid DEBUG level in production |
| 147 | +- Use log rotation to manage file sizes |
| 148 | +- Consider disabling file output for high-performance scenarios |
0 commit comments