66 lines
1.9 KiB
Markdown
66 lines
1.9 KiB
Markdown
# Operation Logger API Documentation
|
|
|
|
## Overview
|
|
The `OperationLogger` class provides functionality for recording and managing operation logs, including:
|
|
- Logging API operations
|
|
- Querying operation logs with filters
|
|
- Cleaning up old logs
|
|
|
|
## Class Methods
|
|
|
|
### `log_operation(user_id, operation_type, endpoint, parameters=None, status="SUCCESS")`
|
|
Records an API operation in the database.
|
|
|
|
**Parameters:**
|
|
- `user_id` (int): ID of the user performing the operation
|
|
- `operation_type` (str): Type of operation (e.g., "CREATE", "UPDATE")
|
|
- `endpoint` (str): API endpoint accessed
|
|
- `parameters` (str, optional): Operation parameters in string format
|
|
- `status` (str): Operation status (default: "SUCCESS")
|
|
|
|
**Returns:**
|
|
- int: ID of the created log record
|
|
|
|
**Example:**
|
|
```python
|
|
logger.log_operation(123, "LOGIN", "/api/auth/login", "username=test", "SUCCESS")
|
|
```
|
|
|
|
### `get_operations(user_id=None, start_time=None, end_time=None, limit=100)`
|
|
Queries operation logs with optional filters.
|
|
|
|
**Parameters:**
|
|
- `user_id` (int, optional): Filter by user ID
|
|
- `start_time` (datetime, optional): Earliest log time
|
|
- `end_time` (datetime, optional): Latest log time
|
|
- `limit` (int): Maximum number of logs to return (default: 100)
|
|
|
|
**Returns:**
|
|
- List of log records with fields:
|
|
- id
|
|
- user_id
|
|
- operation_type
|
|
- endpoint
|
|
- parameters
|
|
- status
|
|
- created_at
|
|
|
|
### `clean_old_logs(days=30)`
|
|
Deletes logs older than specified number of days.
|
|
|
|
**Parameters:**
|
|
- `days` (int): Delete logs older than this many days (default: 30)
|
|
|
|
**Returns:**
|
|
- int: Number of logs deleted
|
|
|
|
## Error Handling
|
|
- Raises `OperationLogError` for all failures
|
|
- Detailed error logging through module logger
|
|
|
|
## Best Practices
|
|
- Log all critical operations for audit trail
|
|
- Regularly clean old logs to maintain performance
|
|
- Use appropriate status values ("SUCCESS", "FAILED", etc.)
|
|
- Include relevant parameters for debugging
|