Skip to content

Commit bff331e

Browse files
authored
Merge pull request #18 from cmarchena/feature/comprehensive-logging
feat: Add comprehensive logging for file operations in openai_service.py
2 parents a5d1172 + 70dc2a5 commit bff331e

File tree

6 files changed

+393
-49
lines changed

6 files changed

+393
-49
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ coverage.xml
5050
# Django stuff:
5151
*.log
5252

53+
# Application logs
54+
logs/
55+
5356
# Sphinx documentation
5457
docs/_build/
5558

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,68 @@ class TestNewFeature(unittest.TestCase):
203203
pass
204204
```
205205

206+
## Logging
207+
208+
The application includes comprehensive logging to improve debugging capabilities and operational visibility. Logs are written to both console and rotating log files.
209+
210+
### Log Files Location
211+
- **Directory:** `logs/`
212+
- **Main log file:** `logs/app.log`
213+
- **Rotation:** Files rotate at 10MB with up to 5 backup files
214+
215+
### Log Levels
216+
- **DEBUG:** Detailed diagnostic information (file operations, API calls, data flow)
217+
- **INFO:** General informational messages (successful operations, state changes)
218+
- **WARNING:** Warning messages for recoverable issues (fallbacks, empty files)
219+
- **ERROR:** Error messages for failures that don't stop execution
220+
- **CRITICAL:** Critical failures requiring immediate attention
221+
222+
### Configuring Log Levels
223+
Set the `LOG_LEVEL` environment variable to control logging verbosity:
224+
225+
```bash
226+
export LOG_LEVEL=DEBUG # For detailed debugging
227+
export LOG_LEVEL=INFO # For normal operation
228+
export LOG_LEVEL=WARNING # For production with minimal logs
229+
```
230+
231+
### Interpreting Common Log Messages
232+
233+
#### File Operations (generate_meet_link)
234+
- `"Attempting to read meet_links.txt"` - Starting file read operation
235+
- `"Retrieved Meet link from file: {link}"` - Successfully read link from file
236+
- `"meet_links.txt not found. Falling back to API."` - File missing, using API fallback
237+
- `"meet_links.txt is empty. Falling back to API."` - File exists but no links available
238+
239+
#### API Operations
240+
- `"Starting event details extraction from message"` - Beginning OpenAI API call for event parsing
241+
- `"Successfully extracted event details: {details}"` - Event parsing completed
242+
- `"Starting Google Calendar event scheduling"` - Beginning calendar API call
243+
- `"Successfully scheduled event: {title}"` - Calendar event created
244+
245+
#### Error Scenarios
246+
- `"OpenAI API error while extracting event details: {error}"` - OpenAI API failure
247+
- `"Google Calendar API HTTP error: {status} - {content}"` - Calendar API error
248+
- `"Error uploading file {path}: {error}"` - File upload failure
249+
250+
### Security Considerations
251+
- Sensitive data (API keys, tokens, personal information) is never logged
252+
- Meet link URLs in logs are sanitized if they contain query parameters
253+
- Log files are excluded from version control via `.gitignore`
254+
255+
### Enabling Debug Logging
256+
For troubleshooting issues, enable DEBUG logging:
257+
258+
1. Set environment variable: `export LOG_LEVEL=DEBUG`
259+
2. Restart the application
260+
3. Check `logs/app.log` for detailed diagnostic information
261+
262+
### Log Rotation
263+
Logs automatically rotate when they reach 10MB to prevent disk space issues. The rotation creates backup files:
264+
- `app.log.1` (most recent backup)
265+
- `app.log.2` (older backup)
266+
- Up to `app.log.5` (oldest backup)
267+
206268
## Contributing
207269
208270
Contributions are welcome! Please fork the repository and submit a pull request with your improvements.

app/config.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
from dotenv import load_dotenv
44
import logging
5+
from .logging_config import setup_logging
56

67

78
def load_configurations(app):
@@ -18,8 +19,5 @@ def load_configurations(app):
1819

1920

2021
def configure_logging():
21-
logging.basicConfig(
22-
level=logging.INFO,
23-
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
24-
stream=sys.stdout,
25-
)
22+
"""Configure logging using the centralized logging configuration."""
23+
setup_logging()

app/logging_config.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import logging.config
2+
import os
3+
4+
LOGGING_CONFIG = {
5+
'version': 1,
6+
'disable_existing_loggers': False,
7+
'formatters': {
8+
'standard': {
9+
'format': '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
10+
},
11+
'detailed': {
12+
'format': '%(asctime)s - %(name)s - %(levelname)s - %(funcName)s:%(lineno)d - %(message)s'
13+
},
14+
},
15+
'handlers': {
16+
'console': {
17+
'class': 'logging.StreamHandler',
18+
'level': 'INFO',
19+
'formatter': 'standard',
20+
'stream': 'ext://sys.stdout',
21+
},
22+
'file': {
23+
'class': 'logging.handlers.RotatingFileHandler',
24+
'level': 'DEBUG',
25+
'formatter': 'detailed',
26+
'filename': 'logs/app.log',
27+
'maxBytes': 10485760, # 10MB
28+
'backupCount': 5,
29+
},
30+
},
31+
'loggers': {
32+
'app.services.openai_service': {
33+
'level': 'DEBUG',
34+
'handlers': ['console', 'file'],
35+
'propagate': False,
36+
},
37+
'app': {
38+
'level': 'INFO',
39+
'handlers': ['console', 'file'],
40+
'propagate': False,
41+
},
42+
},
43+
'root': {
44+
'level': 'INFO',
45+
'handlers': ['console', 'file'],
46+
},
47+
}
48+
49+
def setup_logging():
50+
"""Setup logging configuration."""
51+
# Create logs directory if it doesn't exist
52+
os.makedirs('logs', exist_ok=True)
53+
54+
# Apply logging configuration
55+
logging.config.dictConfig(LOGGING_CONFIG)
56+
57+
# Set log level from environment variable if provided
58+
log_level = os.getenv('LOG_LEVEL', 'INFO').upper()
59+
try:
60+
numeric_level = getattr(logging, log_level)
61+
logging.getLogger().setLevel(numeric_level)
62+
logging.getLogger('app').setLevel(numeric_level)
63+
logging.getLogger('app.services.openai_service').setLevel(logging.DEBUG)
64+
except AttributeError:
65+
logging.warning(f"Invalid LOG_LEVEL '{log_level}', using INFO")
66+
logging.getLogger().setLevel(logging.INFO)

0 commit comments

Comments
 (0)