Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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));
};
};
47 changes: 47 additions & 0 deletions tests/utils/fixtures/puppeteer-logger-fixture.ts
Original file line number Diff line number Diff line change
@@ -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 <compiled-fixture> <prefix> <logFilePath | -->
// If <logFilePath> 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 <prefix> <logFilePath|-->');
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');
112 changes: 112 additions & 0 deletions tests/utils/logger.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
Loading