56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
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 |