From 42db57ea864af34482b4c9741f0a551b8773e2b5 Mon Sep 17 00:00:00 2001 From: Ted Drain Date: Thu, 14 Sep 2017 19:54:57 -0700 Subject: [PATCH 1/3] Changed api.py to use new log file name --- homeassistant/bootstrap.py | 24 +++++++++++++++--------- homeassistant/components/api.py | 5 ++--- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 1fa113ab597ed..5a8f0d0537afe 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -26,7 +26,12 @@ _LOGGER = logging.getLogger(__name__) -ERROR_LOG_FILENAME = 'home-assistant.log' +# After the logging is initialized, ERROR_LOG_PATH will be the full +# path to the log file. This is needed because components/api needs +# to access the log file location. +DEFAULT_LOG_FILENAME = 'home-assistant.log' +ERROR_LOG_PATH = None + FIRST_INIT_COMPONENT = set(( 'recorder', 'mqtt', 'mqtt_eventstream', 'logger', 'introduction', 'frontend', 'history')) @@ -243,25 +248,26 @@ def async_enable_logging(hass: core.HomeAssistant, verbose: bool=False, pass # Log errors to a file if we have write access to file or config dir + global ERROR_LOG_PATH if log_file is None: - err_log_path = hass.config.path(ERROR_LOG_FILENAME) + ERROR_LOG_PATH = hass.config.path(DEFAULT_LOG_FILENAME) else: - err_log_path = os.path.abspath(log_file) + ERROR_LOG_PATH = os.path.abspath(log_file) - err_path_exists = os.path.isfile(err_log_path) - err_dir = os.path.dirname(err_log_path) + err_path_exists = os.path.isfile(ERROR_LOG_PATH) + err_dir = os.path.dirname(ERROR_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 \ + if (err_path_exists and os.access(ERROR_LOG_PATH, os.W_OK)) or \ (not err_path_exists and os.access(err_dir, os.W_OK)): if log_rotate_days: err_handler = logging.handlers.TimedRotatingFileHandler( - err_log_path, when='midnight', backupCount=log_rotate_days) + ERROR_LOG_PATH, when='midnight', backupCount=log_rotate_days) else: err_handler = logging.FileHandler( - err_log_path, mode='w', delay=True) + ERROR_LOG_PATH, mode='w', delay=True) err_handler.setLevel(logging.INFO if verbose else logging.WARNING) err_handler.setFormatter(logging.Formatter(fmt, datefmt=datefmt)) @@ -283,7 +289,7 @@ def async_stop_async_handler(event): else: _LOGGER.error( - "Unable to setup error log %s (access denied)", err_log_path) + "Unable to setup error log %s (access denied)", ERROR_LOG_PATH) def mount_local_lib_path(config_dir: str) -> str: diff --git a/homeassistant/components/api.py b/homeassistant/components/api.py index c22683970bf90..9ad5126855d62 100644 --- a/homeassistant/components/api.py +++ b/homeassistant/components/api.py @@ -13,7 +13,7 @@ import homeassistant.core as ha import homeassistant.remote as rem -from homeassistant.bootstrap import ERROR_LOG_FILENAME +from homeassistant.bootstrap import ERROR_LOG_PATH from homeassistant.const import ( EVENT_HOMEASSISTANT_STOP, EVENT_TIME_CHANGED, HTTP_BAD_REQUEST, HTTP_CREATED, HTTP_NOT_FOUND, @@ -51,8 +51,7 @@ def setup(hass, config): hass.http.register_view(APIComponentsView) hass.http.register_view(APITemplateView) - hass.http.register_static_path( - URL_API_ERROR_LOG, hass.config.path(ERROR_LOG_FILENAME), False) + hass.http.register_static_path(URL_API_ERROR_LOG, ERROR_LOG_PATH, False) return True From 29ff9f5ec48604772aefb364ab98da1c4a4240bd Mon Sep 17 00:00:00 2001 From: Ted Drain Date: Thu, 14 Sep 2017 21:00:33 -0700 Subject: [PATCH 2/3] Only serve log file if logs are active --- homeassistant/components/api.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/api.py b/homeassistant/components/api.py index 9ad5126855d62..a32e45574c311 100644 --- a/homeassistant/components/api.py +++ b/homeassistant/components/api.py @@ -51,7 +51,9 @@ def setup(hass, config): hass.http.register_view(APIComponentsView) hass.http.register_view(APITemplateView) - hass.http.register_static_path(URL_API_ERROR_LOG, ERROR_LOG_PATH, False) + if ERROR_LOG_PATH is not None: + hass.http.register_static_path(URL_API_ERROR_LOG, ERROR_LOG_PATH, + False) return True From 1b86892d2606987e199d7d761b03678bd9b4935b Mon Sep 17 00:00:00 2001 From: Ted Drain Date: Fri, 15 Sep 2017 07:42:46 -0700 Subject: [PATCH 3/3] Changed log file location to be in hass.data --- homeassistant/bootstrap.py | 28 ++++++++++++++-------------- homeassistant/components/api.py | 8 ++++---- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 5a8f0d0537afe..3ff4d99fb9897 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -26,11 +26,10 @@ _LOGGER = logging.getLogger(__name__) -# After the logging is initialized, ERROR_LOG_PATH will be the full -# path to the log file. This is needed because components/api needs -# to access the log file location. -DEFAULT_LOG_FILENAME = 'home-assistant.log' -ERROR_LOG_PATH = None +ERROR_LOG_FILENAME = 'home-assistant.log' + +# hass.data key for logging information. +DATA_LOGGING = 'logging' FIRST_INIT_COMPONENT = set(( 'recorder', 'mqtt', 'mqtt_eventstream', 'logger', 'introduction', @@ -248,26 +247,25 @@ def async_enable_logging(hass: core.HomeAssistant, verbose: bool=False, pass # Log errors to a file if we have write access to file or config dir - global ERROR_LOG_PATH if log_file is None: - ERROR_LOG_PATH = hass.config.path(DEFAULT_LOG_FILENAME) + err_log_path = hass.config.path(ERROR_LOG_FILENAME) else: - ERROR_LOG_PATH = os.path.abspath(log_file) + err_log_path = os.path.abspath(log_file) - err_path_exists = os.path.isfile(ERROR_LOG_PATH) - err_dir = os.path.dirname(ERROR_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(ERROR_LOG_PATH, os.W_OK)) or \ + 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)): if log_rotate_days: err_handler = logging.handlers.TimedRotatingFileHandler( - ERROR_LOG_PATH, when='midnight', backupCount=log_rotate_days) + err_log_path, when='midnight', backupCount=log_rotate_days) else: err_handler = logging.FileHandler( - ERROR_LOG_PATH, mode='w', delay=True) + err_log_path, mode='w', delay=True) err_handler.setLevel(logging.INFO if verbose else logging.WARNING) err_handler.setFormatter(logging.Formatter(fmt, datefmt=datefmt)) @@ -287,9 +285,11 @@ def async_stop_async_handler(event): logger.addHandler(async_handler) logger.setLevel(logging.INFO) + # Save the log file location for access by other components. + hass.data[DATA_LOGGING] = err_log_path else: _LOGGER.error( - "Unable to setup error log %s (access denied)", ERROR_LOG_PATH) + "Unable to setup error log %s (access denied)", err_log_path) def mount_local_lib_path(config_dir: str) -> str: diff --git a/homeassistant/components/api.py b/homeassistant/components/api.py index a32e45574c311..3b905ab04205d 100644 --- a/homeassistant/components/api.py +++ b/homeassistant/components/api.py @@ -13,7 +13,7 @@ import homeassistant.core as ha import homeassistant.remote as rem -from homeassistant.bootstrap import ERROR_LOG_PATH +from homeassistant.bootstrap import DATA_LOGGING from homeassistant.const import ( EVENT_HOMEASSISTANT_STOP, EVENT_TIME_CHANGED, HTTP_BAD_REQUEST, HTTP_CREATED, HTTP_NOT_FOUND, @@ -51,9 +51,9 @@ def setup(hass, config): hass.http.register_view(APIComponentsView) hass.http.register_view(APITemplateView) - if ERROR_LOG_PATH is not None: - hass.http.register_static_path(URL_API_ERROR_LOG, ERROR_LOG_PATH, - False) + log_path = hass.data.get(DATA_LOGGING, None) + if log_path: + hass.http.register_static_path(URL_API_ERROR_LOG, log_path, False) return True