Implementation:Microsoft Playwright DebugLogger
Overview
DebugLogger provides a structured debug logging system for Playwright's server internals, using the debug module with color-coded categories and optional file output.
Description
The DebugLogger class manages a collection of debug loggers, lazily creating them for each log category. Each category has a predefined color:
api(cyan),protocol(green),browser(reset),error(red)channel(blue),server(cyan),socks(purple)- And more categories for install, download, recorder, etc.
When the DEBUG_FILE environment variable is set, all debug output is redirected to a file with ANSI escape codes stripped.
The module also provides RecentLogsCollector, a bounded log buffer that keeps the most recent log entries for inclusion in error reports.
Usage
Used throughout Playwright's server code for debug-level logging. Enabled via the DEBUG=pw:* environment variable.
Code Reference
Source Location
packages/playwright-core/src/server/utils/debugLogger.ts (96 lines)
Class Signatures
export type LogName = 'api' | 'protocol' | 'install' | 'download' | 'browser' | 'socks' | 'error' | 'channel' | 'server' | ...;
class DebugLogger {
log(name: LogName, message: string | Error | object): void
isEnabled(name: LogName): boolean
}
export const debugLogger: DebugLogger;
export class RecentLogsCollector {
constructor()
log(message: string): void
recentLogs(): string[]
}
Import
import { debugLogger, RecentLogsCollector } from './server/utils/debugLogger';
import type { LogName } from './server/utils/debugLogger';
I/O Contract
Inputs
name: LogName-- the log categorymessage: string | Error | object-- the message to log
Outputs
- Outputs to stderr via the
debugmodule (when enabled) - Optionally writes to a file specified by
DEBUG_FILEenvironment variable
Related Pages
- Microsoft_Playwright_ProgressController -- Uses debugLogger for call logging
- Microsoft_Playwright_Server_Instrumentation -- LogName type used in SdkObject