Added log-file command line flag (#9422)
This commit is contained in:
parent
89d6784fa0
commit
411c9620c1
2 changed files with 29 additions and 13 deletions
|
@ -126,6 +126,12 @@ def get_arguments() -> argparse.Namespace:
|
|||
type=int,
|
||||
default=None,
|
||||
help='Enables daily log rotation and keeps up to the specified days')
|
||||
parser.add_argument(
|
||||
'--log-file',
|
||||
type=str,
|
||||
default=None,
|
||||
help='Log file to write to. If not set, CONFIG/home-assistant.log '
|
||||
'is used')
|
||||
parser.add_argument(
|
||||
'--runner',
|
||||
action='store_true',
|
||||
|
@ -256,13 +262,14 @@ def setup_and_run_hass(config_dir: str,
|
|||
}
|
||||
hass = bootstrap.from_config_dict(
|
||||
config, config_dir=config_dir, verbose=args.verbose,
|
||||
skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days)
|
||||
skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days,
|
||||
log_file=args.log_file)
|
||||
else:
|
||||
config_file = ensure_config_file(config_dir)
|
||||
print('Config directory:', config_dir)
|
||||
hass = bootstrap.from_config_file(
|
||||
config_file, verbose=args.verbose, skip_pip=args.skip_pip,
|
||||
log_rotate_days=args.log_rotate_days)
|
||||
log_rotate_days=args.log_rotate_days, log_file=args.log_file)
|
||||
|
||||
if hass is None:
|
||||
return None
|
||||
|
|
|
@ -38,7 +38,8 @@ def from_config_dict(config: Dict[str, Any],
|
|||
enable_log: bool=True,
|
||||
verbose: bool=False,
|
||||
skip_pip: bool=False,
|
||||
log_rotate_days: Any=None) \
|
||||
log_rotate_days: Any=None,
|
||||
log_file: Any=None) \
|
||||
-> Optional[core.HomeAssistant]:
|
||||
"""Try to configure Home Assistant from a configuration dictionary.
|
||||
|
||||
|
@ -56,7 +57,7 @@ def from_config_dict(config: Dict[str, Any],
|
|||
hass = hass.loop.run_until_complete(
|
||||
async_from_config_dict(
|
||||
config, hass, config_dir, enable_log, verbose, skip_pip,
|
||||
log_rotate_days)
|
||||
log_rotate_days, log_file)
|
||||
)
|
||||
|
||||
return hass
|
||||
|
@ -69,7 +70,8 @@ def async_from_config_dict(config: Dict[str, Any],
|
|||
enable_log: bool=True,
|
||||
verbose: bool=False,
|
||||
skip_pip: bool=False,
|
||||
log_rotate_days: Any=None) \
|
||||
log_rotate_days: Any=None,
|
||||
log_file: Any=None) \
|
||||
-> Optional[core.HomeAssistant]:
|
||||
"""Try to configure Home Assistant from a configuration dictionary.
|
||||
|
||||
|
@ -88,7 +90,7 @@ def async_from_config_dict(config: Dict[str, Any],
|
|||
yield from hass.async_add_job(conf_util.process_ha_config_upgrade, hass)
|
||||
|
||||
if enable_log:
|
||||
async_enable_logging(hass, verbose, log_rotate_days)
|
||||
async_enable_logging(hass, verbose, log_rotate_days, log_file)
|
||||
|
||||
hass.config.skip_pip = skip_pip
|
||||
if skip_pip:
|
||||
|
@ -153,7 +155,8 @@ def from_config_file(config_path: str,
|
|||
hass: Optional[core.HomeAssistant]=None,
|
||||
verbose: bool=False,
|
||||
skip_pip: bool=True,
|
||||
log_rotate_days: Any=None):
|
||||
log_rotate_days: Any=None,
|
||||
log_file: Any=None):
|
||||
"""Read the configuration file and try to start all the functionality.
|
||||
|
||||
Will add functionality to 'hass' parameter if given,
|
||||
|
@ -165,7 +168,7 @@ def from_config_file(config_path: str,
|
|||
# run task
|
||||
hass = hass.loop.run_until_complete(
|
||||
async_from_config_file(
|
||||
config_path, hass, verbose, skip_pip, log_rotate_days)
|
||||
config_path, hass, verbose, skip_pip, log_rotate_days, log_file)
|
||||
)
|
||||
|
||||
return hass
|
||||
|
@ -176,7 +179,8 @@ def async_from_config_file(config_path: str,
|
|||
hass: core.HomeAssistant,
|
||||
verbose: bool=False,
|
||||
skip_pip: bool=True,
|
||||
log_rotate_days: Any=None):
|
||||
log_rotate_days: Any=None,
|
||||
log_file: Any=None):
|
||||
"""Read the configuration file and try to start all the functionality.
|
||||
|
||||
Will add functionality to 'hass' parameter.
|
||||
|
@ -187,7 +191,7 @@ def async_from_config_file(config_path: str,
|
|||
hass.config.config_dir = config_dir
|
||||
yield from async_mount_local_lib_path(config_dir, hass.loop)
|
||||
|
||||
async_enable_logging(hass, verbose, log_rotate_days)
|
||||
async_enable_logging(hass, verbose, log_rotate_days, log_file)
|
||||
|
||||
try:
|
||||
config_dict = yield from hass.async_add_job(
|
||||
|
@ -205,7 +209,7 @@ def async_from_config_file(config_path: str,
|
|||
|
||||
@core.callback
|
||||
def async_enable_logging(hass: core.HomeAssistant, verbose: bool=False,
|
||||
log_rotate_days=None) -> None:
|
||||
log_rotate_days=None, log_file=None) -> None:
|
||||
"""Set up the logging.
|
||||
|
||||
This method must be run in the event loop.
|
||||
|
@ -239,13 +243,18 @@ 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
|
||||
err_log_path = hass.config.path(ERROR_LOG_FILENAME)
|
||||
if log_file is None:
|
||||
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)
|
||||
|
||||
# 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(hass.config.config_dir, os.W_OK)):
|
||||
(not err_path_exists and os.access(err_dir, os.W_OK)):
|
||||
|
||||
if log_rotate_days:
|
||||
err_handler = logging.handlers.TimedRotatingFileHandler(
|
||||
|
|
Loading…
Add table
Reference in a new issue