refactor(logging): use subsystem loggers for discord/ws
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
import { danger } from "../../globals.js";
|
||||
import { formatDurationSeconds } from "../../infra/format-duration.js";
|
||||
import { enqueueSystemEvent } from "../../infra/system-events.js";
|
||||
import { createSubsystemLogger } from "../../logging.js";
|
||||
import { resolveAgentRoute } from "../../routing/resolve-route.js";
|
||||
import {
|
||||
normalizeDiscordSlug,
|
||||
@@ -19,7 +20,7 @@ import { formatDiscordReactionEmoji, formatDiscordUserTag } from "./format.js";
|
||||
|
||||
type LoadedConfig = ReturnType<typeof import("../../config/config.js").loadConfig>;
|
||||
type RuntimeEnv = import("../../runtime.js").RuntimeEnv;
|
||||
type Logger = ReturnType<typeof import("../../logging.js").getChildLogger>;
|
||||
type Logger = ReturnType<typeof import("../../logging.js").createSubsystemLogger>;
|
||||
|
||||
export type DiscordMessageEvent = Parameters<MessageCreateListener["handle"]>[0];
|
||||
|
||||
@@ -28,6 +29,7 @@ export type DiscordMessageHandler = (data: DiscordMessageEvent, client: Client)
|
||||
type DiscordReactionEvent = Parameters<MessageReactionAddListener["handle"]>[0];
|
||||
|
||||
const DISCORD_SLOW_LISTENER_THRESHOLD_MS = 1000;
|
||||
const discordEventQueueLog = createSubsystemLogger("discord/event-queue");
|
||||
|
||||
function logSlowDiscordListener(params: {
|
||||
logger: Logger | undefined;
|
||||
@@ -40,12 +42,15 @@ function logSlowDiscordListener(params: {
|
||||
decimals: 1,
|
||||
unit: "seconds",
|
||||
});
|
||||
const message = `[EventQueue] Slow listener detected: ${params.listener} took ${duration} for event ${params.event}`;
|
||||
if (params.logger?.warn) {
|
||||
params.logger.warn(message);
|
||||
} else {
|
||||
console.warn(message);
|
||||
}
|
||||
const message = `Slow listener detected: ${params.listener} took ${duration} for event ${params.event}`;
|
||||
const logger = params.logger ?? discordEventQueueLog;
|
||||
logger.warn("Slow listener detected", {
|
||||
listener: params.listener,
|
||||
event: params.event,
|
||||
durationMs: params.durationMs,
|
||||
duration,
|
||||
consoleMessage: message,
|
||||
});
|
||||
}
|
||||
|
||||
export function registerDiscordListener(listeners: Array<object>, listener: object) {
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
import type { ClawdbotConfig, ReplyToMode } from "../../config/config.js";
|
||||
import { loadConfig } from "../../config/config.js";
|
||||
import { danger, logVerbose, shouldLogVerbose } from "../../globals.js";
|
||||
import { getChildLogger } from "../../logging.js";
|
||||
import { createSubsystemLogger } from "../../logging.js";
|
||||
import type { RuntimeEnv } from "../../runtime.js";
|
||||
import { resolveDiscordAccount } from "../accounts.js";
|
||||
import { attachDiscordGatewayLogging } from "../gateway-logging.js";
|
||||
@@ -178,7 +178,7 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) {
|
||||
],
|
||||
);
|
||||
|
||||
const logger = getChildLogger({ module: "discord-auto-reply" });
|
||||
const logger = createSubsystemLogger("discord/monitor");
|
||||
const guildHistories = new Map<string, HistoryEntry[]>();
|
||||
let botUserId: string | undefined;
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import chalk from "chalk";
|
||||
import { isVerbose } from "../globals.js";
|
||||
import { createSubsystemLogger, shouldLogSubsystemToConsole } from "../logging.js";
|
||||
import { getDefaultRedactPatterns, redactSensitiveText } from "../logging/redact.js";
|
||||
import { shouldLogSubsystemToConsole } from "../logging.js";
|
||||
import { DEFAULT_WS_SLOW_MS, getGatewayWsLogStyle } from "./ws-logging.js";
|
||||
|
||||
const LOG_VALUE_LIMIT = 240;
|
||||
@@ -21,6 +21,7 @@ const wsInflightCompact = new Map<string, WsInflightEntry>();
|
||||
let wsLastCompactConnId: string | undefined;
|
||||
const wsInflightOptimized = new Map<string, number>();
|
||||
const wsInflightSince = new Map<string, number>();
|
||||
const wsLog = createSubsystemLogger("gateway/ws");
|
||||
|
||||
export function shortId(value: string): string {
|
||||
const s = value.trim();
|
||||
@@ -167,7 +168,7 @@ export function logWs(direction: "in" | "out", kind: string, meta?: Record<strin
|
||||
|
||||
const dirArrow = direction === "in" ? "←" : "→";
|
||||
const dirColor = direction === "in" ? chalk.greenBright : chalk.cyanBright;
|
||||
const prefix = `${chalk.gray("[gws]")} ${dirColor(dirArrow)} ${chalk.bold(kind)}`;
|
||||
const prefix = `${dirColor(dirArrow)} ${chalk.bold(kind)}`;
|
||||
|
||||
const headline =
|
||||
(kind === "req" || kind === "res") && method
|
||||
@@ -206,7 +207,7 @@ export function logWs(direction: "in" | "out", kind: string, meta?: Record<strin
|
||||
(t): t is string => Boolean(t),
|
||||
);
|
||||
|
||||
console.log(tokens.join(" "));
|
||||
wsLog.info(tokens.join(" "));
|
||||
}
|
||||
|
||||
function logWsOptimized(direction: "in" | "out", kind: string, meta?: Record<string, unknown>) {
|
||||
@@ -225,9 +226,9 @@ function logWsOptimized(direction: "in" | "out", kind: string, meta?: Record<str
|
||||
|
||||
if (kind === "parse-error") {
|
||||
const errorMsg = typeof meta?.error === "string" ? formatForLog(meta.error) : undefined;
|
||||
console.log(
|
||||
wsLog.warn(
|
||||
[
|
||||
`${chalk.gray("[gws]")} ${chalk.redBright("✗")} ${chalk.bold("parse-error")}`,
|
||||
`${chalk.redBright("✗")} ${chalk.bold("parse-error")}`,
|
||||
errorMsg ? `${chalk.dim("error")}=${errorMsg}` : undefined,
|
||||
`${chalk.dim("conn")}=${chalk.gray(shortId(connId ?? "?"))}`,
|
||||
]
|
||||
@@ -262,7 +263,7 @@ function logWsOptimized(direction: "in" | "out", kind: string, meta?: Record<str
|
||||
}
|
||||
|
||||
const tokens = [
|
||||
`${chalk.gray("[gws]")} ${chalk.yellowBright("⇄")} ${chalk.bold("res")}`,
|
||||
`${chalk.yellowBright("⇄")} ${chalk.bold("res")}`,
|
||||
statusToken,
|
||||
method ? chalk.bold(method) : undefined,
|
||||
durationToken,
|
||||
@@ -271,7 +272,7 @@ function logWsOptimized(direction: "in" | "out", kind: string, meta?: Record<str
|
||||
id ? `${chalk.dim("id")}=${chalk.gray(shortId(id))}` : undefined,
|
||||
].filter((t): t is string => Boolean(t));
|
||||
|
||||
console.log(tokens.join(" "));
|
||||
wsLog.info(tokens.join(" "));
|
||||
}
|
||||
|
||||
function logWsCompact(direction: "in" | "out", kind: string, meta?: Record<string, unknown>) {
|
||||
@@ -298,7 +299,7 @@ function logWsCompact(direction: "in" | "out", kind: string, meta?: Record<strin
|
||||
? chalk.greenBright
|
||||
: chalk.cyanBright;
|
||||
|
||||
const prefix = `${chalk.gray("[gws]")} ${arrowColor(compactArrow)} ${chalk.bold(kind)}`;
|
||||
const prefix = `${arrowColor(compactArrow)} ${chalk.bold(kind)}`;
|
||||
|
||||
const statusToken =
|
||||
kind === "res" && ok !== undefined
|
||||
@@ -346,5 +347,5 @@ function logWsCompact(direction: "in" | "out", kind: string, meta?: Record<strin
|
||||
(t): t is string => Boolean(t),
|
||||
);
|
||||
|
||||
console.log(tokens.join(" "));
|
||||
wsLog.info(tokens.join(" "));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user