Skip to content

Commit f775a76

Browse files
authored
Merge pull request #411 from adolago/fix/openclaw-11029-issue-350
[OpenClaw anomalyco#11029] Use subsystem logger in hooks loader
2 parents 6b1acc8 + 36e52e3 commit f775a76

2 files changed

Lines changed: 15 additions & 30 deletions

File tree

‎packages/zee/Swabble/src/hooks/loader.test.ts‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from "node:fs/promises";
22
import os from "node:os";
33
import path from "node:path";
4-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
4+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
55
import { loadInternalHooks } from "./loader.js";
66
import {
77
clearInternalHooks,
@@ -151,8 +151,6 @@ describe("loader", () => {
151151
});
152152

153153
it("should handle module loading errors gracefully", async () => {
154-
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
155-
156154
const cfg: ZeeConfig = {
157155
hooks: {
158156
internal: {
@@ -169,17 +167,9 @@ describe("loader", () => {
169167

170168
const count = await loadInternalHooks(cfg, tmpDir);
171169
expect(count).toBe(0);
172-
expect(consoleError).toHaveBeenCalledWith(
173-
expect.stringContaining("Failed to load hook handler"),
174-
expect.any(String),
175-
);
176-
177-
consoleError.mockRestore();
178170
});
179171

180172
it("should handle non-function exports", async () => {
181-
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
182-
183173
// Create a module with a non-function export
184174
const handlerPath = path.join(tmpDir, "bad-export.js");
185175
await fs.writeFile(handlerPath, 'export default "not a function";', "utf-8");
@@ -200,9 +190,6 @@ describe("loader", () => {
200190

201191
const count = await loadInternalHooks(cfg, tmpDir);
202192
expect(count).toBe(0);
203-
expect(consoleError).toHaveBeenCalledWith(expect.stringContaining("is not a function"));
204-
205-
consoleError.mockRestore();
206193
});
207194

208195
it("should handle relative paths", async () => {

‎packages/zee/Swabble/src/hooks/loader.ts‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ import path from "node:path";
1010
import { registerInternalHook } from "./internal-hooks.js";
1111
import type { ZeeConfig } from "../config/config.js";
1212
import type { InternalHookHandler } from "./internal-hooks.js";
13+
import { createSubsystemLogger } from "../logging/subsystem.js";
1314
import { loadWorkspaceHookEntries } from "./workspace.js";
1415
import { resolveHookConfig } from "./config.js";
1516
import { shouldIncludeHook } from "./config.js";
1617

18+
const log = createSubsystemLogger("hooks:loader");
19+
1720
/**
1821
* Load and register all hook handlers
1922
*
@@ -67,38 +70,34 @@ export async function loadInternalHooks(cfg: ZeeConfig, workspaceDir: string): P
6770
const handler = mod[exportName];
6871

6972
if (typeof handler !== "function") {
70-
console.error(
71-
`Hook error: Handler '${exportName}' from ${entry.hook.name} is not a function`,
72-
);
73+
log.error(`Handler '${exportName}' from ${entry.hook.name} is not a function`);
7374
continue;
7475
}
7576

7677
// Register for all events listed in metadata
7778
const events = entry.metadata?.events ?? [];
7879
if (events.length === 0) {
79-
console.warn(`Hook warning: Hook '${entry.hook.name}' has no events defined in metadata`);
80+
log.warn(`Hook '${entry.hook.name}' has no events defined in metadata`);
8081
continue;
8182
}
8283

8384
for (const event of events) {
8485
registerInternalHook(event, handler as InternalHookHandler);
8586
}
8687

87-
console.log(
88+
log.info(
8889
`Registered hook: ${entry.hook.name} -> ${events.join(", ")}${exportName !== "default" ? ` (export: ${exportName})` : ""}`,
8990
);
9091
loadedCount++;
9192
} catch (err) {
92-
console.error(
93-
`Failed to load hook ${entry.hook.name}:`,
94-
err instanceof Error ? err.message : String(err),
93+
log.error(
94+
`Failed to load hook ${entry.hook.name}: ${err instanceof Error ? err.message : String(err)}`,
9595
);
9696
}
9797
}
9898
} catch (err) {
99-
console.error(
100-
"Failed to load directory-based hooks:",
101-
err instanceof Error ? err.message : String(err),
99+
log.error(
100+
`Failed to load directory-based hooks: ${err instanceof Error ? err.message : String(err)}`,
102101
);
103102
}
104103

@@ -121,20 +120,19 @@ export async function loadInternalHooks(cfg: ZeeConfig, workspaceDir: string): P
121120
const handler = mod[exportName];
122121

123122
if (typeof handler !== "function") {
124-
console.error(`Hook error: Handler '${exportName}' from ${modulePath} is not a function`);
123+
log.error(`Handler '${exportName}' from ${modulePath} is not a function`);
125124
continue;
126125
}
127126

128127
// Register the handler
129128
registerInternalHook(handlerConfig.event, handler as InternalHookHandler);
130-
console.log(
129+
log.info(
131130
`Registered hook (legacy): ${handlerConfig.event} -> ${modulePath}${exportName !== "default" ? `#${exportName}` : ""}`,
132131
);
133132
loadedCount++;
134133
} catch (err) {
135-
console.error(
136-
`Failed to load hook handler from ${handlerConfig.module}:`,
137-
err instanceof Error ? err.message : String(err),
134+
log.error(
135+
`Failed to load hook handler from ${handlerConfig.module}: ${err instanceof Error ? err.message : String(err)}`,
138136
);
139137
}
140138
}

0 commit comments

Comments
 (0)