Repository navigation
Expand file tree
/
Copy pathlogs-cli.ts
More file actions
328 lines (308 loc) 路 9.48 KB
/
Copy pathlogs-cli.ts
File metadata and controls
328 lines (308 loc) 路 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import type { Command } from "commander";
import { setTimeout as delay } from "node:timers/promises";
import { buildGatewayConnectionDetails } from "../gateway/call.js";
import { parseLogLine } from "../logging/parse-log-line.js";
import { formatLocalIsoWithOffset } from "../logging/timestamps.js";
import { formatDocsLink } from "../terminal/links.js";
import { clearActiveProgressLine } from "../terminal/progress-line.js";
import { createSafeStreamWriter } from "../terminal/stream-writer.js";
import { colorize, isRich, theme } from "../terminal/theme.js";
import { formatCliCommand } from "./command-format.js";
import { addGatewayClientOptions, callGatewayFromCli } from "./gateway-rpc.js";
type LogsTailPayload = {
file?: string;
cursor?: number;
size?: number;
lines?: string[];
truncated?: boolean;
reset?: boolean;
};
type LogsCliOptions = {
limit?: string;
maxBytes?: string;
follow?: boolean;
interval?: string;
json?: boolean;
plain?: boolean;
color?: boolean;
localTime?: boolean;
url?: string;
token?: string;
timeout?: string;
expectFinal?: boolean;
};
function parsePositiveInt(value: string | undefined, fallback: number): number {
if (!value) {
return fallback;
}
const parsed = Number.parseInt(value, 10);
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
}
async function fetchLogs(
opts: LogsCliOptions,
cursor: number | undefined,
showProgress: boolean,
): Promise<LogsTailPayload> {
const limit = parsePositiveInt(opts.limit, 200);
const maxBytes = parsePositiveInt(opts.maxBytes, 250_000);
const payload = await callGatewayFromCli(
"logs.tail",
opts,
{ cursor, limit, maxBytes },
{ progress: showProgress },
);
if (!payload || typeof payload !== "object") {
throw new Error("Unexpected logs.tail response");
}
return payload as LogsTailPayload;
}
export function formatLogTimestamp(
value?: string,
mode: "pretty" | "plain" = "plain",
localTime = false,
) {
if (!value) {
return "";
}
const parsed = new Date(value);
if (Number.isNaN(parsed.getTime())) {
return value;
}
let timeString: string;
if (localTime) {
timeString = formatLocalIsoWithOffset(parsed);
} else {
timeString = parsed.toISOString();
}
if (mode === "pretty") {
return timeString.slice(11, 19);
}
return timeString;
}
function formatLogLine(
raw: string,
opts: {
pretty: boolean;
rich: boolean;
localTime: boolean;
},
): string {
const parsed = parseLogLine(raw);
if (!parsed) {
return raw;
}
const label = parsed.subsystem ?? parsed.module ?? "";
const time = formatLogTimestamp(parsed.time, opts.pretty ? "pretty" : "plain", opts.localTime);
const level = parsed.level ?? "";
const levelLabel = level.padEnd(5).trim();
const message = parsed.message || parsed.raw;
if (!opts.pretty) {
return [time, level, label, message].filter(Boolean).join(" ").trim();
}
const timeLabel = colorize(opts.rich, theme.muted, time);
const labelValue = colorize(opts.rich, theme.accent, label);
const levelValue =
level === "error" || level === "fatal"
? colorize(opts.rich, theme.error, levelLabel)
: level === "warn"
? colorize(opts.rich, theme.warn, levelLabel)
: level === "debug" || level === "trace"
? colorize(opts.rich, theme.muted, levelLabel)
: colorize(opts.rich, theme.info, levelLabel);
const messageValue =
level === "error" || level === "fatal"
? colorize(opts.rich, theme.error, message)
: level === "warn"
? colorize(opts.rich, theme.warn, message)
: level === "debug" || level === "trace"
? colorize(opts.rich, theme.muted, message)
: colorize(opts.rich, theme.info, message);
const head = [timeLabel, levelValue, labelValue].filter(Boolean).join(" ");
return [head, messageValue].filter(Boolean).join(" ").trim();
}
function createLogWriters() {
const writer = createSafeStreamWriter({
beforeWrite: () => clearActiveProgressLine(),
onBrokenPipe: (err, stream) => {
const code = err.code ?? "EPIPE";
const target = stream === process.stdout ? "stdout" : "stderr";
const message = `openclaw logs: output ${target} closed (${code}). Stopping tail.`;
try {
clearActiveProgressLine();
process.stderr.write(`${message}\n`);
} catch {
// ignore secondary failures while reporting the broken pipe
}
},
});
return {
logLine: (text: string) => writer.writeLine(process.stdout, text),
errorLine: (text: string) => writer.writeLine(process.stderr, text),
emitJsonLine: (payload: Record<string, unknown>, toStdErr = false) =>
writer.write(toStdErr ? process.stderr : process.stdout, `${JSON.stringify(payload)}\n`),
};
}
function emitGatewayError(
err: unknown,
opts: LogsCliOptions,
mode: "json" | "text",
rich: boolean,
emitJsonLine: (payload: Record<string, unknown>, toStdErr?: boolean) => boolean,
errorLine: (text: string) => boolean,
) {
const details = buildGatewayConnectionDetails({ url: opts.url });
const message = "Gateway not reachable. Is it running and accessible?";
const hint = `Hint: run \`${formatCliCommand("openclaw doctor")}\`.`;
const errorText = err instanceof Error ? err.message : String(err);
if (mode === "json") {
if (
!emitJsonLine(
{
type: "error",
message,
error: errorText,
details,
hint,
},
true,
)
) {
return;
}
return;
}
if (!errorLine(colorize(rich, theme.error, message))) {
return;
}
if (!errorLine(details.message)) {
return;
}
errorLine(colorize(rich, theme.muted, hint));
}
export function registerLogsCli(program: Command) {
const logs = program
.command("logs")
.description("Tail gateway file logs via RPC")
.option("--limit <n>", "Max lines to return", "200")
.option("--max-bytes <n>", "Max bytes to read", "250000")
.option("--follow", "Follow log output", false)
.option("--interval <ms>", "Polling interval in ms", "1000")
.option("--json", "Emit JSON log lines", false)
.option("--plain", "Plain text output (no ANSI styling)", false)
.option("--no-color", "Disable ANSI colors")
.option("--local-time", "Display timestamps in local timezone", false)
.addHelpText(
"after",
() =>
`\n${theme.muted("Docs:")} ${formatDocsLink("/cli/logs", "docs.openclaw.ai/cli/logs")}\n`,
);
addGatewayClientOptions(logs);
logs.action(async (opts: LogsCliOptions) => {
const { logLine, errorLine, emitJsonLine } = createLogWriters();
const interval = parsePositiveInt(opts.interval, 1000);
let cursor: number | undefined;
let first = true;
const jsonMode = Boolean(opts.json);
const pretty = !jsonMode && Boolean(process.stdout.isTTY) && !opts.plain;
const rich = isRich() && opts.color !== false;
const localTime = Boolean(opts.localTime);
while (true) {
let payload: LogsTailPayload;
// Show progress spinner only on first fetch, not during follow polling
const showProgress = first && !opts.follow;
try {
payload = await fetchLogs(opts, cursor, showProgress);
} catch (err) {
emitGatewayError(err, opts, jsonMode ? "json" : "text", rich, emitJsonLine, errorLine);
process.exit(1);
return;
}
const lines = Array.isArray(payload.lines) ? payload.lines : [];
if (jsonMode) {
if (first) {
if (
!emitJsonLine({
type: "meta",
file: payload.file,
cursor: payload.cursor,
size: payload.size,
})
) {
return;
}
}
for (const line of lines) {
const parsed = parseLogLine(line);
if (parsed) {
if (!emitJsonLine({ type: "log", ...parsed })) {
return;
}
} else {
if (!emitJsonLine({ type: "raw", raw: line })) {
return;
}
}
}
if (payload.truncated) {
if (
!emitJsonLine({
type: "notice",
message: "Log tail truncated (increase --max-bytes).",
})
) {
return;
}
}
if (payload.reset) {
if (
!emitJsonLine({
type: "notice",
message: "Log cursor reset (file rotated).",
})
) {
return;
}
}
} else {
if (first && payload.file) {
const prefix = pretty ? colorize(rich, theme.muted, "Log file:") : "Log file:";
if (!logLine(`${prefix} ${payload.file}`)) {
return;
}
}
for (const line of lines) {
if (
!logLine(
formatLogLine(line, {
pretty,
rich,
localTime,
}),
)
) {
return;
}
}
if (payload.truncated) {
if (!errorLine("Log tail truncated (increase --max-bytes).")) {
return;
}
}
if (payload.reset) {
if (!errorLine("Log cursor reset (file rotated).")) {
return;
}
}
}
cursor =
typeof payload.cursor === "number" && Number.isFinite(payload.cursor)
? payload.cursor
: cursor;
first = false;
if (!opts.follow) {
return;
}
await delay(interval);
}
});
}