AI generated prompt for Rust Error Handler and Logger
**Context**: Design a robust error handler and logger for a Rust application, focusing on providing informative error messages, categorizing errors, and logging mechanisms for debugging and monitoring purposes. The error handler should be capable of handling various types of errors, including runtime errors, syntax errors, and custom application-specific errors. **Detailed Instructions**: 1. Develop a custom error type that can encapsulate different error variants, including but not limited to, `IOError`, `NetworkError`, `DatabaseError`, and `UnknownError`. Each variant should have associated details such as error message and optional additional context (e.g., file path for `IOError`, URL for `NetworkError`). 2. Implement a logger that can log errors at different levels (e.g., debug, info, warn, error, fatal). The logger should be configurable to log to the console, file, or both, with the ability to customize log format and verbosity level. 3. Create a global error handler function that catches and processes all unhandled errors, logs them using the custom logger, and then performs appropriate actions based on the error type (e.g., exit the application for fatal errors, retry for transient errors). 4. Integrate the error handler with the logger to ensure all errors are logged appropriately, including the error message, error type, and any additional context. 5. Ensure the solution is extensible, allowing for easy addition of new error types and custom error handling logic without modifying the existing codebase. **Output Format**: The solution should be presented as a Rust code snippet that includes: - The definition of the custom error type. - The implementation of the logger. - The global error handler function. - Example usage demonstrating how to use the custom error type, logger, and error handler in a Rust application. **Examples**: - An example of logging an `IOError` when attempting to read from a non-existent file. - An example of handling a `NetworkError` by retrying the network request. - An example of customizing the logger to log errors to both console and file with a specific format.
This coding prompt is designed to help you get better results from AI assistants like ChatGPT, Claude, and Gemini. Here's how to make the most of it:
💡 Pro tip: Save this prompt to your collection to use it again later. Well-crafted prompts can save hours of back-and-forth with AI.
Adjust the prompt to match your specific industry, audience, or use case. Adding relevant context improves output quality.
Specify your desired output length (e.g., "in 200 words" or "in 3 bullet points") to get more targeted responses.
Add tone instructions like "professional," "casual," or "technical" to match your brand voice.
Include an example of the output format you want to help the AI understand your expectations.
This prompt has been tested and optimized for all major AI models. For best results with coding-related prompts, consider using an AI-powered IDE like Cursor or Windsurf.
Learn more about using prompts effectively with our comprehensive guides:
0 people found this prompt helpful
Based on 0 reviews
Be the first to share your experience with this prompt!
This prompt was reviewed and verified to work with current AI models.
Tested with ChatGPT, Claude & Gemini. Reviewed by community users.
AI prompts work best when you customize them for your specific situation. Follow these steps to get the most out of Rust Error Handler And Logger.