From d7036f7be6c18710e7aa39efae7b366c98d11665 Mon Sep 17 00:00:00 2001 From: Jonatan Schlag Date: Mon, 7 May 2018 16:53:36 +0200 Subject: [PATCH] Add logging to a general log file Signed-off-by: Jonatan Schlag --- nitsi.in | 15 +++++++++------ src/nitsi/logger.py | 37 +++++++++++++++++++++++++++++++++++++ src/nitsi/test.py | 2 +- 3 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 src/nitsi/logger.py diff --git a/nitsi.in b/nitsi.in index 460487d..2090e0d 100755 --- 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 index 0000000..f492bda --- /dev/null +++ b/src/nitsi/logger.py @@ -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 diff --git a/src/nitsi/test.py b/src/nitsi/test.py index 0ddcdab..f67ceb5 100755 --- a/src/nitsi/test.py +++ b/src/nitsi/test.py @@ -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)) -- 2.47.2