Improve log output
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Mon, 28 May 2018 13:03:33 +0000 (15:03 +0200)
committerJonatan Schlag <jonatan.schlag@ipfire.org>
Mon, 28 May 2018 13:03:33 +0000 (15:03 +0200)
Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
nitsi.in
src/nitsi/logger.py
src/nitsi/serial_connection.py

index fc1ce8e..f12a5a4 100755 (executable)
--- a/nitsi.in
+++ b/nitsi.in
@@ -3,7 +3,7 @@
 import argparse
 import logging
 
-from nitsi.logger import init_logging
+from nitsi.logger import init_logging, Log_Formatter
 from nitsi.recipe import RecipeExeption
 from nitsi.test import TestException, test
 
@@ -13,7 +13,7 @@ logger.setLevel(logging.DEBUG)
 ch = logging.StreamHandler()
 ch.setLevel(logging.DEBUG)
 
-formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
+formatter = Log_Formatter()
 ch.setFormatter(formatter)
 # add the handlers to the logger
 logger.addHandler(ch)
index 7990732..4868fda 100644 (file)
@@ -73,3 +73,21 @@ class TestFormatter(logging.Formatter):
         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
index 3471ad3..3e65225 100644 (file)
@@ -23,14 +23,23 @@ class serial_connection():
         self.con = serial.Serial(device)
 
         self.log_output = self.log.getChild("output")
+        # Do not propagate the output to ancestor loggers as it looks ugly
+        self.log_output.propagate = False
+        # Logging handler for file
         log_file_handler = logging.FileHandler(self.log_file)
         log_file_handler.setLevel(logging.INFO)
         log_file_handler.terminator = ""
+        # Loggin Handler for Stream
+        stream_handler = logging.StreamHandler()
+        stream_handler.setLevel(logging.INFO)
+        stream_handler.terminator = ""
         formatter = logger.TestFormatter(name=self.name,
                                     start_time=log_start_time,
                                     longest_machine_name=longest_machine_name)
         log_file_handler.setFormatter(formatter)
+        stream_handler.setFormatter(formatter)
         self.log_output.addHandler(log_file_handler)
+        self.log_output.addHandler(stream_handler)
 
     def read(self, size=1):
         if len(self.buffer) >= size:
@@ -94,7 +103,7 @@ class serial_connection():
     def log_console_line(self, line):
         self.log.debug("Get in function log_console_line()")
         self.log_output.info(line)
-        sys.stdout.write(line)
+        #sys.stdout.write(line)
 
     @property
     def in_waiting(self):