From a3fca6c161d4d30306bb07597e93b64e1896726c Mon Sep 17 00:00:00 2001 From: Praveen Mittal Date: Sat, 12 Sep 2026 22:15:27 +0200 Subject: [PATCH] fix(logger): puppeteerLogger should check DEBUG before checking --log-file puppeteerLogger() checked `logFileStream` truthiness before checking whether the namespace was DEBUG-enabled, so once --log-file was set, every CDP protocol message (puppeteer:protocol:SEND/RECV) was written unconditionally, ignoring DEBUG entirely. This is a regression from #2562 (replacing the `debug` npm package with Node's native util.debuglog): in v1.7.0, saveLogsToFile() called debug.enable() with the user's DEBUG namespaces plus mcp:log, so only explicitly enabled namespaces were written to the file. Reorder the check so DEBUG-enablement gates both branches, matching v1.7.0 behavior. logger() (the separate mcp:log function) is unaffected and correctly unconditional, since mcp:log was always in the enabled set even in v1.7.0. --- src/utils/logger.ts | 14 ++- .../fixtures/puppeteer-logger-fixture.ts | 47 ++++++++ tests/utils/logger.test.ts | 112 ++++++++++++++++++ 3 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 tests/utils/fixtures/puppeteer-logger-fixture.ts create mode 100644 tests/utils/logger.test.ts diff --git a/src/utils/logger.ts b/src/utils/logger.ts index d7748c25bc..5251f025c3 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -50,6 +50,11 @@ export const logger: Logger = (...args: unknown[]) => { }; export const puppeteerLogger = (prefix: string) => { + const dbg = util.debuglog(prefix); + if (!dbg.enabled) { + return undefined; + } + if (logFileStream) { return (...args: unknown[]) => { logFileStream!.write( @@ -58,10 +63,7 @@ export const puppeteerLogger = (prefix: string) => { }; } - const dbg = util.debuglog(prefix); - return dbg.enabled - ? (...args: unknown[]) => { - dbg('%s %s', new Date().toISOString(), util.format(...args)); - } - : undefined; + return (...args: unknown[]) => { + dbg('%s %s', new Date().toISOString(), util.format(...args)); + }; }; diff --git a/tests/utils/fixtures/puppeteer-logger-fixture.ts b/tests/utils/fixtures/puppeteer-logger-fixture.ts new file mode 100644 index 0000000000..3895737813 --- /dev/null +++ b/tests/utils/fixtures/puppeteer-logger-fixture.ts @@ -0,0 +1,47 @@ +/** + * @license + * Copyright 2026 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +// Standalone fixture spawned as a child process by logger.test.ts. +// +// `util.debuglog(prefix).enabled` reflects the NODE_DEBUG environment +// variable as it was when the *process* started, not a value that can be +// toggled at runtime within a single process. Testing puppeteerLogger's +// DEBUG-gating behavior for both the enabled and disabled cases therefore +// requires spawning separate processes with different NODE_DEBUG values, +// which is what this fixture is for. +// +// Usage: node +// If is "--", saveLogsToFile is not called (no-log-file case). + +import { + saveLogsToFile, + flushLogs, + puppeteerLogger, +} from '../../../src/utils/logger.js'; + +const [, , prefix, logFilePathArg] = process.argv; + +if (!prefix || !logFilePathArg) { + console.error('usage: puppeteer-logger-fixture '); + process.exit(2); +} + +const logFile = + logFilePathArg !== '--' ? saveLogsToFile(logFilePathArg) : undefined; + +const write = puppeteerLogger(prefix); +if (write) { + write('fixture message'); +} + +if (logFile) { + await flushLogs(logFile); +} + +// `write` is undefined when the namespace is not DEBUG-enabled; print +// whether a logger was returned so the parent test can assert on both the +// in-process return value and (for the file case) the file's contents. +console.log(write ? 'logger:defined' : 'logger:undefined'); diff --git a/tests/utils/logger.test.ts b/tests/utils/logger.test.ts new file mode 100644 index 0000000000..867492e476 --- /dev/null +++ b/tests/utils/logger.test.ts @@ -0,0 +1,112 @@ +/** + * @license + * Copyright 2026 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import assert from 'node:assert'; +import {execFileSync} from 'node:child_process'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import process from 'node:process'; +import {describe, it} from 'node:test'; +import {fileURLToPath} from 'node:url'; + +const __dirname = path.dirname(fileURLToPath(import.meta.url)); +const FIXTURE = path.join(__dirname, 'fixtures', 'puppeteer-logger-fixture.js'); + +/** + * Runs the puppeteer-logger-fixture in a fresh child process with the given + * NODE_DEBUG value. A fresh process is required because + * `util.debuglog(...).enabled` reflects NODE_DEBUG as of process start, not + * a value that can be toggled at runtime (see the fixture's own comment). + */ +function runFixture( + prefix: string, + logFilePath: string | undefined, + nodeDebug: string | undefined, +): string { + const env = {...process.env}; + if (nodeDebug === undefined) { + delete env.NODE_DEBUG; + } else { + env.NODE_DEBUG = nodeDebug; + } + return execFileSync( + process.execPath, + [FIXTURE, prefix, logFilePath ?? '--'], + {env, encoding: 'utf8'}, + ).trim(); +} + +describe('puppeteerLogger', () => { + it('does not log to a file when the namespace is not DEBUG-enabled', () => { + const logFilePath = path.join( + os.tmpdir(), + `puppeteer-logger-test-${process.pid}-${Date.now()}.log`, + ); + try { + const result = runFixture( + 'puppeteer:protocol:SEND', + logFilePath, + 'mcp:log', // DEBUG is set, but does not include this prefix. + ); + assert.strictEqual(result, 'logger:undefined'); + // saveLogsToFile() creates the file unconditionally (--log-file is a + // global option, independent of any specific namespace), so the + // regression check is on CONTENT, not existence: the file must stay + // empty rather than receiving this namespace's traffic. + const contents = fs.existsSync(logFilePath) + ? fs.readFileSync(logFilePath, 'utf8') + : ''; + assert.strictEqual( + contents, + '', + 'puppeteerLogger must not write to the log file for a namespace ' + + 'that is not enabled via NODE_DEBUG, even when --log-file is set', + ); + } finally { + fs.rmSync(logFilePath, {force: true}); + } + }); + + it('logs to a file when the namespace is DEBUG-enabled', () => { + const logFilePath = path.join( + os.tmpdir(), + `puppeteer-logger-test-${process.pid}-${Date.now()}.log`, + ); + try { + const result = runFixture( + 'puppeteer:protocol:SEND', + logFilePath, + 'puppeteer:protocol:SEND', + ); + assert.strictEqual(result, 'logger:defined'); + const contents = fs.readFileSync(logFilePath, 'utf8'); + assert.match(contents, /puppeteer:protocol:SEND fixture message/); + } finally { + fs.rmSync(logFilePath, {force: true}); + } + }); + + it('logs to a file when NODE_DEBUG=* enables every namespace', () => { + const logFilePath = path.join( + os.tmpdir(), + `puppeteer-logger-test-${process.pid}-${Date.now()}.log`, + ); + try { + const result = runFixture('puppeteer:protocol:RECV', logFilePath, '*'); + assert.strictEqual(result, 'logger:defined'); + const contents = fs.readFileSync(logFilePath, 'utf8'); + assert.match(contents, /puppeteer:protocol:RECV fixture message/); + } finally { + fs.rmSync(logFilePath, {force: true}); + } + }); + + it('returns undefined (no debuglog fallback either) when no log file and namespace is disabled', () => { + const result = runFixture('puppeteer:protocol:SEND', undefined, undefined); + assert.strictEqual(result, 'logger:undefined'); + }); +});