fupio

Fupio çevrimiçi paylaşmanın en kolay yolu Daha fazla bilgi »

Fupio'ya katıl
egghead.io - Bite-sized Web Development Video Tutorials & Training

egghead.io - Bite-sized Web Development Video Tutorials & Training

Debugging hooks can be confusing at first. Using console.log doesn't work because stdout is reserved for communicating with the AI. You need a different approach for both quick debugging and production-ready logging. This lesson takes you from quick debugging with console.error to building a robust, persistent logging system using JSONL files. You'll learn why certain outputs are silent, how to see immediate debug information, and how to create a production logging solution with automatic log rotation—all with the AI's help. ## The Immediate Fix: Use console.error The simplest way to see debug output from your hooks is to use console.error instead of console.log. This directs your log messages to standard error (stderr), which Cursor displays in the "Hooks" output panel.
With this complete logging strategy, you have both immediate debugging visibility and a persistent, manageable history. Your hooks become production-ready—reliable, observable, and easy to troubleshoot when issues arise. --- ### Prompts ~~~markdown @index.ts Please add some comments. ~~~ ~~~markdown Add some more comments ~~~ ~~~markdown remove all the comments ~~~ ~~~markdown Add the comments back ~~~ ~~~markdown remove the comments again ~~~ ~~~markdown Once there are over 500 JSON objects in this file, please remove the first 100 to keep this log rolling. ~~~ ~~~markdown Make sure this is only checking an opening brace on a new line to avoid matching nested objects. ~~~ ### Code Snippets ~~~typescript // Use console.error for debugging output in hooks console.error(JSON.stringify(output, null, 2)); ~~~ ~~~typescript // Import Node.js modules for file operations import { appendFile, readFile, writeFile } from "node:fs/promises"; import { join } from "node:path"; ~~~ ~~~typescript // Setup for logging to a JSON Lines file const output = { timestamp: new Date().toISOString(), ...input, stdout: result.stdout.toString(), stderr: result.stderr.toString(), exitCode: result.exitCode, }; const logFilePath = join(input.workspace_roots[0]!, "logs", "after-file-edit.jsonl"); await appendFile(logFilePath, JSON.stringify(output) + '\n'); ~~~ ~~~typescript // Refined regex to count top-level JSON objects at the start of a line const entryCount = (logContent.match(/^s*{/gm) || []).length; ~~~
https://egghead.io/lessons/logging-and-debugging-cursor-hooks~j0yh2

Comments