Debugging from user-supplied logs was impossible: a successful action left one terse, over-summarized debug line and nothing about what it sent QQ or what QQ returned. Add full-chain tracing that stays off disk so it scales to hundreds of groups. common: - New `trace` level (below debug). Never written to file — it lives only in an in-memory ring + the WebUI stream, so the high-volume full chain costs no disk. `emit` early-returns for trace before format/render (and before any lazy producer) when trace isn't active → ~free at the default level. - Dedicated O(1) circular ring for trace (default 5000, SNOWLUMA_TRACE_BUFFER), separate from the normal 1000-entry buffer (also switched off array .shift); getRecentLogs merges both by id. - request-context.ts: AsyncLocalStorage request id. The logger auto-stamps `[req#N]` on every line in a request's async chain — across packages, zero signature threading — and records it on the entry. - log-summary.renderParamsVerbose: deep render of params (nested structure, per-string cap, total budget, token/password/secret redaction). chokepoints (auto-cover every action without touching 100+ handlers): - onebot api-handler: wrap each action in a request id (only when trace is live); trace the full params on entry and status+elapsed on exit; the terse debug breadcrumb still persists to file, failures still warn. - core bridge.sendRawPacket: trace on success (cmd, sizes, elapsed); warn on errorCode!=0 / no response (cmd, code, message) so disk keeps real problems. - protocol highway: fold the bare console.log lines into the logger (trace). webui: trace as the lowest filterable level; render the `[req#N]` tag and let search match it. Server needs no change — LOG_LEVELS, getRecentLogs and the SSE stream carry it through.
119 lines
3.8 KiB
TypeScript
119 lines
3.8 KiB
TypeScript
const MAX_FIELD = 40;
|
|
const MAX_TOTAL = 200;
|
|
|
|
function valueRepr(v: unknown): string {
|
|
if (v === null) return 'null';
|
|
if (v === undefined) return 'undefined';
|
|
switch (typeof v) {
|
|
case 'string':
|
|
return v.length > MAX_FIELD ? `"${v.slice(0, MAX_FIELD)}..."` : `"${v}"`;
|
|
case 'number':
|
|
case 'boolean':
|
|
case 'bigint':
|
|
return String(v);
|
|
case 'object':
|
|
if (Array.isArray(v)) return `[len=${v.length}]`;
|
|
return '{...}';
|
|
default:
|
|
return typeof v;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Render a params object as a single line for logging. Skips deep
|
|
* traversal: nested objects collapse to `{...}`, arrays to `[len=N]`.
|
|
* Strings are quoted; long ones are truncated with an ellipsis.
|
|
*
|
|
* Output is capped at MAX_TOTAL chars; on overflow the tail is
|
|
* replaced with `...` so the next field doesn't get half-rendered.
|
|
*/
|
|
export function summarizeParams(params: unknown): string {
|
|
if (params === null || params === undefined) return '{}';
|
|
if (typeof params !== 'object') {
|
|
const s = String(params);
|
|
return s.length > MAX_TOTAL ? `${s.slice(0, MAX_TOTAL)}...` : s;
|
|
}
|
|
if (Array.isArray(params)) return `[len=${params.length}]`;
|
|
|
|
const out: string[] = [];
|
|
let total = 0;
|
|
for (const [k, v] of Object.entries(params as Record<string, unknown>)) {
|
|
const entry = `${k}=${valueRepr(v)}`;
|
|
// +1 accounts for the separating space we'd insert when joining.
|
|
if (total > 0 && total + entry.length + 1 > MAX_TOTAL) {
|
|
out.push('...');
|
|
break;
|
|
}
|
|
out.push(entry);
|
|
total += entry.length + (out.length > 1 ? 1 : 0);
|
|
}
|
|
return out.join(' ');
|
|
}
|
|
|
|
const VERBOSE_STRING_MAX = 200;
|
|
const VERBOSE_TOTAL_MAX = 1500;
|
|
// Keys whose values must never be rendered — tokens, passwords, secrets.
|
|
const REDACT_KEY = /^(.*[-_])?(token|password|passwd|secret|access[-_]?token)([-_].*)?$/i;
|
|
|
|
/**
|
|
* Deep, debug-grade render of a params object for the `trace` level: shows the
|
|
* full nested structure (message segments, nested objects) so a reproduction
|
|
* is legible — unlike {@link summarizeParams}, which collapses nested values.
|
|
*
|
|
* Three guards keep it safe and bounded:
|
|
* - per-string cap (long values like base64 image data become `"…<N B>"`),
|
|
* - total output budget (so a giant payload can't flood the buffer),
|
|
* - key-based redaction (token / password / secret → `"***"`).
|
|
*/
|
|
export function renderParamsVerbose(params: unknown): string {
|
|
const seen = new WeakSet<object>();
|
|
let budget = VERBOSE_TOTAL_MAX;
|
|
|
|
const walk = (value: unknown, key?: string): string => {
|
|
if (budget <= 0) return '…';
|
|
if (key !== undefined && REDACT_KEY.test(key)) return '"***"';
|
|
if (value === null) return 'null';
|
|
if (value === undefined) return 'undefined';
|
|
|
|
switch (typeof value) {
|
|
case 'string': {
|
|
const truncated =
|
|
value.length > VERBOSE_STRING_MAX
|
|
? `${value.slice(0, VERBOSE_STRING_MAX)}…<${value.length}B>`
|
|
: value;
|
|
const out = JSON.stringify(truncated);
|
|
budget -= out.length;
|
|
return out;
|
|
}
|
|
case 'number':
|
|
case 'boolean':
|
|
case 'bigint': {
|
|
const out = String(value);
|
|
budget -= out.length;
|
|
return out;
|
|
}
|
|
case 'object': {
|
|
if (seen.has(value as object)) return '"[circular]"';
|
|
seen.add(value as object);
|
|
const parts: string[] = [];
|
|
if (Array.isArray(value)) {
|
|
for (const item of value) {
|
|
if (budget <= 0) { parts.push('…'); break; }
|
|
parts.push(walk(item));
|
|
}
|
|
return `[${parts.join(',')}]`;
|
|
}
|
|
for (const [k, v] of Object.entries(value as Record<string, unknown>)) {
|
|
if (budget <= 0) { parts.push('…'); break; }
|
|
parts.push(`${k}:${walk(v, k)}`);
|
|
}
|
|
return `{${parts.join(',')}}`;
|
|
}
|
|
default:
|
|
return typeof value;
|
|
}
|
|
};
|
|
|
|
return walk(params);
|
|
}
|