Spaces:
Sleeping
Sleeping
log file: refactor check_create_file
Browse files- file_handler/file_utils.py +54 -9
- utils/logger.py +2 -2
file_handler/file_utils.py
CHANGED
|
@@ -27,9 +27,49 @@ 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 check_create_file(filename: str, dir_path: Union[str, Path]="logs") -> Path:
|
| 31 |
"""
|
| 32 |
-
check if File exists, else create one
|
| 33 |
|
| 34 |
Args:
|
| 35 |
directory_path (str): The path to the directory.
|
|
@@ -37,32 +77,37 @@ def check_create_file(filename: str, dir_path: Union[str, Path]="logs") -> Path:
|
|
| 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
|
| 49 |
-
dir_path.
|
|
|
|
| 50 |
|
| 51 |
file_path = dir_path / filename # Concatenate directory and filename to get full path
|
| 52 |
-
print(f"file_path: {file_path}") ##
|
| 53 |
|
| 54 |
-
file_path.touch(exist_ok=True, mode=0o2664) # Creates an empty file if it doesn't exists
|
| 55 |
|
| 56 |
-
'''
|
| 57 |
if not file_path.exists(): # check if file doesn't exist
|
| 58 |
-
file_path.touch(exist_ok=True
|
| 59 |
#file_dir.touch(mode=0o2644, exist_ok=True) #, parents=True) ##SMY: Note Permission Errno13 - https://stackoverflow.com/a/57454275
|
| 60 |
#file_dir.chmod(0)
|
| 61 |
-
'''
|
| 62 |
|
| 63 |
return file_path
|
| 64 |
|
| 65 |
## debug
|
|
|
|
|
|
|
| 66 |
#print(f'file: {check_create_file("app_logging.log")}')
|
| 67 |
|
| 68 |
def is_file_with_extension(path_obj: Path) -> bool:
|
|
|
|
| 27 |
output_dir.mkdir(mode=0o2644, parents=True, exist_ok=True)
|
| 28 |
return output_dir
|
| 29 |
|
| 30 |
+
def check_create_logfile(filename: str, dir_path: Union[str, Path]="logs") -> Path:
|
| 31 |
+
"""
|
| 32 |
+
check if log file exists, else create one and 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 |
+
import datetime
|
| 42 |
+
|
| 43 |
+
# 1. Get the path of the current script's parent directory (the project folder).
|
| 44 |
+
# `__file__` is a special variable that holds the path to the current script.
|
| 45 |
+
project_root = Path(__file__).parent.parent.resolve()
|
| 46 |
+
dir_path = dir_path if isinstance(dir_path, Path) else Path(dir_path)
|
| 47 |
+
|
| 48 |
+
# 2. Define the path for the logs directory.
|
| 49 |
+
# The `/` operator is overloaded to join paths easily.
|
| 50 |
+
logs_dir = project_root / dir_path
|
| 51 |
+
|
| 52 |
+
# 3. Create the logs directory if it doesn't already exist.
|
| 53 |
+
# `mkdir()` with `exist_ok=True` prevents a FileExistsError if the folder exists.
|
| 54 |
+
logs_dir.mkdir(exist_ok=True)
|
| 55 |
+
|
| 56 |
+
# 4. Create log file with a timestamp inside the new logs directory.
|
| 57 |
+
# This ensures a unique file is created each time the script runs.
|
| 58 |
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d") #.strftime("%Y-%m-%d_%H-%M-%S")
|
| 59 |
+
log_file = logs_dir / f"{Path(filename).stem}_{timestamp}.log"
|
| 60 |
+
|
| 61 |
+
# 5. Check if the file exists (it won't,if it's not the same day).
|
| 62 |
+
if not log_file.exists():
|
| 63 |
+
# If the file doesn't exist, touch() will create an empty file.
|
| 64 |
+
log_file.touch()
|
| 65 |
+
|
| 66 |
+
print(f"Created log file at: {log_file}")
|
| 67 |
+
|
| 68 |
+
return log_file
|
| 69 |
+
|
| 70 |
def check_create_file(filename: str, dir_path: Union[str, Path]="logs") -> Path:
|
| 71 |
"""
|
| 72 |
+
check if File exists, else create one and return the file path.
|
| 73 |
|
| 74 |
Args:
|
| 75 |
directory_path (str): The path to the directory.
|
|
|
|
| 77 |
Returns:
|
| 78 |
The pathlib.Path object for the file
|
| 79 |
"""
|
| 80 |
+
# Get project root
|
| 81 |
+
project_root = Path(__file__).parent.resolve() ##SMY: `__file__` is a special variable pointing to current file`
|
| 82 |
|
| 83 |
#file_dir = Path("logs") / file_dir if not isinstance(file_dir, Path) else Path(file_dir)
|
| 84 |
dir_path = dir_path if isinstance(dir_path, Path) else Path(dir_path)
|
| 85 |
|
| 86 |
# Ensure the directory exists
|
| 87 |
+
# Create the file parent directory, relative to the project root, if it doesn't exist.
|
| 88 |
# `parents=True` creates any missing parent directories.
|
| 89 |
# `exist_ok=True` prevents an error if the directory already exists.
|
| 90 |
+
dir_path = project_root / dir_path
|
| 91 |
+
dir_path.mkdir(parents=True, exist_ok=True) #, mode=0o2664) #, mode=0o2644)
|
| 92 |
+
#dir_path.chmod(0)
|
| 93 |
|
| 94 |
file_path = dir_path / filename # Concatenate directory and filename to get full path
|
| 95 |
+
#print(f"file_path: {file_path}") ##debug
|
| 96 |
|
| 97 |
+
#file_path.touch(exist_ok=True, mode=0o2664) # Creates an empty file if it doesn't exists
|
| 98 |
|
| 99 |
+
#'''
|
| 100 |
if not file_path.exists(): # check if file doesn't exist
|
| 101 |
+
file_path.touch(exist_ok=True) #, mode=0o2664) # Creates an empty file if it doesn't exists
|
| 102 |
#file_dir.touch(mode=0o2644, exist_ok=True) #, parents=True) ##SMY: Note Permission Errno13 - https://stackoverflow.com/a/57454275
|
| 103 |
#file_dir.chmod(0)
|
| 104 |
+
#'''
|
| 105 |
|
| 106 |
return file_path
|
| 107 |
|
| 108 |
## debug
|
| 109 |
+
#from pathlib import Path
|
| 110 |
+
#from typing import Union
|
| 111 |
#print(f'file: {check_create_file("app_logging.log")}')
|
| 112 |
|
| 113 |
def is_file_with_extension(path_obj: Path) -> bool:
|
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_logfile
|
| 69 |
+
file_handler = logging.FileHandler(check_create_logfile("app_logging.log"), mode="a", encoding="utf-8")
|
| 70 |
file_handler.setFormatter(JsonFormatter())
|
| 71 |
|
| 72 |
root = logging.getLogger()
|