]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-volume: log: be more robust, report back to config
authorAlfredo Deza <adeza@redhat.com>
Thu, 22 Jun 2017 19:36:34 +0000 (15:36 -0400)
committerAlfredo Deza <adeza@redhat.com>
Fri, 4 Aug 2017 14:25:57 +0000 (10:25 -0400)
Signed-off-by: Alfredo Deza <adeza@redhat.com>
src/ceph-volume/ceph_volume/log.py

index 78e26a1124c38b7e9c39e9d6228ad71705b54a1c..6a12e670846ce097376d7d7fcdd950d8ff230792 100644 (file)
@@ -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))