Skip to content

Commit

Permalink
Fixing bug that doesn't log to file (#53)
Browse files Browse the repository at this point in the history
* initial commit to add file handler

* fixed file handler bug

* black-ified 💥 💔 💥

---------

Co-authored-by: kash <kashvijayperth@gmail.com>
  • Loading branch information
initd1 and kash authored Apr 14, 2023
1 parent 70fa138 commit 8b1b849
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
36 changes: 35 additions & 1 deletion Config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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
15 changes: 12 additions & 3 deletions Config/logger.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion IP/ip_reputation_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions Tests/Config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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())
Expand Down

0 comments on commit 8b1b849

Please sign in to comment.