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,21 +152,32 @@ def from_config_file(config_path, hass=None, enable_logging=True):
hass = homeassistant.HomeAssistant() hass = homeassistant.HomeAssistant()
# Set config dir to directory holding config file # 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: if enable_logging:
# Setup the logging for home assistant. # Setup the logging for home assistant.
logging.basicConfig(level=logging.INFO) 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_handler = logging.FileHandler( err_log_path = hass.get_config_path("home-assistant.log")
hass.get_config_path("home-assistant.log"), mode='w', delay=True) err_path_exists = os.path.isfile(err_log_path)
err_handler.setLevel(logging.ERROR) if (err_path_exists and os.access(err_log_path, os.W_OK)) or \
err_handler.setFormatter( (not err_path_exists and os.access(hass.config_dir, os.W_OK)):
logging.Formatter('%(asctime)s %(name)s: %(message)s',
datefmt='%H:%M %d-%m-%y')) err_handler = logging.FileHandler(
logging.getLogger('').addHandler(err_handler) err_log_path, mode='w', delay=True)
err_handler.setLevel(logging.ERROR)
err_handler.setFormatter(
logging.Formatter('%(asctime)s %(name)s: %(message)s',
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 # Read config
config = configparser.ConfigParser() config = configparser.ConfigParser()