Dokumentation » KI-System » Protokoll
KI-Protokoll
Erstellt: 2025-12-20 | Aktualisiert: 2025-12-31
Automatisches Logging-System für Claude Code Sessions. Erfasst alle Requests, Responses und Tool-Aufrufe in einer MariaDB-Datenbank via Hook-System.
Datenbank ki_dev
Tabelle protokoll
Hook-Script /var/www/tools/ki-protokoll/claude-hook/log_to_db.py
Config (User) /root/.claude/settings.json
Config (Projekt) /var/www/dev.campus.systemische-tools.de/.claude/settings.local.json
Erfasste Events
Event Beschreibung Hooks
UserPromptSubmit User-Eingaben log_to_db.py
PreToolUse Tool-Aufruf (Request) log_to_db.py + spezifische Matcher
PostToolUse Tool-Ergebnis (Response) log_to_db.py + spezifische Matcher
SessionStart Session-Beginn log_to_db.py
SessionEnd Session-Ende log_to_db.py
Stop Abbruch log_to_db.py
SubagentStop Subagent-Abbruch log_to_db.py
Notification Benachrichtigungen log_to_db.py
Hook-Scripts
Script Zweck Events/Matcher
log_to_db.py Logging in protokoll-Tabelle Alle Events
block_direct_db.py Blockiert mysql/mariadb CLI PreToolUse:Bash
block_direct_task_db.py Blockiert direkte Task-DB-Zugriffe PreToolUse:Bash
block_password_exposure.py Blockiert Passwort-Exposition PreToolUse:Bash
file_backup_hook.py Backup vor Dateiänderungen PreToolUse:Edit|Write
hook_dispatcher.py Contract-Validierung PreToolUse:Write, PostToolUse:Write|Edit
task_completion_guard.py Task-Completion-Prüfung PreToolUse:tasks_status
Datenbank-Schema
CREATE TABLE protokoll (
id bigint(20) NOT NULL AUTO_INCREMENT,
timestamp datetime(6) DEFAULT current_timestamp(6),
request_ip varchar(45) NOT NULL,
client_name varchar(255) NOT NULL,
request text NOT NULL,
request_timestamp datetime(6) NOT NULL,
response text DEFAULT NULL,
response_timestamp datetime(6) DEFAULT NULL,
duration_ms int(10) unsigned DEFAULT NULL,
tokens_input int(10) unsigned DEFAULT NULL,
tokens_output int(10) unsigned DEFAULT NULL,
tokens_total int(10) unsigned DEFAULT NULL,
model_name varchar(255) DEFAULT NULL,
status enum('pending','completed','error') DEFAULT 'pending',
error_message text DEFAULT NULL,
PRIMARY KEY (id),
KEY idx_timestamp (timestamp),
KEY idx_client_name (client_name),
KEY idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Hook-Konfiguration
Die Hooks werden in zwei Konfigurationsdateien definiert:
User-Scope: /root/.claude/settings.json - gilt für alle Projekte
Projekt-Scope: .claude/settings.local.json - projektspezifisch
Beispiel: PreToolUse mit Matchern
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{"type": "command", "command": "/var/www/scripts/hooks/block_direct_db.py"},
{"type": "command", "command": "/var/www/scripts/hooks/block_password_exposure.py"}
]
},
{
"matcher": "Edit|Write",
"hooks": [
{"type": "command", "command": "/var/www/tools/ki-protokoll/claude-hook/file_backup_hook.py", "timeout": 10}
]
},
{
"matcher": "",
"hooks": [
{"type": "command", "command": "/var/www/tools/ki-protokoll/claude-hook/log_to_db.py", "timeout": 5}
]
}
]
Abfragen
# Via MCP-DB (empfohlen)
db_select("SELECT id, timestamp, client_name, status FROM protokoll ORDER BY id DESC LIMIT 10", database="ki_dev")
# Requests pro Tag
db_select("SELECT DATE(timestamp) as tag, COUNT(*) as anzahl FROM protokoll GROUP BY DATE(timestamp) ORDER BY tag DESC", database="ki_dev")
Sicherheit
Sensible Daten (Passwörter, API-Keys, Tokens) werden automatisch mit [REDACTED] maskiert
Lange Inhalte werden auf 10.000 Zeichen gekürzt
DB-User hat entsprechende Rechte
Blocking-Hooks verhindern unsichere Operationen
Verwandte Themen