Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncTimedRotatingFileHandler file delete issue #27

Open
RokubanRKB opened this issue Apr 10, 2023 · 1 comment
Open

AsyncTimedRotatingFileHandler file delete issue #27

RokubanRKB opened this issue Apr 10, 2023 · 1 comment

Comments

@RokubanRKB
Copy link

RokubanRKB commented Apr 10, 2023

I am using time rotating file handler that roll over at midnight to create new log file every day.

With the backup_count set, I've noticed that when the backup files reached the setting value, the oldest file is not being deleted instead the latest rolled over file is been deleted.

async def get_files_to_delete(self) -> List[str]:
"""
Determine the files to delete when rolling over.
"""
dir_name, base_name = os.path.split(self.absolute_file_path)
loop = get_running_loop()
file_names = await loop.run_in_executor(
None, lambda: os.listdir(dir_name)
)
result = []
prefix = base_name + "."
plen = len(prefix)
for file_name in file_names:
if file_name[:plen] == prefix:
suffix = file_name[plen:]
if self.ext_match.match(suffix):
result.append(os.path.join(dir_name, file_name))
if len(result) < self.backup_count:
return []
else:
result.sort(reverse=True) # os.listdir order is not defined
return result[: len(result) - self.backup_count]

Upon checking the code in get_files_to_delete method.

The last part of the method returns the files that exceeds backup_count to be delete.

The reversed descending sort gives the wrong result and latest log file is being deleted.

Possible fix is remove reverse=True so the result list is in ascending sort.

Not sure if anyone else is having this issue.

My environment is Debian 11 with python 3.9.2 and aiologger 0.7.0 release.

@edonavon
Copy link

I confirm seeing the same issue and same solution as you suggested is working for me
My environment is ubuntu 22, python 3.11.3, aiologger 0.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants