Skip to content

TimedRotatingFileHandler and RotatingFileHandler open new streams in the wrong mode #298

@nhoad

Description

@nhoad

TimedRotatingFileHandler and RotatingFileHandler's perform_rollover methods open the new stream in w mode, not a mode. The result is that if multiple processes are writing to logs, then they start competing with each other. Here's some test code that demonstrates:

import logbook
import datetime

log = logbook.Logger("test")
handler = logbook.TimedRotatingFileHandler("testlog.log", date_format='%Y-%m')

with handler:
    log.info("Testing")
    print(handler.stream)
    handler.perform_rollover(handler._get_timestamp(datetime.datetime.utcnow()))
    print(handler.stream)
    log.info("Testing 2")

With the output showing that the mode has changed:

<_io.TextIOWrapper name='/home/nhoad/testlog-2019-10.log' mode='a' encoding='utf-8'>
<_io.TextIOWrapper name='/home/nhoad/testlog-2019-10.log' mode='w' encoding='utf-8'>

Is there a reason that both of these methods not do self._open(self._mode) instead?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions