]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-volume terminal create a logger to get terminal+log messages in one call
authorAlfredo Deza <adeza@redhat.com>
Thu, 1 Feb 2018 20:32:54 +0000 (15:32 -0500)
committerAndrew Schoen <aschoen@redhat.com>
Wed, 7 Feb 2018 14:46:46 +0000 (08:46 -0600)
Signed-off-by: Alfredo Deza <adeza@redhat.com>
(cherry picked from commit f6dd0ff15f0aeda9f660e923153f1d3dae68e583)

src/ceph-volume/ceph_volume/terminal.py

index 55ce2d4ca280946b90e2ad5bcf23890430d4589b..23ce57e0e9c2b5d8631e93a4e48c92c751e44b96 100644 (file)
@@ -1,3 +1,4 @@
+import logging
 import sys
 
 
@@ -114,6 +115,14 @@ def error(msg):
     return _Write(prefix=red_arrow).raw(msg)
 
 
+def info(msg):
+    return _Write(prefix=blue_arrow).raw(msg)
+
+
+def debug(msg):
+    return _Write(prefix=blue_arrow).raw(msg)
+
+
 def warning(msg):
     return _Write(prefix=yellow_arrow).raw(msg)
 
@@ -122,6 +131,48 @@ def success(msg):
     return _Write(prefix=green_arrow).raw(msg)
 
 
+class MultiLogger(object):
+    """
+    Proxy class to be able to report on both logger instances and terminal
+    messages avoiding the issue of having to call them both separately
+
+    Initialize it in the same way a logger object::
+
+        logger = terminal.MultiLogger(__name__)
+    """
+
+    def __init__(self, name):
+        self.logger = logging.getLogger(name)
+
+    def _make_record(self, msg, *args):
+        if len(str(args)):
+            try:
+                return msg % args
+            except TypeError:
+                self.logger.exception('unable to produce log record: %s' % msg)
+        return msg
+
+    def warning(self, msg, *args):
+        record = self._make_record(msg, *args)
+        warning(record)
+        self.logger.warning(record)
+
+    def debug(self, msg, *args):
+        record = self._make_record(msg, *args)
+        debug(record)
+        self.logger.debug(record)
+
+    def info(self, msg, *args):
+        record = self._make_record(msg, *args)
+        info(record)
+        self.logger.info(record)
+
+    def error(self, msg, *args):
+        record = self._make_record(msg, *args)
+        error(record)
+        self.logger.error(record)
+
+
 def dispatch(mapper, argv=None):
     argv = argv or sys.argv
     for count, arg in enumerate(argv, 1):