watch_log_file
Monitor log files in real-time to detect errors and issues as they occur, enabling prompt debugging and analysis.
Instructions
Start monitoring a log file for real-time error detection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Path to the log file to monitor | |
| pollInterval | No | Polling interval in milliseconds |
Implementation Reference
- src/server.ts:369-386 (handler)MCP server-side handler for the 'watch_log_file' tool. Validates input arguments and delegates to FileWatcher.watchLogFile to start monitoring the log file.private async handleWatchLogFile(args: any): Promise<MCPToolResult> { const { filePath, pollInterval = 1000 } = args; if (!filePath || typeof filePath !== 'string') { throw new Error('filePath is required and must be a string'); } await this.fileWatcher.watchLogFile(filePath, { pollInterval }); return { success: true, data: { message: `Started watching ${filePath}`, filePath, pollInterval } }; }
- src/tools/fileWatcher.ts:24-77 (helper)Core helper function implementing log file watching using chokidar. Sets up polling watcher, handles file changes by analyzing new content for errors with LogAnalyzer, tracks errors per file.async watchLogFile(filePath: string, options: WatchOptions = {}): Promise<void> { // Validate file path first const validation = await LogUtils.validateFilePath(filePath); if (!validation.valid) { throw new Error(`Cannot watch file: ${validation.error}`); } // Stop existing watcher if present if (this.watchers.has(filePath)) { await this.stopWatching(filePath); } const watchOptions: WatchOptions = { pollInterval: options.pollInterval || 1000, ignoreInitial: options.ignoreInitial ?? false, usePolling: options.usePolling ?? true }; // Get initial file size const stats = await fs.stat(filePath); const initialSize = stats.size; // Create watcher const watcher = chokidar.watch(filePath, { ignoreInitial: watchOptions.ignoreInitial, usePolling: watchOptions.usePolling, interval: watchOptions.pollInterval }); const watchedFile: WatchedFile = { filePath, watcher, lastSize: initialSize, errors: [], lastUpdate: new Date(), options: watchOptions }; // Set up event handlers watcher.on('change', async (path) => { await this.handleFileChange(path); }); watcher.on('error', (error) => { console.error(`File watcher error for ${filePath}:`, error); }); this.watchers.set(filePath, watchedFile); // Process initial content if not ignoring initial if (!watchOptions.ignoreInitial) { await this.handleFileChange(filePath); } }
- src/server.ts:103-121 (registration)Registration of the 'watch_log_file' tool in the ListToolsRequestSchema handler, including name, description, and input schema definition.{ name: 'watch_log_file', description: 'Start monitoring a log file for real-time error detection', inputSchema: { type: 'object', properties: { filePath: { type: 'string', description: 'Path to the log file to monitor' }, pollInterval: { type: 'number', default: 1000, description: 'Polling interval in milliseconds' } }, required: ['filePath'] } },
- src/server.ts:106-121 (schema)Input schema definition for the 'watch_log_file' tool, specifying required filePath and optional pollInterval.inputSchema: { type: 'object', properties: { filePath: { type: 'string', description: 'Path to the log file to monitor' }, pollInterval: { type: 'number', default: 1000, description: 'Polling interval in milliseconds' } }, required: ['filePath'] } },
- src/server.ts:183-184 (handler)Dispatch switch case in CallToolRequestSchema handler that routes 'watch_log_file' calls to the specific handler.case 'watch_log_file': result = await this.handleWatchLogFile(args);