]> git.ipfire.org Git - nitsi.git/blobdiff - src/nitsi/logger.py
Improve log output
[nitsi.git] / src / nitsi / logger.py
index 8f90651e6c8296e04ffe1ddd5ae1919aaba1595e..4868fdab056107ab456231d37da21d25516fb6dc 100644 (file)
@@ -38,7 +38,7 @@ def init_logging(path):
 
 
 class TestFormatter(logging.Formatter):
-    def __init__(self, start_time=None, name=None):
+    def __init__(self, start_time=None, name=None, longest_machine_name=10):
         super().__init__(fmt="[%(asctime)s] %(message)s")
         logger.debug("Initiating TestFormatter for")
         if start_time == None:
@@ -51,6 +51,8 @@ class TestFormatter(logging.Formatter):
         else:
             self.name = name
 
+        self.longest_machine_name = longest_machine_name
+
     def converter(self, recordtime):
 
         # This returns a timestamp relatively to the time when we started
@@ -61,10 +63,31 @@ class TestFormatter(logging.Formatter):
         return time.gmtime(recordtime)
 
     def format(self, record):
-        return "[{}][{}] {}".format(self.formatTime(record), self.name, record.getMessage())
+        return "[{}][{:^{align}}] {}".format(self.formatTime(record),
+                                                self.name,
+                                                record.getMessage(),
+                                                align=self.longest_machine_name)
 
     def formatTime(self, record, datefmt=None):
         ct = self.converter(record.created)
         t = time.strftime("%H:%M:%S", ct)
         s = "{}.{:03d}".format(t, round(record.msecs,None))
         return s
+
+
+class Log_Formatter(logging.Formatter):
+    def __init__(self):
+        super().__init__(fmt="[%(asctime)s] [%(levelname)s] %(message)s")
+        logger.debug("Initiating Log_Formatter")
+
+    def format(self, record):
+        # We use 8 to align the levelname, because CRITICAL ist the longest name and has 8 chars
+        return "[{}][{:^}][{:^8}] {}".format(self.formatTime(record),
+                                                record.name,
+                                                record.levelname,
+                                                record.getMessage())
+
+    def formatTime(self, record, datefmt=None):
+        t = time.strftime("%H:%M:%S", time.gmtime(record.created))
+        s = "{}.{:03d}".format(t, round(record.msecs,None))
+        return s
\ No newline at end of file