Skip to content

Commit

Permalink
Changes in log handling
Browse files Browse the repository at this point in the history
  • Loading branch information
P-T-I committed Jan 13, 2023
1 parent bb27339 commit 0434cdd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CveXplore/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.8
0.2.9
8 changes: 8 additions & 0 deletions CveXplore/database/maintenance/Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Configuration
=============
"""
import ast
import datetime
import json
import os
Expand All @@ -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
Expand All @@ -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")
Expand Down
31 changes: 16 additions & 15 deletions CveXplore/database/maintenance/LogHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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)

0 comments on commit 0434cdd

Please sign in to comment.