Skip to content

Commit 04baadc

Browse files
committed
fix(core): redact simple structured logger output
1 parent 858f056 commit 04baadc

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

packages/core/src/v3/utils/structuredLogger.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { redact } from "../../logger.js";
2+
13
type StructuredArgs = (Record<string, unknown> | undefined)[];
24

35
export interface StructuredLogger {
@@ -88,14 +90,14 @@ export class SimpleStructuredLogger implements StructuredLogger {
8890
level: string,
8991
...args: StructuredArgs
9092
) {
91-
const structuredLog = {
93+
const structuredLog = redact({
9294
timestamp: new Date(),
9395
message,
9496
$name: this.name,
9597
$level: level,
9698
...this.fields,
9799
...(args.length === 1 ? args[0] : args),
98-
};
100+
}) as Record<string, unknown>;
99101

100102
if (SimpleStructuredLogger.onLog) {
101103
try {

packages/core/test/logger.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Logger, redact } from "../src/logger.js";
2+
import { SimpleStructuredLogger } from "../src/v3/utils/structuredLogger.js";
23

34
function captureLogLine(fn: () => void): Record<string, any> {
45
const spy = vi.spyOn(console, "info").mockImplementation(() => {});
@@ -167,6 +168,26 @@ describe("Logger redaction", () => {
167168
});
168169
});
169170

171+
describe("SimpleStructuredLogger redaction", () => {
172+
it("redacts fields and arguments with the default deny-list", () => {
173+
const logger = new SimpleStructuredLogger("test");
174+
175+
const line = captureLogLine(() =>
176+
logger.child({ headers: { authorization: "Bearer should-not-appear" } }).info("run started", {
177+
payload: { secret: "value" },
178+
apiKey: "tr_prod_should_not_appear",
179+
harmless: "keep me",
180+
})
181+
);
182+
183+
expect(line.headers).toMatch(/^\[filtered/);
184+
expect(line.payload).toMatch(/^\[filtered/);
185+
expect(line.apiKey).toMatch(/^\[filtered/);
186+
expect(line.harmless).toBe("keep me");
187+
expect(JSON.stringify(line)).not.toContain("should-not-appear");
188+
});
189+
});
190+
170191
describe("redact()", () => {
171192
it("applies the same default deny-list and truncation as the Logger", () => {
172193
const result = redact({

0 commit comments

Comments
 (0)