get_console_logs
Retrieve and filter browser console logs by level (log, info, warn, error, debug) with an option to clear logs afterward. Enhances debugging and monitoring capabilities in Playwright-based browser automation.
Instructions
Get console logs from the browser
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| clear | No | Clear console logs after retrieving | |
| level | No | Filter logs by level |
Implementation Reference
- src/index.ts:601-630 (handler)Main handler function for the get_console_logs tool, which filters console logs by optional level, formats them with timestamps, returns as text response, and optionally clears the log storage.case 'get_console_logs': { if (!currentPage) { throw new Error('No browser page available. Launch a browser first.'); } const params = GetConsoleLogsSchema.parse(args); // Filter logs by level if specified const filteredLogs = params.level ? consoleLogs.filter(log => log.level === params.level) : consoleLogs; // Clear logs if requested if (params.clear) { consoleLogs = []; } const logText = filteredLogs.length > 0 ? filteredLogs.map(log => `[${log.timestamp.toISOString()}] ${log.level.toUpperCase()}: ${log.message}`).join('\n') : '(no console logs)'; return { content: [ { type: 'text', text: `Console Logs:\n${logText}` } ] }; }
- src/index.ts:59-62 (schema)Zod schema for validating input parameters: optional level filter and clear flag.const GetConsoleLogsSchema = z.object({ level: z.enum(['log', 'info', 'warn', 'error', 'debug']).optional(), clear: z.boolean().default(false) });
- src/index.ts:295-313 (registration)Registration of the tool in the ListTools response, providing name, description, and JSON input schema.{ name: 'get_console_logs', description: 'Get console logs from the browser', inputSchema: { type: 'object', properties: { level: { type: 'string', enum: ['log', 'info', 'warn', 'error', 'debug'], description: 'Filter logs by level' }, clear: { type: 'boolean', default: false, description: 'Clear console logs after retrieving' } } } },
- src/index.ts:88-88 (helper)Global storage array for console logs captured from the browser page.let consoleLogs: Array<{level: string, message: string, timestamp: Date}> = [];
- src/index.ts:440-446 (helper)Page event listener that captures console messages and stores them in the global consoleLogs array. Set up during browser launch.currentPage.on('console', (msg) => { consoleLogs.push({ level: msg.type(), message: msg.text(), timestamp: new Date() }); });