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:
parent
5dcc731d7c
commit
7b0d48ca32
40
admin_api.py
40
admin_api.py
|
|
@ -365,33 +365,37 @@ def debug_management():
|
||||||
|
|
||||||
# ===== view-logs =====
|
# ===== view-logs =====
|
||||||
if action == 'view-logs':
|
if action == 'view-logs':
|
||||||
|
log_file = current_app.config.get('LOG_FILE', '/app/logs/spawner.log')
|
||||||
try:
|
try:
|
||||||
import subprocess
|
with open(log_file, 'r', encoding='utf-8') as f:
|
||||||
# Lese Docker Container Logs
|
lines = f.readlines()
|
||||||
result = subprocess.run(
|
last_100 = lines[-100:] if len(lines) > 100 else lines
|
||||||
['docker', 'logs', '--tail', '100', 'spawner'],
|
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
timeout=10
|
|
||||||
)
|
|
||||||
logs = result.stdout if result.returncode == 0 else result.stderr
|
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
'action': 'view-logs',
|
'action': 'view-logs',
|
||||||
'source': 'docker logs spawner',
|
'source': 'Flask Log File',
|
||||||
'lines': len(logs.split('\n')),
|
'total_lines': len(lines),
|
||||||
'logs': logs
|
'displayed_lines': len(last_100),
|
||||||
|
'logs': ''.join(last_100)
|
||||||
}), 200
|
}), 200
|
||||||
|
except FileNotFoundError:
|
||||||
|
return jsonify({'error': f'Log-Datei nicht gefunden: {log_file}'}), 404
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify({'error': f'Fehler beim Lesen der Logs: {str(e)}'}), 500
|
return jsonify({'error': f'Fehler beim Lesen der Logs: {str(e)}'}), 500
|
||||||
|
|
||||||
# ===== clear-logs =====
|
# ===== clear-logs =====
|
||||||
elif action == 'clear-logs':
|
elif action == 'clear-logs':
|
||||||
return jsonify({
|
log_file = current_app.config.get('LOG_FILE', '/app/logs/spawner.log')
|
||||||
'action': 'clear-logs',
|
try:
|
||||||
'message': 'Docker-Logs können nicht gelöscht werden',
|
with open(log_file, 'w') as f:
|
||||||
'info': 'Nutze stattdessen: docker-compose logs -f spawner --tail 1'
|
f.write('')
|
||||||
}), 200
|
current_app.logger.info('[DEBUG] Logs wurden gelöscht')
|
||||||
|
return jsonify({
|
||||||
|
'action': 'clear-logs',
|
||||||
|
'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 =====
|
# ===== delete-email =====
|
||||||
elif action == 'delete-email':
|
elif action == 'delete-email':
|
||||||
|
|
|
||||||
26
app.py
26
app.py
|
|
@ -9,6 +9,9 @@ from api import api_bp, check_if_token_revoked
|
||||||
from admin_api import admin_bp
|
from admin_api import admin_bp
|
||||||
from config import Config
|
from config import Config
|
||||||
from container_manager import ContainerManager
|
from container_manager import ContainerManager
|
||||||
|
import logging
|
||||||
|
from logging.handlers import RotatingFileHandler
|
||||||
|
import os
|
||||||
|
|
||||||
# Flask-App initialisieren
|
# Flask-App initialisieren
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
@ -46,6 +49,29 @@ def invalid_token_callback(error):
|
||||||
def missing_token_callback(error):
|
def missing_token_callback(error):
|
||||||
return jsonify({'error': 'Authentifizierung erforderlich'}), 401
|
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
|
# Flask-Login initialisieren
|
||||||
login_manager = LoginManager()
|
login_manager = LoginManager()
|
||||||
login_manager.init_app(app)
|
login_manager.init_app(app)
|
||||||
|
|
|
||||||
|
|
@ -592,7 +592,7 @@ curl -H "X-Debug-Token: xxx" \
|
||||||
"http://localhost:5000/api/admin/debug?action=view-logs"
|
"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)
|
#### 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"
|
"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
|
```bash
|
||||||
docker-compose down
|
docker exec spawner rm /app/logs/spawner.log
|
||||||
docker-compose up -d spawner
|
|
||||||
```
|
```
|
||||||
|
|
||||||
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)
|
#### 3. User entfernen (delete-email)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user