Skip to content

Commit 93923d9

Browse files
authored
chore: add browser logs to the log file (#66)
1 parent bfbdada commit 93923d9

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/browser.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type McpLaunchOptions = {
6161
userDataDir?: string;
6262
headless: boolean;
6363
isolated: boolean;
64+
logFile?: fs.WriteStream;
6465
};
6566

6667
export async function launch(options: McpLaunchOptions): Promise<Browser> {
@@ -100,7 +101,7 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
100101
}
101102

102103
try {
103-
return await puppeteer.launch({
104+
const browser = await puppeteer.launch({
104105
...connectOptions,
105106
channel: puppeterChannel,
106107
executablePath,
@@ -110,6 +111,13 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
110111
headless,
111112
args,
112113
});
114+
if (options.logFile) {
115+
// FIXME: we are probably subscribing too late to catch startup logs. We
116+
// should expose the process earlier or expose the getRecentLogs() getter.
117+
browser.process()?.stderr?.pipe(options.logFile);
118+
browser.process()?.stdout?.pipe(options.logFile);
119+
}
120+
return browser;
113121
} catch (error) {
114122
// TODO: check browser logs for `Failed to create a ProcessSingleton for
115123
// your profile directory` instead.
@@ -145,6 +153,7 @@ export async function resolveBrowser(options: {
145153
channel?: Channel;
146154
headless: boolean;
147155
isolated: boolean;
156+
logFile?: fs.WriteStream;
148157
}) {
149158
const browser = options.browserUrl
150159
? await ensureBrowserConnected(options.browserUrl)

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,7 @@ export const args = yargsInstance
113113
.help()
114114
.parseSync();
115115

116-
if (args.logFile) {
117-
saveLogsToFile(args.logFile);
118-
}
116+
const logFile = args.logFile ? saveLogsToFile(args.logFile) : undefined;
119117

120118
function readPackageJson(): {version?: string} {
121119
const currentDir = import.meta.dirname;
@@ -155,6 +153,7 @@ async function getContext(): Promise<McpContext> {
155153
customDevTools: args.customDevtools,
156154
channel: args.channel as Channel,
157155
isolated: args.isolated,
156+
logFile,
158157
});
159158
if (context?.browser !== browser) {
160159
context = await McpContext.from(browser, logger);

src/logger.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const namespacesToEnable = [
1414
...(process.env['DEBUG'] ? [process.env['DEBUG']] : []),
1515
];
1616

17-
export function saveLogsToFile(fileName: string) {
17+
export function saveLogsToFile(fileName: string): fs.WriteStream {
1818
// Enable overrides everything so we need to add them
1919
debug.enable(namespacesToEnable.join(','));
2020

@@ -27,6 +27,7 @@ export function saveLogsToFile(fileName: string) {
2727
logFile.end();
2828
process.exit(1);
2929
});
30+
return logFile;
3031
}
3132

3233
export const logger = debug(mcpDebugNamespace);

0 commit comments

Comments
 (0)