121 lines
3.9 KiB
Python
121 lines
3.9 KiB
Python
"""
|
|
Logging utilities for server actions
|
|
"""
|
|
|
|
import datetime
|
|
import pytz
|
|
from flask import request
|
|
from flask_login import current_user
|
|
from app.extensions import db
|
|
from app.models.server_log import ServerLog
|
|
|
|
# Get local timezone
|
|
LOCAL_TZ = pytz.timezone('Europe/Bucharest') # Adjust this to your local timezone
|
|
|
|
def log_action(action, level='INFO', user_id=None, ip_address=None, user_agent=None):
|
|
"""
|
|
Log an action to the server log database
|
|
|
|
Args:
|
|
action (str): Description of the action
|
|
level (str): Log level (INFO, WARNING, ERROR, DEBUG)
|
|
user_id (int): User ID if action is user-specific
|
|
ip_address (str): IP address of the request
|
|
user_agent (str): User agent string
|
|
"""
|
|
try:
|
|
# Auto-detect user and request info if not provided
|
|
if user_id is None and hasattr(current_user, 'id') and current_user.is_authenticated:
|
|
user_id = current_user.id
|
|
|
|
if ip_address is None and request:
|
|
ip_address = request.remote_addr
|
|
|
|
if user_agent is None and request:
|
|
user_agent = request.user_agent.string
|
|
|
|
ServerLog.log(
|
|
action=action,
|
|
user_id=user_id,
|
|
ip_address=ip_address,
|
|
user_agent=user_agent,
|
|
level=level
|
|
)
|
|
print(f"[{level}] {action}")
|
|
|
|
except Exception as e:
|
|
print(f"Error logging action: {e}")
|
|
# Try to rollback and log without context
|
|
try:
|
|
db.session.rollback()
|
|
log_entry = ServerLog(action=f"ERROR: {action} (logging failed: {str(e)})", level='ERROR')
|
|
db.session.add(log_entry)
|
|
db.session.commit()
|
|
except:
|
|
pass # If this fails too, we can't do much
|
|
|
|
def get_recent_logs(limit=50):
|
|
"""
|
|
Get the most recent log entries
|
|
|
|
Args:
|
|
limit (int): Maximum number of log entries to return
|
|
|
|
Returns:
|
|
list: List of ServerLog objects
|
|
"""
|
|
return ServerLog.get_recent_logs(limit)
|
|
|
|
# Helper functions for common log actions
|
|
def log_upload(file_type, file_name, target_type, target_name):
|
|
"""Log file upload action"""
|
|
log_action(f"{file_type.upper()} file '{file_name}' uploaded for {target_type} '{target_name}'")
|
|
|
|
def log_process(file_type, file_name, target_type, target_name):
|
|
"""Log file processing action"""
|
|
log_action(f"{file_type.upper()} file '{file_name}' processed for {target_type} '{target_name}'")
|
|
|
|
def log_player_created(username, hostname):
|
|
"""Log player creation"""
|
|
log_action(f"Player '{username}' with hostname '{hostname}' was created")
|
|
|
|
def log_player_edited(username):
|
|
"""Log player edit"""
|
|
log_action(f"Player '{username}' was edited")
|
|
|
|
def log_player_deleted(username):
|
|
"""Log player deletion"""
|
|
log_action(f"Player '{username}' was deleted")
|
|
|
|
def log_group_created(name):
|
|
"""Log group creation"""
|
|
log_action(f"Group '{name}' was created")
|
|
|
|
def log_group_edited(name):
|
|
"""Log group edit"""
|
|
log_action(f"Group '{name}' was edited")
|
|
|
|
def log_group_deleted(name):
|
|
"""Log group deletion"""
|
|
log_action(f"Group '{name}' was deleted")
|
|
|
|
def log_user_created(username, role):
|
|
"""Log user creation"""
|
|
log_action(f"User '{username}' with role '{role}' was created")
|
|
|
|
def log_user_deleted(username):
|
|
"""Log user deletion"""
|
|
log_action(f"User '{username}' was deleted")
|
|
|
|
def log_content_added(file_name, target_type, target_name):
|
|
"""Log content addition"""
|
|
log_action(f"Content '{file_name}' added to {target_type} '{target_name}'")
|
|
|
|
def log_content_reordered(target_type, target_name):
|
|
"""Log content reordering"""
|
|
log_action(f"Content reordered for {target_type} '{target_name}'")
|
|
|
|
def log_content_duration_changed(file_name, new_duration):
|
|
"""Log content duration change"""
|
|
log_action(f"Content '{file_name}' duration changed to {new_duration} seconds")
|