Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Multiple TimedRotatingFileHandler with similar names but different backup counts do not work #93205

Closed
Assignees
vsajip
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@ilCatania

Description

@ilCatania

Bug report

When setting up multiple logging handlers of typeTimedRotatingFileHandler that log to the same directory and only differ by the file extension, rollover does not work properly for either of them.

Consider this configuration:

root=logging.getLogger()root.addHandler(TimedRotatingFileHandler("test.log",when="S",backupCount=2,delay=True))root.addHandler(TimedRotatingFileHandler("test.log.json",when="S",backupCount=1,delay=True))

running this for several seconds should cause the logging directory to have 5 files (time stamps would obviously vary based on when you run it) :

test.logtest.log.2022-05-25_05-19-19test.log.2022-05-25_05-19-18test.log.jsontest.log.json.2022-05-25_05-19-19

However, the second handler deletes files that should only match the first handler, so it ends up not deleting its own files:

test.logtest.log.2022-05-25_05-19-19test.log.jsontest.log.json.2022-05-25_05-19-17test.log.json.2022-05-25_05-19-18test.log.json.2022-05-25_05-19-19

Digging through code this seems to be caused by the change inbpo-44753 aka#88916, revertingthis change solves the issue for me.

Here's the full code to reproduce the issue:

importloggingimportdatetimefromlogging.handlersimportTimedRotatingFileHandlerfromtempfileimportTemporaryDirectoryfrompathlibimportPathfromtimeimportsleepwithTemporaryDirectory()astd:tmp_path=Path(td)filename="test.log"filename_json=f"{filename}.json"logfile=tmp_path/filenamelogfile_json=tmp_path/filename_jsonh1=TimedRotatingFileHandler(logfile,when="S",backupCount=2,delay=True)h2=TimedRotatingFileHandler(logfile_json,when="S",backupCount=1,delay=True)times= []forlog_strin ("hi1","hi2","hi3","hi4"):times.append(datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S"))forhin (h1,h2):h.emit(logging.LogRecord("name",logging.INFO,"path",1,log_str, (),None))sleep(1)assertlogfile.is_file()actual=set(f.nameforfintmp_path.iterdir())expected= {"test.log",f"test.log.{times[-3]}",f"test.log.{times[-2]}","test.log.json",f"test.log.json.{times[-2]}",    }assertactual==expected, (f"\n\texpected:\t{','.join(expected)}"f"\n\tactual:\t\t{','.join(actual)}"    )assertlogfile.read_text()=="hi4\n"assertlogfile_json.read_text()=="hi4\n"assert (tmp_path/f"{filename}.{times[-3]}").read_text()=="hi2\n"assert (tmp_path/f"{filename_json}.{times[-2]}").read_text()=="hi3\n"

Your environment

Tested this with Python 3.9.7 and 3.10.4, and with the current development cpython code.

Linux 3.10.0-957.27.2.el7.x86_64#1 SMP Mon Jul 29 17:46:05 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Linked PRs

Metadata

Metadata

Assignees

Labels

stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp