From f2b9d58af36431053bd08cf532dae786ffb83c38 Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Tue, 3 Sep 2024 12:46:00 +0200 Subject: [PATCH] modified: app.py modified: requirements.txt new file: templates/logs.html --- app.py | 39 ++++++++++++++++++++++++++++++++++++++- requirements.txt | 3 ++- templates/logs.html | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 templates/logs.html diff --git a/app.py b/app.py index 5a79fb7..8468774 100644 --- a/app.py +++ b/app.py @@ -1,12 +1,20 @@ -from flask import Flask, render_template, redirect, url_for, request, session +from flask import Flask, render_template, redirect, url_for, request, session, send_file +from flask_socketio import SocketIO, emit import os import subprocess import psutil import mysql.connector +import threading +import tailer app = Flask(__name__) app.secret_key = os.getenv("FLASK_SECRET_KEY", "default_secret_key") +socketio = SocketIO(app) + +# Logs Directory +LOGS_DIR = "logs" + # Verwende Umgebungsvariablen aus Coolify für die Datenbankverbindung DB_HOST = os.getenv("DB_HOST") DB_PORT = os.getenv("DB_PORT") @@ -138,5 +146,34 @@ def users(): return render_template("users.html", users=users) return redirect(url_for("login")) +@app.route("/logs") +def logs(): + if "username" in session: + log_files = sorted(os.listdir(LOGS_DIR), reverse=True) + return render_template("logs.html", log_files=log_files) + return redirect(url_for("login")) + +@app.route("/logs/") +def view_log(log_file): + if "username" in session: + log_path = os.path.join(LOGS_DIR, log_file) + if os.path.exists(log_path): + return send_file(log_path) + else: + return "Log file not found", 404 + return redirect(url_for("login")) + +@socketio.on('connect') +def handle_connect(): + """Streamt Echtzeit-Logs an den Client.""" + def tail_log(): + if bot_status(): + log_file = os.path.join(LOGS_DIR, f"{datetime.now().strftime('%Y-%m-%d')}.log") + for line in tailer.follow(open(log_file)): + socketio.emit('log', {'data': line}) + + thread = threading.Thread(target=tail_log) + thread.start() + if __name__ == "__main__": app.run(host="0.0.0.0", port=5000, debug=True) diff --git a/requirements.txt b/requirements.txt index be09bb6..be91404 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,4 +12,5 @@ beautifulsoup4 pdfplumber python-dotenv flask -psutil \ No newline at end of file +psutil +flask-socketio \ No newline at end of file diff --git a/templates/logs.html b/templates/logs.html new file mode 100644 index 0000000..ac4e0bc --- /dev/null +++ b/templates/logs.html @@ -0,0 +1,38 @@ + + + + + + Bot Logs + + + + +
+

Bot Logs

+
+

Live Logs

+

+        
+
+

Log Files

+
    + {% for log_file in log_files %} +
  • + {{ log_file }} +
  • + {% endfor %} +
+
+ Back to Dashboard +
+ + +