class TimedRotatingFileMode(FileModeBase): def __init__(self, filepath: str = "log/delogger.log", when: str = "midnight", backup_count: int = 0, level: int = DEBUG, **kwargs) -> None: super().__init__(**kwargs) self.level = level self.filepath = filepath self.when = when self.backup_count = backup_count self.logfile = LogFile(filepath) self.logfile.mkdir() def load(self, delogger) -> None: timed_hdlr = TimedRotatingFileHandler( filename=str(self.logfile.filepath), when=self.when, backupCount=self.backup_count, ) delogger.add_handler(timed_hdlr, self.level, fmt=self.fmt, datefmt=self.datefmt)
def __init__(self, filepath: str = "log/delogger.log", when: str = "midnight", backup_count: int = 0, level: int = DEBUG, **kwargs) -> None: super().__init__(**kwargs) self.level = level self.filepath = filepath self.when = when self.backup_count = backup_count self.logfile = LogFile(filepath) self.logfile.mkdir()
def _load_file_path(self, dirpath: str, fmt: str, backup_count: int) -> LogFile: """Get the file path of the log output destination. For each directory, determine the log file path only once at runtime. Args: dirpath (str): Directory path. fmt (str): Filename like date_string. backup_count (int): Leave logs up to the designated generation. """ logfile = LogFile(str(Path(dirpath) / fmt)) # If already same logfile, return the filepath. for fpath in CountRotatingFileHandler._files: if fpath == logfile: return fpath CountRotatingFileHandler._files.append(logfile) if backup_count <= 0: return logfile file_list = self._get_match_files(logfile.filepath.parent, fmt) # Delete the old file and set a new filepath if len(file_list) >= backup_count: os.remove(file_list[0]) return logfile
def load(self, delogger) -> None: run_hdlr = CountRotatingFileHandler(filepath=self.filepath, backup_count=self.backup_count) delogger.add_handler(run_hdlr, self.level, fmt=self.fmt, datefmt=self.datefmt) self.logfile = LogFile(run_hdlr.filepath)
def test_eq(self): logfile = LogFile("log") with pytest.raises(NotImplementedError): if logfile == "no": pass
def test_str(self): logfile = LogFile("log") assert str(logfile) == "log"
def test_contains(self): logfile = LogFile("log") same_logfile = LogFile("log") assert same_logfile in logfile