Make log rollover at startup (#54865)
* Secure log rollover at startup. * Review comments. * Please CI.pull/55143/head
parent
725b316ec6
commit
1e3452496a
|
@ -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
|
||||
)
|
||||
else:
|
||||
err_handler = logging.FileHandler(err_log_path, mode="w", delay=True)
|
||||
err_handler = logging.handlers.RotatingFileHandler(
|
||||
err_log_path, backupCount=1
|
||||
)
|
||||
|
||||
err_handler.rotate(err_log_path, f"{err_log_path[:-4]}.previous.log")
|
||||
err_handler.setLevel(logging.INFO if verbose else logging.WARNING)
|
||||
err_handler.setFormatter(logging.Formatter(fmt, datefmt=datefmt))
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue