summaryrefslogtreecommitdiffstats
path: root/src/ceph-volume/ceph_volume/log.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
commit19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch)
tree42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/ceph-volume/ceph_volume/log.py
parentInitial commit. (diff)
downloadceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.tar.xz
ceph-19fcec84d8d7d21e796c7624e521b60d28ee21ed.zip
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/ceph-volume/ceph_volume/log.py')
-rw-r--r--src/ceph-volume/ceph_volume/log.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/ceph-volume/ceph_volume/log.py b/src/ceph-volume/ceph_volume/log.py
new file mode 100644
index 000000000..b283bedbb
--- /dev/null
+++ b/src/ceph-volume/ceph_volume/log.py
@@ -0,0 +1,49 @@
+import logging
+import os
+from ceph_volume import terminal
+from ceph_volume import conf
+
+BASE_FORMAT = "[%(name)s][%(levelname)-6s] %(message)s"
+FILE_FORMAT = "[%(asctime)s]" + BASE_FORMAT
+
+
+def setup(name='ceph-volume.log', log_path=None, log_level=None):
+ log_path = log_path or conf.log_path
+ # 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()
+ # The default path is where all ceph log files are, and will get rotated by
+ # Ceph's logrotate rules.
+ log_level = log_level or "DEBUG"
+ log_level = getattr(logging, log_level.upper())
+ root_logger.setLevel(log_level)
+
+ try:
+ fh = logging.FileHandler(log_path)
+ except (OSError, IOError) as err:
+ terminal.warning("Falling back to /tmp/ for logging. Can't use %s" % log_path)
+ terminal.warning(str(err))
+ conf.log_path = tmp_log_file
+ fh = logging.FileHandler(tmp_log_file)
+
+ fh.setLevel(log_level)
+ fh.setFormatter(logging.Formatter(FILE_FORMAT))
+
+ root_logger.addHandler(fh)
+
+
+def setup_console():
+ # TODO: At some point ceph-volume should stop using the custom logger
+ # interface that exists in terminal.py and use the logging module to
+ # produce output for the terminal
+ # Console Logger
+ sh = logging.StreamHandler()
+ sh.setFormatter(logging.Formatter('[terminal] %(message)s'))
+ sh.setLevel(logging.DEBUG)
+
+ terminal_logger = logging.getLogger('terminal')
+
+ # allow all levels at root_logger, handlers control individual levels
+ terminal_logger.addHandler(sh)