From f291b321c724ebf820c78d3b71e60b3a81e2eda1 Mon Sep 17 00:00:00 2001 From: Alfredo Deza Date: Thu, 22 Jun 2017 15:36:34 -0400 Subject: [PATCH] ceph-volume: log: be more robust, report back to config Signed-off-by: Alfredo Deza --- src/ceph-volume/ceph_volume/log.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/ceph-volume/ceph_volume/log.py b/src/ceph-volume/ceph_volume/log.py index 78e26a1124c..6a12e670846 100644 --- a/src/ceph-volume/ceph_volume/log.py +++ b/src/ceph-volume/ceph_volume/log.py @@ -1,23 +1,33 @@ -from datetime import datetime import logging import os +from ceph_volume import config BASE_FORMAT = "[%(name)s][%(levelname)-6s] %(message)s" FILE_FORMAT = "[%(asctime)s]" + BASE_FORMAT -def setup(config=None): +def setup(config=None, name='ceph-volume.log'): + # if a non-root user calls help or other no-sudo-required command the + # logger will fail to write to /var/lib/ceph/ so this /tmp/ path is used as + # a fallback + tmp_log_file = os.path.join('/tmp/', name) root_logger = logging.getLogger() - log_path = config.get('--log-path', '/var/log/ceph/') - if not os.path.exists(log_path): - raise RuntimeError('configured ``--log-path`` value does not exist: %s' % log_path) - date = datetime.strftime(datetime.utcnow(), '%Y-%m-%d') - log_file = os.path.join(log_path, 'ceph-volume-%s.log' % date) + # The default path is where all ceph log files are, and will get rotated by + # Ceph's logrotate rules. + default_log_path = os.environ.get('CEPH_VOLUME_LOG_PATH', '/var/log/ceph/') + log_path = config.get('--log-path', default_log_path) + log_file = os.path.join(log_path, 'ceph-volume.log') root_logger.setLevel(logging.DEBUG) # File Logger - fh = logging.FileHandler(log_file) + config['log_path'] = log_file + try: + fh = logging.FileHandler(log_file) + except (OSError, IOError): + config['log_path'] = tmp_log_file + fh = logging.FileHandler(tmp_log_file) + fh.setLevel(logging.DEBUG) fh.setFormatter(logging.Formatter(FILE_FORMAT))