]> git.ipfire.org Git - people/ms/nitsi.git/blobdiff - src/nitsi/logger.py
Add logging to a general log file
[people/ms/nitsi.git] / src / nitsi / logger.py
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