Add logging to a general log file
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Mon, 7 May 2018 14:53:36 +0000 (16:53 +0200)
committerJonatan Schlag <jonatan.schlag@ipfire.org>
Mon, 7 May 2018 14:53:36 +0000 (16:53 +0200)
Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
nitsi.in
src/nitsi/logger.py [new file with mode: 0644]
src/nitsi/test.py

index 460487d..2090e0d 100755 (executable)
--- a/nitsi.in
+++ b/nitsi.in
@@ -1,22 +1,18 @@
 #!/usr/bin/python3
 
 from nitsi.test import test
+from nitsi.logger import init_logging
 import logging
 
 logger = logging.getLogger("nitsi")
 logger.setLevel(logging.DEBUG)
-# create file handler which logs even debug messages
-fh = logging.FileHandler('nitsi.log')
-fh.setLevel(logging.DEBUG)
 # create console handler with a higher log level
 ch = logging.StreamHandler()
 ch.setLevel(logging.DEBUG)
 
 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
-fh.setFormatter(formatter)
 ch.setFormatter(formatter)
 # add the handlers to the logger
-logger.addHandler(fh)
 logger.addHandler(ch)
 
 if __name__ == "__main__":
@@ -34,7 +30,14 @@ if __name__ == "__main__":
     if args.version:
         logger.info("nitsi version: {}".format("@PACKAGE_VERSION@"))
     else:
-        currenttest = test(args.dir)
+        log_dir = init_logging(args.dir)
+        # We now going to log everything to log_dir/genaral.log
+        fh = logging.FileHandler("{}/general.log".format(log_dir))
+        fh.setLevel(logging.DEBUG)
+        logger.addHandler(fh)
+        logger.debug("We now logging everything to {}/general.log".format(log_dir))
+
+        currenttest = test(args.dir, log_dir)
         currenttest.read_settings()
         currenttest.virtual_environ_setup()
         currenttest.load_recipe()
diff --git a/src/nitsi/logger.py b/src/nitsi/logger.py
new file mode 100644 (file)
index 0000000..f492bda
--- /dev/null
@@ -0,0 +1,37 @@
+#!/usr/bin/python3
+
+import logging
+import os
+import time
+
+logger = logging.getLogger("nitsi.logger")
+
+class Logger_Exeception(BaseException):
+    pass
+
+# This function should create the necessary folders
+# and touch the logging files
+def init_logging(path):
+    logger.debug("Init logging directory")
+    if not os.path.isdir(path):
+        logger.error("{} is not a valid directory".format(path))
+
+    try:
+        path = os.path.abspath(path)
+    except BaseException as e:
+        logger.error("Failed to get the absolute path for: {}".format(path))
+
+    log_dir = "{}/log".format(path)
+
+    if not os.path.exists(log_dir):
+        os.mkdir(log_dir)
+
+    time_dir = log_dir + "/" + time.strftime("%Y-%m-%d_%H-%M-%S" ,time.gmtime(time.time()))
+
+    if os.path.exists(time_dir):
+        logger.error("Path {} alreday exist".format(time_dir))
+        raise Logger_Exeception
+    else:
+        os.mkdir(time_dir)
+
+    return time_dir
index 0ddcdab..f67ceb5 100755 (executable)
@@ -15,7 +15,7 @@ import logging
 logger = logging.getLogger("nitsi.test")
 
 class test():
-    def __init__(self, path):
+    def __init__(self, path, log_path):
         try:
             self.path = os.path.abspath(path)
             self.log = logger.getChild(os.path.basename(self.path))