Skip to content

Commit

Permalink
Secure log rollover at startup.
Browse files Browse the repository at this point in the history
  • Loading branch information
janiversen committed Aug 19, 2021
1 parent 6d0ce81 commit 1bed18b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 7 additions & 5 deletions homeassistant/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,17 @@ def async_enable_logging(
not err_path_exists and os.access(err_dir, os.W_OK)
):

err_handler: logging.FileHandler
if log_rotate_days:
err_handler: logging.FileHandler = (
logging.handlers.TimedRotatingFileHandler(
err_log_path, when="midnight", backupCount=log_rotate_days
)
err_handler = logging.handlers.TimedRotatingFileHandler(
err_log_path, when="midnight", backupCount=log_rotate_days, delay=True
)
else:
err_handler = logging.FileHandler(err_log_path, mode="w", delay=True)
err_handler = logging.handlers.RotatingFileHandler(
err_log_path, backupCount=1, delay=True
)

err_handler.rotate(err_log_path, f"{err_log_path}.bak")
err_handler.setLevel(logging.INFO if verbose else logging.WARNING)
err_handler.setFormatter(logging.Formatter(fmt, datefmt=datefmt))

Expand Down
7 changes: 7 additions & 0 deletions tests/test_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ async def test_async_enable_logging(hass):
) as mock_async_activate_log_queue_handler:
bootstrap.async_enable_logging(hass)
mock_async_activate_log_queue_handler.assert_called_once()
mock_async_activate_log_queue_handler.reset_mock()
bootstrap.async_enable_logging(
hass,
log_rotate_days=5,
log_file="test.log",
)
mock_async_activate_log_queue_handler.assert_called_once()


async def test_load_hassio(hass):
Expand Down

0 comments on commit 1bed18b

Please sign in to comment.