Home Assistant no longer crashes if it cannot write its log file

This commit is contained in:
Paulus Schoutsen 2014-09-22 21:44:26 -05:00
parent d570aeef33
commit 17eefcffbe

View file

@ -152,15 +152,21 @@ def from_config_file(config_path, hass=None, enable_logging=True):
hass = homeassistant.HomeAssistant()
# Set config dir to directory holding config file
hass.config_dir = os.path.dirname(config_path)
hass.config_dir = os.path.abspath(os.path.dirname(config_path))
if enable_logging:
# Setup the logging for home assistant.
logging.basicConfig(level=logging.INFO)
# Log errors to a file
# Log errors to a file if we have write access to file or config dir
err_log_path = hass.get_config_path("home-assistant.log")
err_path_exists = os.path.isfile(err_log_path)
if (err_path_exists and os.access(err_log_path, os.W_OK)) or \
(not err_path_exists and os.access(hass.config_dir, os.W_OK)):
err_handler = logging.FileHandler(
hass.get_config_path("home-assistant.log"), mode='w', delay=True)
err_log_path, mode='w', delay=True)
err_handler.setLevel(logging.ERROR)
err_handler.setFormatter(
@ -168,6 +174,11 @@ def from_config_file(config_path, hass=None, enable_logging=True):
datefmt='%H:%M %d-%m-%y'))
logging.getLogger('').addHandler(err_handler)
else:
logging.getLogger(__name__).error(
"Unable to setup error log {} (access denied)".format(
err_log_path))
# Read config
config = configparser.ConfigParser()
config.read(config_path)