KI-Protokoll

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/var/www/dev.campus.systemische-tools.de/.claude/settings.local.json

Erfasste Events

EventBeschreibung
UserPromptSubmitUser-Eingaben
PreToolUseTool-Aufruf (Request)
PostToolUseTool-Ergebnis (Response)
SessionStartSession-Beginn
SessionEndSession-Ende
Stop/SubagentStopAbbruch-Events

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;

Abfragen

# Letzte 10 Einträge
mariadb ki_dev -e "SELECT id, timestamp, client_name, status FROM protokoll ORDER BY id DESC LIMIT 10"

# Requests pro Tag
mariadb ki_dev -e "SELECT DATE(timestamp) as tag, COUNT(*) as anzahl FROM protokoll GROUP BY DATE(timestamp) ORDER BY tag DESC"

# Durchschnittliche Duration
mariadb ki_dev -e "SELECT AVG(duration_ms) as avg_ms FROM protokoll WHERE status = 'completed'"

Hook-Konfiguration

Datei: /var/www/dev.campus.systemische-tools.de/.claude/settings.local.json

{
  "hooks": {
    "UserPromptSubmit": [{
      "hooks": [{
        "type": "command",
        "command": "/var/www/tools/ki-protokoll/claude-hook/log_to_db.py",
        "timeout": 5
      }]
    }],
    "PreToolUse": [{
      "matcher": "",
      "hooks": [{
        "type": "command",
        "command": "/var/www/tools/ki-protokoll/claude-hook/log_to_db.py",
        "timeout": 5
      }]
    }],
    "PostToolUse": [{
      "matcher": "",
      "hooks": [{
        "type": "command",
        "command": "/var/www/tools/ki-protokoll/claude-hook/log_to_db.py",
        "timeout": 10
      }]
    }]
  }
}

Sicherheit

Abhängigkeiten

# Python-Modul
pip3 install pymysql