Hone logo
Hone
Problems

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:

  1. Create a Logger: Instantiate a logging.Logger object.
  2. Set Log Level: Configure the logger's log level based on the log_level parameter (defaulting to logging.INFO). Valid log levels are logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, and logging.CRITICAL.
  3. Create a File Handler: Create a logging.FileHandler to write logs to a file specified by the log_file parameter (defaulting to 'app.log'). The file should be opened in append mode.
  4. Create a Formatter: Create a logging.Formatter with a custom format string: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'.
  5. Add Handler to Logger: Add the file handler to the logger.
  6. Set Formatter to Handler: Set the formatter for the file handler.
  7. 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_file parameter should accept strings representing valid file paths.
  • The log_level parameter must be one of the valid logging level constants (e.g., logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL). If an invalid level is provided, a TypeError should 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 logging module.
  • Consider using a try-except block 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 TypeError is the appropriate response.
Loading editor...
python