Organize project: Move old code and documentation to oldcode folder, add comprehensive README
This commit is contained in:
99
logger_module.py
Normal file
99
logger_module.py
Normal file
@@ -0,0 +1,99 @@
|
||||
"""
|
||||
Logging utilities for Prezenta Work
|
||||
Handles both local file logging and remote server notifications
|
||||
"""
|
||||
|
||||
import logging
|
||||
import os
|
||||
from datetime import datetime, timedelta
|
||||
import requests
|
||||
from config_settings import LOG_FILENAME, LOG_FORMAT, LOG_RETENTION_DAYS, MONITORING_SERVER_URL, REQUEST_TIMEOUT
|
||||
|
||||
|
||||
def setup_logging():
|
||||
"""Configure the logging system"""
|
||||
logging.basicConfig(
|
||||
filename=LOG_FILENAME,
|
||||
level=logging.INFO,
|
||||
format=LOG_FORMAT
|
||||
)
|
||||
return logging.getLogger(__name__)
|
||||
|
||||
|
||||
def read_masa_name():
|
||||
"""
|
||||
Read the table/room name (idmasa) from file
|
||||
Returns 'unknown' if file not found
|
||||
"""
|
||||
from config_settings import ID_MASA_FILE
|
||||
try:
|
||||
with open(ID_MASA_FILE, "r") as file:
|
||||
n_masa = file.readline().strip()
|
||||
return n_masa if n_masa else "unknown"
|
||||
except FileNotFoundError:
|
||||
logging.error(f"File {ID_MASA_FILE} not found.")
|
||||
return "unknown"
|
||||
|
||||
|
||||
def send_log_to_server(log_message, n_masa, hostname, device_ip):
|
||||
"""
|
||||
Send log message to remote monitoring server
|
||||
|
||||
Args:
|
||||
log_message: The message to send
|
||||
n_masa: Table/room name
|
||||
hostname: Device hostname
|
||||
device_ip: Device IP address
|
||||
"""
|
||||
try:
|
||||
log_data = {
|
||||
"hostname": str(hostname),
|
||||
"device_ip": str(device_ip),
|
||||
"nume_masa": str(n_masa),
|
||||
"log_message": str(log_message)
|
||||
}
|
||||
|
||||
print(log_data) # Debugging
|
||||
response = requests.post(MONITORING_SERVER_URL, json=log_data, timeout=REQUEST_TIMEOUT)
|
||||
response.raise_for_status()
|
||||
logging.info(f"Log successfully sent to server: {log_message}")
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
logging.error(f"Failed to send log to server: {e}")
|
||||
|
||||
|
||||
def log_with_server(message, hostname, device_ip):
|
||||
"""
|
||||
Log message locally and send to remote server
|
||||
|
||||
Args:
|
||||
message: The message to log
|
||||
hostname: Device hostname
|
||||
device_ip: Device IP address
|
||||
"""
|
||||
n_masa = read_masa_name()
|
||||
formatted_message = f"{message} (n_masa: {n_masa})"
|
||||
logging.info(formatted_message)
|
||||
send_log_to_server(message, n_masa, hostname, device_ip)
|
||||
|
||||
|
||||
def delete_old_logs():
|
||||
"""Delete log files older than LOG_RETENTION_DAYS"""
|
||||
from config_settings import LOG_FILE
|
||||
|
||||
if os.path.exists(LOG_FILE):
|
||||
file_mod_time = datetime.fromtimestamp(os.path.getmtime(LOG_FILE))
|
||||
if datetime.now() - file_mod_time > timedelta(days=LOG_RETENTION_DAYS):
|
||||
try:
|
||||
os.remove(LOG_FILE)
|
||||
logging.info(f"Deleted old log file: {LOG_FILE}")
|
||||
except Exception as e:
|
||||
logging.error(f"Failed to delete log file: {e}")
|
||||
else:
|
||||
logging.info(f"Log file is not older than {LOG_RETENTION_DAYS} days")
|
||||
else:
|
||||
logging.info(f"Log file does not exist: {LOG_FILE}")
|
||||
|
||||
|
||||
# Initialize logger at module load
|
||||
logger = setup_logging()
|
||||
Reference in New Issue
Block a user