Add Logging and Error Handling

You are a senior software engineer specializing in production-ready code, observability, and resilient error handling.

Category
Software Development & Coding
Output
Code
Fields
3
  • software-development
  • code-fix
  • debugging
  • best-practices
  • code-review
  • documentation

Fill in the details

Paste the source code that needs logging and error handling.

Specify the programming language, framework, or library used (e.g., Python/FastAPI, Node.js/Express, React).

Where the code executes (e.g., browser, Node.js, AWS Lambda, Kubernetes, mobile app).

Prompt

You are a senior software engineer specializing in production-ready code, observability, and resilient error handling. Analyze the provided code and refactor it to include proper logging and error handling without introducing unnecessary complexity or performance overhead. Context: - Code to refactor: Code to refactor - Language or stack (if provided): Language or framework - Runtime environment (if provided): Runtime environment Instructions: 1. **Error Handling Strategy**: Identify critical failure points. Clearly state what should be caught locally versus what should bubble up to a higher-level handler or global error boundary. 2. **Logging Guidelines**: Define what to log, at which level (debug, info, warn, error), and explicitly list what must never be logged (e.g., secrets, PII, full request payloads). Avoid noisy or redundant logs. 3. **User vs. Internal Messages**: Separate internal technical logs from user-facing error messages. Provide safe, actionable, and non-technical messages for end-users. 4. **Retry & Fallbacks**: Suggest retry mechanisms or fallback strategies only where they logically improve resilience without masking underlying issues or causing infinite loops. 5. **Refactored Code**: Provide the complete, production-ready code implementing these changes. Keep it idiomatic to the specified stack and environment. Constraints: - Use only the information provided. Do not invent external libraries, APIs, configuration variables, or environment-specific behaviors. - Never swallow errors silently or log sensitive data. - If the language, stack, or environment is not provided, infer standard conventions for the code provided. - If any context is ambiguous or missing, list it in an "Open Questions" section instead of guessing. Output Format: Return a structured markdown response with the following sections: - **Error Handling Strategy**: Bullet points covering catch vs. bubble-up decisions. - **Logging Guidelines**: Clear rules for log levels, content, and exclusions. - **User vs. Internal Messages**: Distinct lists for each. - **Retry/Fallback Recommendations**: Conditional suggestions with reasoning. - **Refactored Code**: A single, complete code block. - **Open Questions**: A short list of any missing context that needs confirmation.

How to use this prompt

  1. Fill in the fields above. Required ones are marked with *.
  2. Check the prompt preview: your answers appear highlighted in place.
  3. Select Copy prompt and paste it into ChatGPT, Claude, Gemini or any AI assistant.
See all Software Development & Coding prompts