diff --git a/Config/config.py b/Config/config.py index 45a57ca..cd28e5b 100644 --- a/Config/config.py +++ b/Config/config.py @@ -14,7 +14,6 @@ def get_config(config_file_path): :rtype: dict """ config = configparser.ConfigParser() - # read from file config.read(config_file_path, encoding="utf-8") config.__dict__ @@ -50,4 +49,39 @@ def configure_logging(): Configure logging for the application. """ logging_config = get_logging_config() + path = os.path.join("Config/logger.ini") + + if not os.path.isdir("Logs"): + os.makedirs("Logs") + if not os.path.exists(os.path.join("Logs", "traceback.log")): + with open(os.path.join("Logs", "traceback.log"), "w") as fp: + fp.write("Created traceback.log as part of tests.") + fp.close() + + # Define log format + log_format = logging.Formatter( + fmt="%(asctime)s %(levelname)-8s %(message)s", + datefmt="%Y-%m-%d %H:%M:%S", + ) + + # Set up console handler + console_handler = logging.StreamHandler() + console_handler.setLevel(logging.WARNING) + console_handler.setFormatter(log_format) + + # Set up file handler + file_handler = logging.FileHandler( + logging_config["handler_file"]["args"], + mode=logging_config["handler_file"]["mode"], + encoding=logging_config["handler_file"]["encoding"], + ) + file_handler.setLevel(logging.DEBUG) + file_handler.setFormatter(log_format) + + # Add handlers to root logger + logging.root.addHandler(console_handler) + logging.root.addHandler(file_handler) + + # Set root logger level + logging.root.setLevel(logging.DEBUG) return logging_config diff --git a/Config/logger.ini b/Config/logger.ini index 8c44020..02dee1b 100644 --- a/Config/logger.ini +++ b/Config/logger.ini @@ -24,11 +24,20 @@ level = INFO formatter = default args = (sys.stdout,) +; [handler_file] +; class = logging.FileHandler +; level = DEBUG +; formatter = default +; args = ('Logs/traceback.log', 'a', 1000000, 5) +; mode = a + [handler_file] -class = logging.FileHandler -level = INFO +class = handlers.RotatingFileHandler +args = Logs/traceback.log +level = DEBUG formatter = default -args = ('Logs/traceback.log', 'a') +mode = a +encoding = utf8 [formatter_default] format = %(asctime)s - %(name)s - %(levelname)s - %(message)s diff --git a/IP/ip_reputation_checker.py b/IP/ip_reputation_checker.py index e596997..d26d57e 100644 --- a/IP/ip_reputation_checker.py +++ b/IP/ip_reputation_checker.py @@ -46,7 +46,7 @@ def checkIPReputationVT(self, ip_address): headers: dict[str, str] = {"x-apikey": vtkey} response = requests.request("GET", url, headers=headers, data=payload).text data = json.loads(response) - print( + logging.info( json.dumps( data["data"]["attributes"]["total_votes"], indent=4, sort_keys=True ) diff --git a/Tests/Config/test_config.py b/Tests/Config/test_config.py index 6469463..d241214 100644 --- a/Tests/Config/test_config.py +++ b/Tests/Config/test_config.py @@ -12,7 +12,7 @@ def setUp(self): os.mkdir("Logs") if not os.path.exists(os.path.join("Logs", "traceback.log")): with open(os.path.join("Logs", "traceback.log"), "w") as fp: - fp.write("Created traceback.log as part of tests.") + fp.write("Created traceback.log as part of tests.\n") fp.close() def test_config_exists(self): @@ -21,7 +21,7 @@ def test_config_exists(self): def test_config_is_valid(self): parser = ConfigParser() if self.test_config_exists: - parser.read(self.path, encoding='utf-8') + parser.read(self.path, encoding="utf-8") # Check if the required sections are present in the config self.assertIn("formatters", parser.sections()) self.assertIn("handler_console", parser.sections())