feat: configure Flask file-based logging for debug API

- Add rotating file handler to app.py for logging to /app/logs/spawner.log
- Configure max 10MB per file with 5 backup files
- Update admin_api.py debug endpoint to read from Flask log file
- Implement clear-logs functionality to truncate log file
- Update documentation with Flask log file details
- Creates log directory automatically if missing
This commit is contained in:
XPS\Micro 2026-02-01 15:51:24 +01:00
parent 5dcc731d7c
commit 7b0d48ca32
3 changed files with 52 additions and 23 deletions

View File

@ -365,33 +365,37 @@ def debug_management():
# ===== view-logs =====
if action == 'view-logs':
log_file = current_app.config.get('LOG_FILE', '/app/logs/spawner.log')
try:
import subprocess
# Lese Docker Container Logs
result = subprocess.run(
['docker', 'logs', '--tail', '100', 'spawner'],
capture_output=True,
text=True,
timeout=10
)
logs = result.stdout if result.returncode == 0 else result.stderr
with open(log_file, 'r', encoding='utf-8') as f:
lines = f.readlines()
last_100 = lines[-100:] if len(lines) > 100 else lines
return jsonify({
'action': 'view-logs',
'source': 'docker logs spawner',
'lines': len(logs.split('\n')),
'logs': logs
'source': 'Flask Log File',
'total_lines': len(lines),
'displayed_lines': len(last_100),
'logs': ''.join(last_100)
}), 200
except FileNotFoundError:
return jsonify({'error': f'Log-Datei nicht gefunden: {log_file}'}), 404
except Exception as e:
return jsonify({'error': f'Fehler beim Lesen der Logs: {str(e)}'}), 500
# ===== clear-logs =====
elif action == 'clear-logs':
log_file = current_app.config.get('LOG_FILE', '/app/logs/spawner.log')
try:
with open(log_file, 'w') as f:
f.write('')
current_app.logger.info('[DEBUG] Logs wurden gelöscht')
return jsonify({
'action': 'clear-logs',
'message': 'Docker-Logs können nicht gelöscht werden',
'info': 'Nutze stattdessen: docker-compose logs -f spawner --tail 1'
'message': 'Log-Datei wurde geleert',
'log_file': log_file
}), 200
except Exception as e:
return jsonify({'error': f'Fehler beim Löschen der Logs: {str(e)}'}), 500
# ===== delete-email =====
elif action == 'delete-email':

26
app.py
View File

@ -9,6 +9,9 @@ from api import api_bp, check_if_token_revoked
from admin_api import admin_bp
from config import Config
from container_manager import ContainerManager
import logging
from logging.handlers import RotatingFileHandler
import os
# Flask-App initialisieren
app = Flask(__name__)
@ -46,6 +49,29 @@ def invalid_token_callback(error):
def missing_token_callback(error):
return jsonify({'error': 'Authentifizierung erforderlich'}), 401
# ========================================
# Logging konfigurieren
# ========================================
log_file = app.config.get('LOG_FILE', '/app/logs/spawner.log')
log_dir = os.path.dirname(log_file)
# Erstelle Log-Verzeichnis falls nicht vorhanden
if log_dir and not os.path.exists(log_dir):
os.makedirs(log_dir, exist_ok=True)
# Rotating File Handler (max 10MB pro Datei, 5 Backups)
if log_file:
file_handler = RotatingFileHandler(
log_file,
maxBytes=10485760, # 10MB
backupCount=5
)
file_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
app.logger.addHandler(file_handler)
app.logger.setLevel(logging.INFO)
# Flask-Login initialisieren
login_manager = LoginManager()
login_manager.init_app(app)

View File

@ -592,7 +592,7 @@ curl -H "X-Debug-Token: xxx" \
"http://localhost:5000/api/admin/debug?action=view-logs"
```
Zeigt die **letzten 100 Zeilen** der Docker Container Logs (entspricht `docker logs spawner --tail 100`).
Zeigt die **letzten 100 Zeilen** der Flask Log-Datei (`/app/logs/spawner.log`). Enthält alle API-Anfragen, Fehler und Debug-Meldungen.
#### 2. Logs löschen (clear-logs)
@ -601,14 +601,13 @@ curl -H "X-Debug-Token: xxx" \
"http://localhost:5000/api/admin/debug?action=clear-logs"
```
**Docker-Logs können nicht gelöscht werden!** Diese Action ist informativ. Um Logs zu löschen, nutze stattdessen:
Löscht die Log-Datei komplett. Um Logs zu löschen, nutze auch direkt im Container:
```bash
docker-compose down
docker-compose up -d spawner
docker exec spawner rm /app/logs/spawner.log
```
Das startet den Container neu und die Logs werden geleert.
Nach dem Löschen werden neue Logs ab der nächsten Aktion geschrieben.
#### 3. User entfernen (delete-email)