Stop writing to config dir log file on supervised install

This commit is contained in:
abmantis 2025-06-12 20:18:43 +01:00
parent 0c5d796cb8
commit 6f8b98894b
2 changed files with 58 additions and 28 deletions

View File

@ -613,12 +613,20 @@ 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:
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)
if err_log_path:
err_path_exists = os.path.isfile(err_log_path)
err_dir = os.path.dirname(err_log_path)
@ -632,10 +640,7 @@ async def async_enable_logging(
)
err_handler.setFormatter(logging.Formatter(fmt, datefmt=FORMAT_DATETIME))
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

View File

@ -88,11 +88,15 @@ async def test_async_enable_logging(
config_log_file_pattern = get_test_config_dir("home-assistant.log*")
arg_log_file_pattern = "test.log*"
# Ensure we start with a clean slate
for f in glob.glob(arg_log_file_pattern):
os.remove(f)
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
@ -121,10 +125,31 @@ async def test_async_enable_logging(
assert "Error rolling over log file" in caplog.text
for f in glob.glob(arg_log_file_pattern):
os.remove(f)
for f in glob.glob(config_log_file_pattern):
os.remove(f)
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: