Skip to content

Commit

Permalink
added log setup function with rotate
Browse files Browse the repository at this point in the history
  • Loading branch information
itsFelixH committed Jan 15, 2024
1 parent a61f29b commit 4c59bd0
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions pattrmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
from io import StringIO
import xml.etree.ElementTree as ET
import logging
from logging.handlers import RotatingFileHandler
import sys

LOG_DIR = "data/logs"
MAIN_LOG = "pattrmm.log"

# functions
def verify_or_create_folder(folder_path, folder_name_for_logging):
Expand All @@ -47,6 +50,21 @@ def verify_or_create_file(file_path, file_name_for_logging):
else:
print(file_name_for_logging.capitalize() + " file present...")

def log_setup():
"""Setup log formatter and handler"""
log_path = LOG_DIR + '/' + MAIN_LOG
need_roll = os.path.isfile(log_path)

logger = logging.getLogger()
logger.setLevel(logging.INFO)
log_handler = RotatingFileHandler(log_path, backupCount=5)
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s: %(message)s', "%Y-%m-%d %H:%M:%S")
log_handler.setFormatter(log_formatter)
logger.addHandler(log_handler)

# roll log if already present
if need_roll:
logger.handlers[0].doRollover()


print("Verifying files...")
Expand All @@ -57,14 +75,9 @@ def verify_or_create_file(file_path, file_name_for_logging):
# history folder for timestamps
verify_or_create_folder("./data/history", "stats")

# logs folder
# logs setup
verify_or_create_folder("data/logs", "logs")

# pattrmm log file
log_file = "data/logs/pattrmm.log"
verify_or_create_file(log_file, "log")

logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s: %(message)s', datefmt="%Y-%m-%d %H:%M:%S")
log_setup()

# preferences folder
verify_or_create_folder("preferences", "preferences")
Expand Down

0 comments on commit 4c59bd0

Please sign in to comment.