feat: Add logs view for superusers to display and search production logs
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -93,6 +93,7 @@ urlpatterns = [
|
||||
name="account_profile_update",
|
||||
),
|
||||
path("server", views.server, name="server"),
|
||||
path("logs/", views.logs_view, name="logs_view"),
|
||||
path("people", views.people, name="people"),
|
||||
path("accounts/", views.UserListTableView.as_view(), name="accounts_list"),
|
||||
#path("accounts/", views.UserListView.as_view(), name="accounts_list"),
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import secrets
|
||||
from typing import Any, Optional
|
||||
import os
|
||||
from django.conf import settings
|
||||
|
||||
from django_tables2 import SingleTableMixin
|
||||
@@ -236,6 +237,48 @@ def account_set_supervisor(request, slug):
|
||||
return redirect(f"{next_url}?supervisor_action=created")
|
||||
|
||||
|
||||
@user_passes_test(lambda u: u.is_superuser)
|
||||
def logs_view(request):
|
||||
"""Display production logs from log.txt with search filtering.
|
||||
|
||||
Scope: superusers only.
|
||||
Functionality: reads log.txt, displays entries with optional text search filter.
|
||||
"""
|
||||
log_file_path = os.path.join(settings.BASE_DIR, "log.txt")
|
||||
entries = []
|
||||
search_query = request.GET.get("q", "").strip()
|
||||
|
||||
if os.path.exists(log_file_path):
|
||||
try:
|
||||
with open(log_file_path, "r", encoding="utf-8", errors="ignore") as f:
|
||||
lines = f.readlines()
|
||||
|
||||
for line in reversed(lines):
|
||||
line = line.rstrip()
|
||||
if not line:
|
||||
continue
|
||||
|
||||
if search_query and search_query.lower() not in line.lower():
|
||||
continue
|
||||
|
||||
entries.append(line)
|
||||
except Exception as e:
|
||||
entries = [f"Error reading log file: {str(e)}"]
|
||||
else:
|
||||
entries = [f"Log file not found at {log_file_path}"]
|
||||
|
||||
return render(
|
||||
request,
|
||||
"logs_viewer.html",
|
||||
{
|
||||
"entries": entries[:500],
|
||||
"total_entries": len(entries),
|
||||
"search_query": search_query,
|
||||
"log_file_path": log_file_path,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def cid_selector(request):
|
||||
return render(
|
||||
request,
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block content %}
|
||||
<div class="container-fluid py-4">
|
||||
<div class="d-flex justify-content-between align-items-start gap-3 mb-4">
|
||||
<div>
|
||||
<h2 class="mb-1">Production Logs</h2>
|
||||
<p class="text-muted small mb-0">
|
||||
Real-time log monitoring from <code>{{ log_file_path }}</code>
|
||||
</p>
|
||||
</div>
|
||||
<div class="text-end">
|
||||
<span class="badge bg-secondary rounded-pill px-3 py-2">
|
||||
Showing {{ entries|length }}/{{ total_entries }} entries
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card border-0 shadow-sm mb-4">
|
||||
<div class="card-body">
|
||||
<form method="get" class="row g-2">
|
||||
<div class="col-12 col-md-8">
|
||||
<input type="search"
|
||||
name="q"
|
||||
class="form-control"
|
||||
placeholder="Search logs by keyword (error, warning, etc)…"
|
||||
value="{{ search_query }}">
|
||||
</div>
|
||||
<div class="col-12 col-md-auto">
|
||||
<button type="submit" class="btn btn-primary">Search</button>
|
||||
{% if search_query %}
|
||||
<a href="{% url 'logs_view' %}" class="btn btn-outline-secondary ms-1">Clear</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% if entries %}
|
||||
<div class="card border-0 shadow-sm">
|
||||
<div class="card-body p-0">
|
||||
<div style="max-height: 70vh; overflow-y: auto;">
|
||||
<table class="table table-sm table-striped mb-0" style="font-family: monospace; font-size: 0.875rem;">
|
||||
<tbody>
|
||||
{% for entry in entries %}
|
||||
<tr>
|
||||
<td class="text-muted align-top" style="width: 30px; white-space: nowrap;">
|
||||
{{ forloop.counter }}
|
||||
</td>
|
||||
<td style="word-break: break-all; white-space: pre-wrap;">
|
||||
{% if 'ERROR' in entry %}
|
||||
<span class="badge bg-danger me-1">ERROR</span>
|
||||
{% elif 'WARNING' in entry or 'WARN' in entry %}
|
||||
<span class="badge bg-warning me-1">WARN</span>
|
||||
{% elif 'DEBUG' in entry %}
|
||||
<span class="badge bg-secondary me-1">DEBUG</span>
|
||||
{% elif 'INFO' in entry %}
|
||||
<span class="badge bg-info me-1">INFO</span>
|
||||
{% endif %}
|
||||
{{ entry }}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="alert alert-secondary mt-3 small">
|
||||
<i class="bi bi-info-circle"></i>
|
||||
Showing most recent entries first. Filtered display limited to 500 entries.
|
||||
{% if search_query %}
|
||||
Search found {{ entries|length }} matching entr{{ entries|length|pluralize:"y,ies" }}.
|
||||
{% endif %}
|
||||
</div>
|
||||
{% else %}
|
||||
<div class="alert alert-info">
|
||||
No log entries found.
|
||||
{% if search_query %}
|
||||
Try a different search term.
|
||||
{% else %}
|
||||
The log file may be empty or not yet created.
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user