Add Activity log Service and model
This commit is contained in:
56
app/services/activity_service.py
Normal file
56
app/services/activity_service.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
# Activity log Service
|
||||
class ActivityService:
|
||||
|
||||
@staticmethod
|
||||
def read_logs(file_name="", search="", level="", user="", from_date="", to_date=""):
|
||||
|
||||
file = Path("logs") / file_name
|
||||
|
||||
if not file.exists():
|
||||
return []
|
||||
|
||||
logs = []
|
||||
|
||||
with open(file, "r", encoding="utf-8") as f:
|
||||
|
||||
for line in reversed(f.readlines()):
|
||||
# Example:
|
||||
# 2026-08-01 10:15:55 | INFO | admin | Dashboard | Dashboard Opened
|
||||
parts = [x.strip() for x in line.split("|")]
|
||||
|
||||
if len(parts) < 5:
|
||||
continue
|
||||
|
||||
record = {
|
||||
"date": parts[0],
|
||||
"level": parts[1],
|
||||
"user": parts[2],
|
||||
"module": parts[3],
|
||||
"message": "|".join(parts[4:])
|
||||
}
|
||||
|
||||
# Search Filter
|
||||
if search and search.lower() not in line.lower():
|
||||
continue
|
||||
|
||||
# Level Filter
|
||||
if level and record["level"] != level:
|
||||
continue
|
||||
|
||||
# User Filter
|
||||
if user and user.lower() not in record["user"].lower():
|
||||
continue
|
||||
|
||||
# Date Filter
|
||||
if from_date and record["date"][:10] < from_date:
|
||||
continue
|
||||
|
||||
if to_date and record["date"][:10] > to_date:
|
||||
continue
|
||||
|
||||
logs.append(record)
|
||||
|
||||
return logs
|
||||
Reference in New Issue
Block a user