Better logdir handling

This commit is contained in:
Corban-Lee Jones 2024-02-07 09:22:01 +00:00
parent df15ad07a5
commit d891ca374b

View File

@ -20,9 +20,8 @@ log = logging.getLogger(__name__)
class LogSetup:
def __init__(self, BASE_DIR: Path):
self.BASE_DIR = BASE_DIR
self.LOGS_DIR = BASE_DIR / "logs/"
def __init__(self, logs_dir: Path):
self.logs_dir = logs_dir
def _open_file(self) -> TextIO:
"""
@ -30,7 +29,7 @@ class LogSetup:
"""
# Create the logs directory if it doesnt exist
self.LOGS_DIR.mkdir(exist_ok=True)
self.logs_dir.mkdir(exist_ok=True)
# Create a generator to generate a unique filename
timestamp = datetime.now().strftime(LOG_FILENAME_FORMAT_PREFIX)
@ -39,7 +38,7 @@ class LogSetup:
# Find a filename that doesn't already exist and return it
for filename in filenames:
try:
return (self.LOGS_DIR / filename).open("x", encoding="utf-8")
return (self.logs_dir / filename).open("x", encoding="utf-8")
except FileExistsError:
continue
@ -48,7 +47,7 @@ class LogSetup:
Search through the logs directory and delete any expired log files.
"""
for path in self.LOGS_DIR.glob('*.txt'):
for path in self.logs_dir.glob('*.txt'):
prefix = path.stem.split('_')[0]
try:
log_date = datetime.strptime(prefix, LOG_FILENAME_FORMAT_PREFIX)