Skip to content

Commit

Permalink
Refactor logging setup and fix unit tests for LoggerConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
yihong1120 committed Jul 16, 2024
1 parent c16f28b commit 638a734
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/monitor_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ def setup_logger(self):
"""
Configures the logger with rotating file handler and console handler.
"""
# Prevent adding handlers multiple times
if self.logger.hasHandlers():
return

# Create log directory if it doesn't exist
Path(self.log_dir).mkdir(parents=True, exist_ok=True)

# Prevent adding handlers multiple times
if self.logger.hasHandlers():
self.logger.handlers.clear()

# Configure the file handler
file_handler = self.get_file_handler()
# Configure the console handler
Expand Down
8 changes: 8 additions & 0 deletions tests/monitor_logger_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def test_setup_logger(
mock_rotating_file_handler.return_value = mock_file_handler
mock_stream_handler.return_value = mock_console_handler

# Set level attribute for the handlers
mock_file_handler.level = self.level
mock_console_handler.level = self.level

# Run setup_logger
self.logger_config.setup_logger()

Expand Down Expand Up @@ -104,6 +108,10 @@ def test_logger_output(
self.logger_config.setup_logger()
logger = self.logger_config.get_logger()

# Set level attribute for the handlers in case they are checked
mock_file_handler.level = logging.DEBUG
mock_console_handler.level = logging.DEBUG

# Test logging output
with self.assertLogs(logger, level='INFO') as log:
logger.info('Test log message')
Expand Down

0 comments on commit 638a734

Please sign in to comment.