Coretex
logs.py
1 from pathlib import Path
2 from logging import FileHandler
3 from logging.handlers import WatchedFileHandler
4 
5 import platform
6 
7 
8 class CoretexFileHandler(WatchedFileHandler):
9 
10  def reopenIfNeeded(self) -> None:
11  # baseFilename is absoulte path to the log file
12  path = Path(self.baseFilename)
13 
14  # If parent dir got deleted for some reason
15  if not path.parent.exists():
16  path.parent.mkdir(parents = True, exist_ok = True)
17 
18  super().reopenIfNeeded()
19 
20 
21 def createFileHandler(path: Path) -> FileHandler:
22  """
23  Creates a handler for writting logs to the file system
24 
25  Parameters
26  ----------
27  path : Path
28  path of the log file
29 
30  Returns
31  -------
32  FileHandler -> if OS is Windows
33  WatchedFileHandler -> if OS is Unix based
34  """
35 
36  if platform.system() == "Windows":
37  return FileHandler(path)
38 
39  return CoretexFileHandler(path)