From 708075fd3c75765d5b493637745199ab074602e8 Mon Sep 17 00:00:00 2001 From: Peter Yefi Date: Mon, 16 Jan 2023 14:11:08 -0500 Subject: [PATCH] create log file automatically if it doesn't exist --- hub_logger/__init__.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/hub_logger/__init__.py b/hub_logger/__init__.py index e440afbf..e4d66f89 100644 --- a/hub_logger/__init__.py +++ b/hub_logger/__init__.py @@ -1,6 +1,15 @@ import logging as logger from pathlib import Path +import os -log_file = (Path(__file__).parent.parent / 'logs/hub.log').resolve() -logger.basicConfig(filename=log_file, format="%(asctime)s:%(levelname)s:{%(pathname)s:%(funcName)s:%(lineno)d} " - "- %(message)s", level=logger.DEBUG) +log_dir = (Path(__file__).parent.parent / 'logs').resolve() +log_file = (log_dir / 'hub.log').resolve() +try: + if not os.path.isfile(log_file): + os.mkdir(log_dir) + with open(log_file, 'x'): + pass + logger.basicConfig(filename=log_file, format="%(asctime)s:%(levelname)s:{%(pathname)s:%(funcName)s:%(lineno)d} " + "- %(message)s", level=logger.DEBUG) +except IOError as err: + print(f'I/O exception: {err}')