Added log-file command line flag (#9422)

This commit is contained in:
Ted Drain 2017-09-13 21:22:42 -07:00 committed by Paulus Schoutsen
parent 89d6784fa0
commit 411c9620c1
2 changed files with 29 additions and 13 deletions

View File

@ -126,6 +126,12 @@ def get_arguments() -> argparse.Namespace:
type=int, type=int,
default=None, default=None,
help='Enables daily log rotation and keeps up to the specified days') 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( parser.add_argument(
'--runner', '--runner',
action='store_true', action='store_true',
@ -256,13 +262,14 @@ def setup_and_run_hass(config_dir: str,
} }
hass = bootstrap.from_config_dict( hass = bootstrap.from_config_dict(
config, config_dir=config_dir, verbose=args.verbose, 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: else:
config_file = ensure_config_file(config_dir) config_file = ensure_config_file(config_dir)
print('Config directory:', config_dir) print('Config directory:', config_dir)
hass = bootstrap.from_config_file( hass = bootstrap.from_config_file(
config_file, verbose=args.verbose, skip_pip=args.skip_pip, 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: if hass is None:
return None return None

View File

@ -38,7 +38,8 @@ def from_config_dict(config: Dict[str, Any],
enable_log: bool=True, enable_log: bool=True,
verbose: bool=False, verbose: bool=False,
skip_pip: bool=False, skip_pip: bool=False,
log_rotate_days: Any=None) \ log_rotate_days: Any=None,
log_file: Any=None) \
-> Optional[core.HomeAssistant]: -> Optional[core.HomeAssistant]:
"""Try to configure Home Assistant from a configuration dictionary. """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( hass = hass.loop.run_until_complete(
async_from_config_dict( async_from_config_dict(
config, hass, config_dir, enable_log, verbose, skip_pip, config, hass, config_dir, enable_log, verbose, skip_pip,
log_rotate_days) log_rotate_days, log_file)
) )
return hass return hass
@ -69,7 +70,8 @@ def async_from_config_dict(config: Dict[str, Any],
enable_log: bool=True, enable_log: bool=True,
verbose: bool=False, verbose: bool=False,
skip_pip: bool=False, skip_pip: bool=False,
log_rotate_days: Any=None) \ log_rotate_days: Any=None,
log_file: Any=None) \
-> Optional[core.HomeAssistant]: -> Optional[core.HomeAssistant]:
"""Try to configure Home Assistant from a configuration dictionary. """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) yield from hass.async_add_job(conf_util.process_ha_config_upgrade, hass)
if enable_log: 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 hass.config.skip_pip = skip_pip
if skip_pip: if skip_pip:
@ -153,7 +155,8 @@ def from_config_file(config_path: str,
hass: Optional[core.HomeAssistant]=None, hass: Optional[core.HomeAssistant]=None,
verbose: bool=False, verbose: bool=False,
skip_pip: bool=True, 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. """Read the configuration file and try to start all the functionality.
Will add functionality to 'hass' parameter if given, Will add functionality to 'hass' parameter if given,
@ -165,7 +168,7 @@ def from_config_file(config_path: str,
# run task # run task
hass = hass.loop.run_until_complete( hass = hass.loop.run_until_complete(
async_from_config_file( 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 return hass
@ -176,7 +179,8 @@ def async_from_config_file(config_path: str,
hass: core.HomeAssistant, hass: core.HomeAssistant,
verbose: bool=False, verbose: bool=False,
skip_pip: bool=True, 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. """Read the configuration file and try to start all the functionality.
Will add functionality to 'hass' parameter. Will add functionality to 'hass' parameter.
@ -187,7 +191,7 @@ def async_from_config_file(config_path: str,
hass.config.config_dir = config_dir hass.config.config_dir = config_dir
yield from async_mount_local_lib_path(config_dir, hass.loop) 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: try:
config_dict = yield from hass.async_add_job( config_dict = yield from hass.async_add_job(
@ -205,7 +209,7 @@ def async_from_config_file(config_path: str,
@core.callback @core.callback
def async_enable_logging(hass: core.HomeAssistant, verbose: bool=False, 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. """Set up the logging.
This method must be run in the event loop. This method must be run in the event loop.
@ -239,13 +243,18 @@ def async_enable_logging(hass: core.HomeAssistant, verbose: bool=False,
pass pass
# Log errors to a file if we have write access to file or config dir # 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_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 # Check if we can write to the error log if it exists or that
# we can create files in the containing directory if not. # 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(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: if log_rotate_days:
err_handler = logging.handlers.TimedRotatingFileHandler( err_handler = logging.handlers.TimedRotatingFileHandler(