Spaces:
Sleeping
Sleeping
fix logger check_create_file() to ensure logging file
Browse files- file_handler/file_utils.py +27 -8
- utils/logger.py +2 -2
file_handler/file_utils.py
CHANGED
|
@@ -27,17 +27,36 @@ def create_outputdir(root: Union[str, Path], output_dir_string:str = None) -> Pa
|
|
| 27 |
output_dir.mkdir(mode=0o2644, parents=True, exist_ok=True)
|
| 28 |
return output_dir
|
| 29 |
|
| 30 |
-
def
|
| 31 |
"""
|
| 32 |
-
check if File
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
"""
|
| 34 |
-
file_dir = Path("logs") / file_dir if not isinstance(file_dir, Path) else Path(file_dir)
|
| 35 |
-
if not file_dir.exists():
|
| 36 |
-
##SMY: [resolved] Permission Errno13 - https://stackoverflow.com/a/57454275
|
| 37 |
-
file_dir.touch(mode=0o2644, exist_ok=True) #, parents=True) ##SMY: create file if not exists
|
| 38 |
-
file_dir.chmod(0)
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def is_file_with_extension(path_obj: Path) -> bool:
|
| 43 |
"""
|
|
|
|
| 27 |
output_dir.mkdir(mode=0o2644, parents=True, exist_ok=True)
|
| 28 |
return output_dir
|
| 29 |
|
| 30 |
+
def check_create_file(filename: str, dir_path: Union[str, Path]="logs") -> Path:
|
| 31 |
"""
|
| 32 |
+
check if File exists, else create one ad return the file path.
|
| 33 |
+
|
| 34 |
+
Args:
|
| 35 |
+
directory_path (str): The path to the directory.
|
| 36 |
+
filename (str): The name of the file to check/create.
|
| 37 |
+
Returns:
|
| 38 |
+
The pathlib.Path object for the file
|
| 39 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
#file_dir = Path("logs") / file_dir if not isinstance(file_dir, Path) else Path(file_dir)
|
| 42 |
+
dir_path = dir_path if isinstance(dir_path, Path) else Path(dir_path)
|
| 43 |
+
|
| 44 |
+
# Ensure the directory exists
|
| 45 |
+
# Create the parent directory if it doesn't exist.
|
| 46 |
+
# `parents=True` creates any missing parent directories.
|
| 47 |
+
# `exist_ok=True` prevents an error if the directory already exists.
|
| 48 |
+
dir_path.mkdir(parents=True, exist_ok=True, mode=0o2644)
|
| 49 |
+
|
| 50 |
+
file_path = dir_path / filename # Concatenate directory and filename to get full path
|
| 51 |
+
if not file_path.exists(): # Create the file if it doesn't exist
|
| 52 |
+
file_path.touch() # Creates an empty file if it doesn't exists
|
| 53 |
+
#file_dir.touch(mode=0o2644, exist_ok=True) #, parents=True) ##SMY: Note Permission Errno13 - https://stackoverflow.com/a/57454275
|
| 54 |
+
#file_dir.chmod(0)
|
| 55 |
+
|
| 56 |
+
return file_path
|
| 57 |
+
|
| 58 |
+
## debug
|
| 59 |
+
#print(f'file: {check_create_file("app_logging.log")}')
|
| 60 |
|
| 61 |
def is_file_with_extension(path_obj: Path) -> bool:
|
| 62 |
"""
|
utils/logger.py
CHANGED
|
@@ -65,8 +65,8 @@ def setup_logging(level: int = None) -> None:
|
|
| 65 |
# File handler
|
| 66 |
#file_handler = logging.FileHandler("logs/app_logging_scrap.log", mode="a", encoding="utf-8")
|
| 67 |
#file_handler = logging.FileHandler("logs/app_logging.log", mode="a", encoding="utf-8")
|
| 68 |
-
from file_handler.file_utils import
|
| 69 |
-
file_handler = logging.FileHandler(
|
| 70 |
file_handler.setFormatter(JsonFormatter())
|
| 71 |
|
| 72 |
root = logging.getLogger()
|
|
|
|
| 65 |
# File handler
|
| 66 |
#file_handler = logging.FileHandler("logs/app_logging_scrap.log", mode="a", encoding="utf-8")
|
| 67 |
#file_handler = logging.FileHandler("logs/app_logging.log", mode="a", encoding="utf-8")
|
| 68 |
+
from file_handler.file_utils import check_create_file
|
| 69 |
+
file_handler = logging.FileHandler(check_create_pfile("app_logging.log"), mode="a", encoding="utf-8")
|
| 70 |
file_handler.setFormatter(JsonFormatter())
|
| 71 |
|
| 72 |
root = logging.getLogger()
|