diff --git a/CveXplore/VERSION b/CveXplore/VERSION index 08456a47..d81f1c3f 100644 --- a/CveXplore/VERSION +++ b/CveXplore/VERSION @@ -1 +1 @@ -0.2.8 \ No newline at end of file +0.2.9 \ No newline at end of file diff --git a/CveXplore/database/maintenance/Config.py b/CveXplore/database/maintenance/Config.py index 045ef19c..20297bd2 100644 --- a/CveXplore/database/maintenance/Config.py +++ b/CveXplore/database/maintenance/Config.py @@ -2,6 +2,7 @@ Configuration ============= """ +import ast import datetime import json import os @@ -10,6 +11,11 @@ runPath = os.path.dirname(os.path.realpath(__file__)) +def getenv_bool(name: str, default: str = "False"): + raw = os.getenv(name, default).title() + return ast.literal_eval(raw) + + class Configuration(object): """ Class holding the configuration @@ -33,6 +39,8 @@ class Configuration(object): HTTP_PROXY = os.getenv("HTTP_PROXY", "") + LOGGING_TO_FILE = getenv_bool("LOGGING_TO_FILE", "False") + LOGGING_FILE_PATH = os.getenv("LOGGING_FILE_PATH", "./") LOGGING_MAX_FILE_SIZE = os.getenv("LOGGING_MAX_FILE_SIZE", "100MB") LOGGING_BACKLOG = os.getenv("LOGGING_BACKLOG", 5) LOGGING_FILE_NAME = os.getenv("LOGGING_FILE_NAME", "./update_populate.log") diff --git a/CveXplore/database/maintenance/LogHandler.py b/CveXplore/database/maintenance/LogHandler.py index 1926417a..19d708b8 100644 --- a/CveXplore/database/maintenance/LogHandler.py +++ b/CveXplore/database/maintenance/LogHandler.py @@ -27,15 +27,15 @@ class HelperLogger(logging.Logger): logger.logging class. """ - runPath = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) - - logPath = os.path.join(runPath, "log") - - if not os.path.exists(logPath): - os.makedirs(logPath) - config = Configuration() + if config.LOGGING_TO_FILE: + try: + if not os.path.exists(config.LOGGING_FILE_PATH): + os.makedirs(config.LOGGING_FILE_PATH) + except PermissionError: + logPath = os.path.expanduser("~") + logDict = { "version": 1, "formatters": { @@ -179,11 +179,12 @@ def __init__(self, name, level=logging.NOTSET): "%(asctime)s - %(name)-8s - %(levelname)-8s - %(message)s" ) - crf = RotatingFileHandler( - filename=self.config.getUpdateLogFile(), - maxBytes=self.config.getMaxLogSize(), - backupCount=self.config.getBacklog(), - ) - crf.setLevel(logging.DEBUG) - crf.setFormatter(formatter) - self.addHandler(crf) + if self.config.LOGGING_TO_FILE: + crf = RotatingFileHandler( + filename=os.path.join(self.logPath, self.config.getUpdateLogFile()), + maxBytes=self.config.getMaxLogSize(), + backupCount=self.config.getBacklog(), + ) + crf.setLevel(logging.DEBUG) + crf.setFormatter(formatter) + self.addHandler(crf)