|
|
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
### `logging_module.py`
|
|
|
|
|
```python
|
|
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
|
import socket
|
|
|
|
|
import requests
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
|
|
# Configure logging
|
|
|
|
|
LOG_FILE_PATH = './data/log.txt'
|
|
|
|
|
logging.basicConfig(filename=LOG_FILE_PATH, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
|
|
|
|
|
|
# Function to delete old logs
|
|
|
|
|
def delete_old_logs():
|
|
|
|
|
log_dir = './data/'
|
|
|
|
|
log_file = 'log.txt'
|
|
|
|
|
log_path = os.path.join(log_dir, log_file)
|
|
|
|
|
|
|
|
|
|
if os.path.exists(log_path):
|
|
|
|
|
file_mod_time = datetime.fromtimestamp(os.path.getmtime(log_path))
|
|
|
|
|
if datetime.now() - file_mod_time > timedelta(days=10):
|
|
|
|
|
os.remove(log_path)
|
|
|
|
|
log_info_with_server(f"Deleted old log file: {log_file}")
|
|
|
|
|
else:
|
|
|
|
|
log_info_with_server(f"Log file is not older than 10 days: {log_file}")
|
|
|
|
|
else:
|
|
|
|
|
log_info_with_server(f"Log file does not exist: {log_file}")
|
|
|
|
|
|
|
|
|
|
# Function to read the name (idmasa) from the file
|
|
|
|
|
def read_name_from_file():
|
|
|
|
|
try:
|
|
|
|
|
with open("./data/idmasa.txt", "r") as file:
|
|
|
|
|
|
|
|
|
|
### How to Use the Module in Another App
|
|
|
|
|
|
|
|
|
|
1. Save the script as `logging_module.py` in a directory accessible to your other Python applications.
|
|
|
|
|
|
|
|
|
|
2. Import the module and use its functions in your other app. For example:
|
|
|
|
|
|
|
|
|
|
```python
|
|
|
|
|
# Import the logging module
|
|
|
|
|
from logging_module import log_info_with_server, delete_old_logs
|
|
|
|
|
|
|
|
|
|
# Example usage
|
|
|
|
|
log_info_with_server("This is a test log message.")
|
|
|
|
|
delete_old_logs()
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
### Explanation of the Script:
|
|
|
|
|
1. **Encapsulation**:
|
|
|
|
|
- The script encapsulates all logging-related functionality, including:
|
|
|
|
|
- Deleting old logs ([`delete_old_logs`](command:_github.copilot.openSymbolFromReferences?%5B%22%22%2C%5B%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A16%2C%22character%22%3A4%7D%7D%2C%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A68%2C%22character%22%3A0%7D%7D%2C%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A16%2C%22character%22%3A4%7D%7D%5D%2C%229495c72a-c9b2-498f-9787-1c43fb524282%22%5D "Go to definition")).
|
|
|
|
|
- Reading the [`idmasa`](command:_github.copilot.openSymbolFromReferences?%5B%22%22%2C%5B%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A31%2C%22character%22%3A29%7D%7D%2C%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A31%2C%22character%22%3A29%7D%7D%5D%2C%229495c72a-c9b2-498f-9787-1c43fb524282%22%5D "Go to definition") value from a file ([`read_name_from_file`](command:_github.copilot.openSymbolFromReferences?%5B%22%22%2C%5B%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A32%2C%22character%22%3A4%7D%7D%2C%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A32%2C%22character%22%3A4%7D%7D%5D%2C%229495c72a-c9b2-498f-9787-1c43fb524282%22%5D "Go to definition")).
|
|
|
|
|
- Sending logs to a remote server ([`send_log_to_server`](command:_github.copilot.openSymbolFromReferences?%5B%22%22%2C%5B%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A42%2C%22character%22%3A4%7D%7D%2C%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A42%2C%22character%22%3A4%7D%7D%5D%2C%229495c72a-c9b2-498f-9787-1c43fb524282%22%5D "Go to definition")).
|
|
|
|
|
- Logging messages locally and remotely ([`log_info_with_server`](command:_github.copilot.openSymbolFromReferences?%5B%22%22%2C%5B%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A60%2C%22character%22%3A4%7D%7D%2C%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A25%2C%22character%22%3A12%7D%7D%5D%2C%229495c72a-c9b2-498f-9787-1c43fb524282%22%5D "Go to definition")).
|
|
|
|
|
|
|
|
|
|
2. **Reusability**:
|
|
|
|
|
- The script is designed to be imported and reused in other Python applications.
|
|
|
|
|
|
|
|
|
|
3. **Configuration**:
|
|
|
|
|
- The log file path (`LOG_FILE_PATH`) and server URL ([`server_url`](command:_github.copilot.openSymbolFromReferences?%5B%22%22%2C%5B%7B%22uri%22%3A%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fapp.py%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%22pos%22%3A%7B%22line%22%3A52%2C%22character%22%3A8%7D%7D%5D%2C%229495c72a-c9b2-498f-9787-1c43fb524282%22%5D "Go to definition")) can be customized as needed.
|
|
|
|
|
|
|
|
|
|
4. **Error Handling**:
|
|
|
|
|
- The script includes error handling for file operations and HTTP requests.
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
### Testing the Module
|
|
|
|
|
1. Create a new Python file (e.g., `test_logging.py`) and import the module:
|
|
|
|
|
```python
|
|
|
|
|
from logging_module import log_info_with_server, delete_old_logs
|
|
|
|
|
|
|
|
|
|
# Test the logging functions
|
|
|
|
|
log_info_with_server("Testing logging from another app.")
|
|
|
|
|
delete_old_logs()
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
2. Run the `test_logging.py` script:
|
|
|
|
|
```bash
|
|
|
|
|
python3 test_logging.py
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
3. Verify that:
|
|
|
|
|
- The log message is written to [`./data/log.txt`](command:_github.copilot.openRelativePath?%5B%7B%22scheme%22%3A%22file%22%2C%22authority%22%3A%22%22%2C%22path%22%3A%22%2Fhome%2Fpi%2FDesktop%2Fprezenta%2Fdata%2Flog.txt%22%2C%22query%22%3A%22%22%2C%22fragment%22%3A%22%22%7D%2C%229495c72a-c9b2-498f-9787-1c43fb524282%22%5D "/home/pi/Desktop/prezenta/data/log.txt").
|
|
|
|
|
- The log message is sent to the remote server (if configured correctly).
|
|
|
|
|
- Old logs are deleted if they are older than 10 days.
|
|
|
|
|
|
|
|
|
|
This approach ensures that the logging functionality is modular and reusable across multiple Python applications.
|