mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 16:44:33 +00:00
refactor(logging): migrate non-agent internal console calls to subsystem logger (#22964)
Merged via /review-pr -> /prepare-pr -> /merge-pr.
Prepared head SHA: b4a5b12422
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Co-authored-by: gumadeiras <5599352+gumadeiras@users.noreply.github.com>
Reviewed-by: @gumadeiras
This commit is contained in:
committed by
GitHub
parent
4ef4aa3c10
commit
2f46308d5a
@@ -1,9 +1,27 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import { withEnv } from "../test-utils/env.js";
|
||||
import { resolveTelegramAccount } from "./accounts.js";
|
||||
import { listTelegramAccountIds, resolveTelegramAccount } from "./accounts.js";
|
||||
|
||||
const { warnMock } = vi.hoisted(() => ({
|
||||
warnMock: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("../logging/subsystem.js", () => ({
|
||||
createSubsystemLogger: () => {
|
||||
const logger = {
|
||||
warn: warnMock,
|
||||
child: () => logger,
|
||||
};
|
||||
return logger;
|
||||
},
|
||||
}));
|
||||
|
||||
describe("resolveTelegramAccount", () => {
|
||||
afterEach(() => {
|
||||
warnMock.mockReset();
|
||||
});
|
||||
|
||||
it("falls back to the first configured account when accountId is omitted", () => {
|
||||
withEnv({ TELEGRAM_BOT_TOKEN: "" }, () => {
|
||||
const cfg: OpenClawConfig = {
|
||||
@@ -63,4 +81,21 @@ describe("resolveTelegramAccount", () => {
|
||||
expect(account.token).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
it("formats debug logs with inspect-style output when debug env is enabled", () => {
|
||||
withEnv({ TELEGRAM_BOT_TOKEN: "", OPENCLAW_DEBUG_TELEGRAM_ACCOUNTS: "1" }, () => {
|
||||
const cfg: OpenClawConfig = {
|
||||
channels: {
|
||||
telegram: { accounts: { work: { botToken: "tok-work" } } },
|
||||
},
|
||||
};
|
||||
|
||||
expect(listTelegramAccountIds(cfg)).toEqual(["work"]);
|
||||
resolveTelegramAccount({ cfg, accountId: "work" });
|
||||
});
|
||||
|
||||
const lines = warnMock.mock.calls.map(([line]) => String(line));
|
||||
expect(lines).toContain("listTelegramAccountIds [ 'work' ]");
|
||||
expect(lines).toContain("resolve { accountId: 'work', enabled: true, tokenSource: 'config' }");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,14 +1,29 @@
|
||||
import util from "node:util";
|
||||
import { createAccountActionGate } from "../channels/plugins/account-action-gate.js";
|
||||
import type { OpenClawConfig } from "../config/config.js";
|
||||
import type { TelegramAccountConfig, TelegramActionConfig } from "../config/types.js";
|
||||
import { isTruthyEnvValue } from "../infra/env.js";
|
||||
import { createSubsystemLogger } from "../logging/subsystem.js";
|
||||
import { listBoundAccountIds, resolveDefaultAgentBoundAccountId } from "../routing/bindings.js";
|
||||
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "../routing/session-key.js";
|
||||
import { resolveTelegramToken } from "./token.js";
|
||||
|
||||
const log = createSubsystemLogger("telegram/accounts");
|
||||
|
||||
function formatDebugArg(value: unknown): string {
|
||||
if (typeof value === "string") {
|
||||
return value;
|
||||
}
|
||||
if (value instanceof Error) {
|
||||
return value.stack ?? value.message;
|
||||
}
|
||||
return util.inspect(value, { colors: false, depth: null, compact: true, breakLength: Infinity });
|
||||
}
|
||||
|
||||
const debugAccounts = (...args: unknown[]) => {
|
||||
if (isTruthyEnvValue(process.env.OPENCLAW_DEBUG_TELEGRAM_ACCOUNTS)) {
|
||||
console.warn("[telegram:accounts]", ...args);
|
||||
const parts = args.map((arg) => formatDebugArg(arg));
|
||||
log.warn(parts.join(" ").trim());
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { firstDefined, isSenderIdAllowed, mergeAllowFromSources } from "../channels/allow-from.js";
|
||||
import type { AllowlistMatch } from "../channels/allowlist-match.js";
|
||||
import { createSubsystemLogger } from "../logging/subsystem.js";
|
||||
|
||||
export type NormalizedAllowFrom = {
|
||||
entries: string[];
|
||||
@@ -11,6 +12,7 @@ export type NormalizedAllowFrom = {
|
||||
export type AllowFromMatch = AllowlistMatch<"wildcard" | "id">;
|
||||
|
||||
const warnedInvalidEntries = new Set<string>();
|
||||
const log = createSubsystemLogger("telegram/bot-access");
|
||||
|
||||
function warnInvalidAllowFromEntries(entries: string[]) {
|
||||
if (process.env.VITEST || process.env.NODE_ENV === "test") {
|
||||
@@ -21,9 +23,9 @@ function warnInvalidAllowFromEntries(entries: string[]) {
|
||||
continue;
|
||||
}
|
||||
warnedInvalidEntries.add(entry);
|
||||
console.warn(
|
||||
log.warn(
|
||||
[
|
||||
"[telegram] Invalid allowFrom entry:",
|
||||
"Invalid allowFrom entry:",
|
||||
JSON.stringify(entry),
|
||||
"- allowFrom/groupAllowFrom authorization requires numeric Telegram sender IDs only.",
|
||||
'If you had "@username" entries, re-run onboarding (it resolves @username to IDs) or replace them manually.',
|
||||
|
||||
@@ -86,6 +86,7 @@ const THREAD_NOT_FOUND_RE = /400:\s*Bad Request:\s*message thread not found/i;
|
||||
const MESSAGE_NOT_MODIFIED_RE =
|
||||
/400:\s*Bad Request:\s*message is not modified|MESSAGE_NOT_MODIFIED/i;
|
||||
const CHAT_NOT_FOUND_RE = /400: Bad Request: chat not found/i;
|
||||
const sendLogger = createSubsystemLogger("telegram/send");
|
||||
const diagLogger = createSubsystemLogger("telegram/diagnostic");
|
||||
|
||||
function createTelegramHttpLogger(cfg: ReturnType<typeof loadConfig>) {
|
||||
@@ -272,7 +273,7 @@ async function withTelegramHtmlParseFallback<T>(params: {
|
||||
throw err;
|
||||
}
|
||||
if (params.verbose) {
|
||||
console.warn(
|
||||
sendLogger.warn(
|
||||
`telegram ${params.label} failed with HTML parse error, retrying as plain text: ${formatErrorMessage(
|
||||
err,
|
||||
)}`,
|
||||
@@ -378,7 +379,7 @@ async function withTelegramThreadFallback<T>(
|
||||
throw err;
|
||||
}
|
||||
if (verbose) {
|
||||
console.warn(
|
||||
sendLogger.warn(
|
||||
`telegram ${label} failed with message_thread_id, retrying without thread: ${formatErrorMessage(err)}`,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user