Robust Python Logging Implementation
Logging is a crucial aspect of software development, enabling developers to track application behavior, debug issues, and monitor performance. This challenge asks you to implement a flexible and configurable logging setup in Python, allowing for different log levels, file output, and custom formatting. A well-designed logging system is essential for maintaining and troubleshooting any non-trivial application.
Problem Description
You are tasked with creating a Python module named logger_setup.py that provides a function setup_logger(log_file='app.log', log_level=logging.INFO) to configure a logger. This function should:
- Create a Logger: Instantiate a
logging.Loggerobject. - Set Log Level: Configure the logger's log level based on the
log_levelparameter (defaulting tologging.INFO). Valid log levels arelogging.DEBUG,logging.INFO,logging.WARNING,logging.ERROR, andlogging.CRITICAL. - Create a File Handler: Create a
logging.FileHandlerto write logs to a file specified by thelog_fileparameter (defaulting to 'app.log'). The file should be opened in append mode. - Create a Formatter: Create a
logging.Formatterwith a custom format string:'%(asctime)s - %(name)s - %(levelname)s - %(message)s'. - Add Handler to Logger: Add the file handler to the logger.
- Set Formatter to Handler: Set the formatter for the file handler.
- Return the Logger: Return the configured logger object.
The module should also include a simple example demonstrating how to use the setup_logger function and log messages at different levels.
Examples
Example 1:
Input: log_file='my_app.log', log_level=logging.DEBUG
Output: A configured logger object that writes DEBUG, INFO, WARNING, ERROR, and CRITICAL messages to 'my_app.log' with the specified format.
Explanation: The function creates a logger, sets the log level to DEBUG, creates a file handler writing to 'my_app.log', creates a formatter, adds the handler to the logger, and sets the formatter for the handler.
Example 2:
Input: log_file='error.log', log_level=logging.ERROR
Output: A configured logger object that writes ERROR and CRITICAL messages to 'error.log' with the specified format.
Explanation: The function creates a logger, sets the log level to ERROR, creates a file handler writing to 'error.log', creates a formatter, adds the handler to the logger, and sets the formatter for the handler.
Example 3: (Edge Case - Invalid Log Level)
Input: log_file='invalid.log', log_level='invalid_level'
Output: A TypeError exception is raised.
Explanation: The function should validate the log level and raise a TypeError if it's not a valid logging level constant.
Constraints
- The
log_fileparameter should accept strings representing valid file paths. - The
log_levelparameter must be one of the validlogginglevel constants (e.g.,logging.DEBUG,logging.INFO,logging.WARNING,logging.ERROR,logging.CRITICAL). If an invalid level is provided, aTypeErrorshould be raised. - The file handler should open the log file in append mode ('a').
- The formatter should use the specified format string.
- The module should be named
logger_setup.py.
Notes
- Use the standard
loggingmodule. - Consider using a
try-exceptblock to handle potential errors during file operations. - The example usage within the module should demonstrate logging at least three different log levels (DEBUG, INFO, WARNING).
- Think about how to make the code reusable and easy to understand. Clear variable names and comments are encouraged.
- The function should return the logger object, allowing the caller to use it for logging.
- Error handling for invalid log levels is important. Raising a
TypeErroris the appropriate response.