style: apply oxfmt

This commit is contained in:
Peter Steinberger
2026-01-17 01:55:42 +00:00
parent 767f55b127
commit 3fb699a84b
22 changed files with 375 additions and 377 deletions

View File

@@ -2,7 +2,15 @@
name: command-logger
description: "Log all command events to a centralized audit file"
homepage: https://docs.clawd.bot/internal-hooks#command-logger
metadata: {"clawdbot":{"emoji":"📝","events":["command"],"install":[{"id":"bundled","kind":"bundled","label":"Bundled with Clawdbot"}]}}
metadata:
{
"clawdbot":
{
"emoji": "📝",
"events": ["command"],
"install": [{ "id": "bundled", "kind": "bundled", "label": "Bundled with Clawdbot" }],
},
}
---
# Command Logger Hook
@@ -44,6 +52,7 @@ No requirements - this hook works out of the box on all platforms.
## Configuration
No configuration needed. The hook automatically:
- Creates the log directory if it doesn't exist
- Appends to the log file (doesn't overwrite)
- Handles errors silently without disrupting command execution
@@ -75,6 +84,7 @@ Or via config:
The hook does not automatically rotate logs. To manage log size, you can:
1. **Manual rotation**:
```bash
mv ~/.clawdbot/logs/commands.log ~/.clawdbot/logs/commands.log.old
```
@@ -94,16 +104,19 @@ The hook does not automatically rotate logs. To manage log size, you can:
## Viewing Logs
View recent commands:
```bash
tail -n 20 ~/.clawdbot/logs/commands.log
```
Pretty-print with jq:
```bash
cat ~/.clawdbot/logs/commands.log | jq .
```
Filter by action:
```bash
grep '"action":"new"' ~/.clawdbot/logs/commands.log | jq .
```

View File

@@ -23,40 +23,41 @@
* ```
*/
import fs from 'node:fs/promises';
import path from 'node:path';
import os from 'node:os';
import type { InternalHookHandler } from '../../internal-hooks.js';
import fs from "node:fs/promises";
import path from "node:path";
import os from "node:os";
import type { InternalHookHandler } from "../../internal-hooks.js";
/**
* Log all command events to a file
*/
const logCommand: InternalHookHandler = async (event) => {
// Only trigger on command events
if (event.type !== 'command') {
if (event.type !== "command") {
return;
}
try {
// Create log directory
const logDir = path.join(os.homedir(), '.clawdbot', 'logs');
const logDir = path.join(os.homedir(), ".clawdbot", "logs");
await fs.mkdir(logDir, { recursive: true });
// Append to command log file
const logFile = path.join(logDir, 'commands.log');
const logLine = JSON.stringify({
timestamp: event.timestamp.toISOString(),
action: event.action,
sessionKey: event.sessionKey,
senderId: event.context.senderId ?? 'unknown',
source: event.context.commandSource ?? 'unknown',
}) + '\n';
const logFile = path.join(logDir, "commands.log");
const logLine =
JSON.stringify({
timestamp: event.timestamp.toISOString(),
action: event.action,
sessionKey: event.sessionKey,
senderId: event.context.senderId ?? "unknown",
source: event.context.commandSource ?? "unknown",
}) + "\n";
await fs.appendFile(logFile, logLine, 'utf-8');
await fs.appendFile(logFile, logLine, "utf-8");
} catch (err) {
console.error(
'[command-logger] Failed to log command:',
err instanceof Error ? err.message : String(err)
"[command-logger] Failed to log command:",
err instanceof Error ? err.message : String(err),
);
}
};