Compare commits

...

2 Commits

Author SHA1 Message Date
abmantis 6f8b98894b Stop writing to config dir log file on supervised install 2025-06-12 20:18:43 +01:00
abmantis 0c5d796cb8 Improve bootstrap file logging test
The path to the log file in the config directory was wrong, so it was
never deleted. This fixes the path and also asserts file existence.
2025-06-12 18:55:21 +01:00
2 changed files with 69 additions and 24 deletions
+25 -20
View File
@@ -613,34 +613,39 @@ async def async_enable_logging(
),
)
logger = logging.getLogger()
logger.setLevel(logging.INFO if verbose else logging.WARNING)
# Log errors to a file if we have write access to file or config dir
if log_file is None:
err_log_path = hass.config.path(ERROR_LOG_FILENAME)
if "SUPERVISOR" in os.environ:
_LOGGER.info("Running in Supervisor, not logging to file")
err_log_path = None
else:
err_log_path = hass.config.path(ERROR_LOG_FILENAME)
else:
err_log_path = os.path.abspath(log_file)
err_path_exists = os.path.isfile(err_log_path)
err_dir = os.path.dirname(err_log_path)
if err_log_path:
err_path_exists = os.path.isfile(err_log_path)
err_dir = os.path.dirname(err_log_path)
# Check if we can write to the error log if it exists or that
# we can create files in the containing directory if not.
if (err_path_exists and os.access(err_log_path, os.W_OK)) or (
not err_path_exists and os.access(err_dir, os.W_OK)
):
err_handler = await hass.async_add_executor_job(
_create_log_file, err_log_path, log_rotate_days
)
# Check if we can write to the error log if it exists or that
# we can create files in the containing directory if not.
if (err_path_exists and os.access(err_log_path, os.W_OK)) or (
not err_path_exists and os.access(err_dir, os.W_OK)
):
err_handler = await hass.async_add_executor_job(
_create_log_file, err_log_path, log_rotate_days
)
err_handler.setFormatter(logging.Formatter(fmt, datefmt=FORMAT_DATETIME))
err_handler.setFormatter(logging.Formatter(fmt, datefmt=FORMAT_DATETIME))
logger.addHandler(err_handler)
logger = logging.getLogger()
logger.addHandler(err_handler)
logger.setLevel(logging.INFO if verbose else logging.WARNING)
# Save the log file location for access by other components.
hass.data[DATA_LOGGING] = err_log_path
else:
_LOGGER.error("Unable to set up error log %s (access denied)", err_log_path)
# Save the log file location for access by other components.
hass.data[DATA_LOGGING] = err_log_path
else:
_LOGGER.error("Unable to set up error log %s (access denied)", err_log_path)
async_activate_log_queue_handler(hass)
+44 -4
View File
@@ -85,6 +85,21 @@ async def test_async_enable_logging(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test to ensure logging is migrated to the queue handlers."""
config_log_file_pattern = get_test_config_dir("home-assistant.log*")
arg_log_file_pattern = "test.log*"
def cleanup_log_files() -> None:
"""Remove all log files."""
for f in glob.glob(config_log_file_pattern):
os.remove(f)
for f in glob.glob(arg_log_file_pattern):
os.remove(f)
# Ensure we start with a clean slate
cleanup_log_files()
assert len(glob.glob(config_log_file_pattern)) == 0
assert len(glob.glob(arg_log_file_pattern)) == 0
with (
patch("logging.getLogger"),
patch(
@@ -97,6 +112,8 @@ async def test_async_enable_logging(
):
await bootstrap.async_enable_logging(hass)
mock_async_activate_log_queue_handler.assert_called_once()
assert len(glob.glob(config_log_file_pattern)) > 0
mock_async_activate_log_queue_handler.reset_mock()
await bootstrap.async_enable_logging(
hass,
@@ -104,13 +121,36 @@ async def test_async_enable_logging(
log_file="test.log",
)
mock_async_activate_log_queue_handler.assert_called_once()
for f in glob.glob("test.log*"):
os.remove(f)
for f in glob.glob("testing_config/home-assistant.log*"):
os.remove(f)
assert len(glob.glob(arg_log_file_pattern)) > 0
assert "Error rolling over log file" in caplog.text
cleanup_log_files()
with (
patch.dict(os.environ, {"SUPERVISOR": "1"}),
patch("logging.getLogger"),
patch(
"homeassistant.bootstrap.async_activate_log_queue_handler"
) as mock_async_activate_log_queue_handler,
):
await bootstrap.async_enable_logging(hass)
mock_async_activate_log_queue_handler.assert_called_once()
# On Supervisor, the default log file should not be created
assert len(glob.glob(config_log_file_pattern)) == 0
mock_async_activate_log_queue_handler.reset_mock()
await bootstrap.async_enable_logging(
hass,
log_rotate_days=5,
log_file="test.log",
)
mock_async_activate_log_queue_handler.assert_called_once()
# Even on Supervisor, the log file should be created if explicitly set
assert len(glob.glob(arg_log_file_pattern)) > 0
cleanup_log_files()
async def test_load_hassio(hass: HomeAssistant) -> None:
"""Test that we load the hassio integration when using Supervisor."""