Robust Error Tracking and Logging in Python
Error tracking is crucial for maintaining stable and reliable applications. This challenge asks you to build a simple error tracking system in Python that captures exceptions, logs them with relevant context, and provides a basic mechanism for reporting. This is a fundamental skill for any Python developer, enabling proactive debugging and improved application resilience.
Problem Description
You are tasked with creating a Python module named error_tracker.py that provides functionality for tracking and logging exceptions. The module should include the following:
-
track_exception(exception, message="")Function: This function takes an exception object and an optional message as input. It should log the exception type, the exception message, a timestamp, and the provided message to a file namederror_log.txt. The timestamp should be in a readable format (e.g., "YYYY-MM-DD HH:MM:SS"). -
get_error_log()Function: This function should read the contents oferror_log.txtand return them as a single string. If the file doesn't exist or is empty, it should return an empty string. -
Error Handling: The
track_exceptionfunction should gracefully handle potential errors during file writing (e.g., permission errors). In case of a file writing error, it should print an error message to the console without raising an exception itself. -
Logging Format: Each error entry in
error_log.txtshould follow this format:[YYYY-MM-DD HH:MM:SS] - Type: {exception_type}, Message: {exception_message}, Context: {message}Where:
YYYY-MM-DD HH:MM:SSis the timestamp.exception_typeis the type of the exception (e.g.,TypeError,ValueError).exception_messageis the exception's message.messageis the optional context message provided to the function.
Examples
Example 1:
Input:
exception = TypeError("Invalid type")
message = "Function argument was not an integer"
track_exception(exception, message)
Output:
error_log.txt will contain:
"[2023-10-27 10:00:00] - Type: TypeError, Message: Invalid type, Context: Function argument was not an integer" (Timestamp will be current)
Example 2:
Input:
exception = ValueError("Invalid value")
track_exception(exception) # No context message
Output:
error_log.txt will contain:
"[2023-10-27 10:01:00] - Type: ValueError, Message: Invalid value, Context:" (Timestamp will be current)
Example 3: (Edge Case - File Writing Error)
Input:
exception = OSError("Permission denied")
message = "Unable to write to error log file"
track_exception(exception, message)
Output:
Console output: "Error writing to error_log.txt: [Errno 13] Permission denied"
error_log.txt will *not* be updated.
Constraints
- The
error_log.txtfile should be created in the same directory as theerror_tracker.pymodule. - The timestamp should be accurate to the second.
- The
get_error_log()function should not modify the contents oferror_log.txt. - The
track_exceptionfunction should not raise exceptions itself, even if file writing fails. - The module should be designed to handle a reasonable number of errors (e.g., up to 1000) without significant performance degradation. While not strictly enforced, consider efficiency in your implementation.
Notes
- Consider using the
datetimemodule for timestamp generation. - The
open()function with the appropriate mode ('a' for append) is essential for logging. - Think about how to handle different exception types gracefully.
- The context message is optional, so handle cases where it's not provided.
- Focus on clear, readable code and proper error handling. The goal is a robust and maintainable error tracking system.
- You do not need to implement any sophisticated reporting mechanisms (e.g., sending emails or uploading logs to a server). The focus is on local error logging.