From c9d8639e2a31bbb5c07669a36bbd51a199355e1b Mon Sep 17 00:00:00 2001 From: caoyuan Date: Tue, 23 Jan 2018 13:57:11 +0800 Subject: [PATCH] Optimise the code for log directory created Change-Id: Icfab4d3e916c570b2887944cfc7a907f8991bfe2 --- freezer/common/config.py | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/freezer/common/config.py b/freezer/common/config.py index 618f8886..fb9b9919 100644 --- a/freezer/common/config.py +++ b/freezer/common/config.py @@ -579,29 +579,24 @@ def get_backup_args(): if not CONF.get('log_file'): log_file = None - for file_name in ['/var/log/freezer-agent/freezer-agent.log', - '/var/log/freezer.log']: - try: - log_file = prepare_logging(file_name) - except IOError: - pass + file_name = '/var/log/freezer-agent/freezer-agent.log' + try: + log_file = prepare_logging(file_name) + except IOError: + pass if not log_file: # Set default working directory to ~/.freezer. If the directory # does not exists it is created - work_dir = os.path.join(home, '.freezer') - if not os.path.exists(work_dir): - try: - os.makedirs(work_dir) - log_file = prepare_logging(os.path.join(work_dir, - 'freezer.log')) - except (OSError, IOError) as err_msg: - # This avoids freezer-agent to crash if it can't write to - # ~/.freezer, which may happen on some env (for me, - # it happens in Jenkins, as freezer-agent can't write to - # /var/lib/jenkins). - print(encodeutils.safe_decode('{}'.format(err_msg)), - file=sys.stderr) + try: + log_file = prepare_logging() + except (OSError, IOError) as err_msg: + # This avoids freezer-agent to crash if it can't write to + # ~/.freezer, which may happen on some env (for me, + # it happens in Jenkins, as freezer-agent can't write to + # /var/lib/jenkins). + print(encodeutils.safe_decode('{}'.format(err_msg)), + file=sys.stderr) if log_file: CONF.set_default('log_file', log_file) else: