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.

Datenbankki_dev
Tabelleprotokoll
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

EventBeschreibungHooks
UserPromptSubmitUser-Eingabenlog_to_db.py
PreToolUseTool-Aufruf (Request)log_to_db.py + spezifische Matcher
PostToolUseTool-Ergebnis (Response)log_to_db.py + spezifische Matcher
SessionStartSession-Beginnlog_to_db.py
SessionEndSession-Endelog_to_db.py
StopAbbruchlog_to_db.py
SubagentStopSubagent-Abbruchlog_to_db.py
NotificationBenachrichtigungenlog_to_db.py

Hook-Scripts

ScriptZweckEvents/Matcher
log_to_db.pyLogging in protokoll-TabelleAlle Events
block_direct_db.pyBlockiert mysql/mariadb CLIPreToolUse:Bash
block_direct_task_db.pyBlockiert direkte Task-DB-ZugriffePreToolUse:Bash
block_password_exposure.pyBlockiert Passwort-ExpositionPreToolUse:Bash
file_backup_hook.pyBackup vor DateiänderungenPreToolUse:Edit|Write
hook_dispatcher.pyContract-ValidierungPreToolUse:Write, PostToolUse:Write|Edit
task_completion_guard.pyTask-Completion-PrüfungPreToolUse: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:

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

Verwandte Themen