Skip to main content

timed-rotating-text-file

made-with-python PyPI license PyPI version shields.io PyPI pyversions GitHub release

A ZERO dependency rotating text file handler which rotates when YOU want it to, like TimedRotatingFileHandler provided by Python's logging module.

When to use this module?

  • When you have that niche case of logs being written into some text file from several functions and you just want it to rotate without having to re-write all the write calls with some new package.

Why not use TimedRotatingFileHandler provided by Python's logging module?

  • If you are thinking of logging, please use TimedRotatingFileHandler. It provides doRollover method which does the same functionality but with all the logging APIs.

Installation

pip install timed-rotating-text-file

Usage

If you have some code like:

with open("path/to/text/file","w") as fp:
  fp.write("some line")

All you have to do is:

from timed_rotating_text_file import TimedRotatingTextFile
with TimedRotatingTextFile("/tmp/tmp.log", when="m", backup_count=5) as fp:
  fp.write("some log")

By default, the interval is 1 so in our example, the rotation will be done every minute.

Result:

-rw-r--r-- 1 root root  840 avril 12 21:03 /tmp/tmp.log
-rw-r--r-- 1 root root 1,7K avril 12 20:53 /tmp/tmp.log.2021-04-12_20-51
-rw-r--r-- 1 root root  840 avril 12 20:55 /tmp/tmp.log.2021-04-12_20-53
-rw-r--r-- 1 root root  840 avril 12 20:57 /tmp/tmp.log.2021-04-12_20-55
-rw-r--r-- 1 root root 1,7K avril 12 20:59 /tmp/tmp.log.2021-04-12_20-57
-rw-r--r-- 1 root root  840 avril 12 21:01 /tmp/tmp.log.2021-04-12_20-59

Arguments

class TimedRotatingTextFile(filename, when='d', interval=1, backup_count=0, mode="ab+", delay=False, utc=False)

Returns a new instance of the TimedRotatingTextFile class. The specified file is opened and used as the stream for file. On rotating it also sets the filename suffix. Rotating happens based on the product of when and interval.

You can use the when to specify the type of interval. The list of possible values is below. Note that they are not case sensitive.

Value Type of Interval
'M' Minutes
'H" Hours
'D' Days
'MIDNIGHT' Roll over at midnight

The system will save old files by appending extensions to the filename. The extensions are date-and-time based, using the strftime format %Y-%m-%d_%H-%M-%S or a leading portion thereof, depending on the rollover interval.

When computing the next rollover time for the first time (when the handler is created), the last modification time of an existing file, or else the current time, is used to compute when the next rotation will occur.

If the utc argument is true, times in UTC will be used; otherwise local time is used.

If backup_count is nonzero, at most backup_count files will be kept, and if more would be created when rollover occurs, the oldest one is deleted. The deletion logic uses the interval to determine which files to delete, so changing the interval may leave old files lying around.

If delay is true, then file opening is deferred until the first call

Calculation of the initial rollover time is done when the handler is initialised. Calculation of subsequent rollover times is done only when rollover occurs, and rollover occurs only when emitting output. If this is not kept in mind, it might lead to some confusion. For example, if an interval of “every minute” is set, that does not mean you will always see files with times (in the filename) separated by a minute; if, during application execution, output is generated more frequently than once a minute, then you can expect to see files with times separated by a minute. If, on the other hand, messages are only output once every five minutes (say), then there will be gaps in the file times corresponding to the minutes where no output (and hence no rollover) occurred.

Inspired by :

Metadata

Release files for timed-rotating-text-file 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for timed-rotating-text-file 0.0.7
File Size Uploaded Provenance Actions
timed_rotating_text_file-0.0.7.tar.gz 17.8 kB Without provenance Details for timed_rotating_text_file-0.0.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for timed-rotating-text-file 0.0.7
File Interpreter ABI Platform Actions
timed_rotating_text_file-0.0.7-py3-none-any.whl Python 3 none any Details for timed_rotating_text_file-0.0.7-py3-none-any.whl

Total release size: 36.5 kB

Release files / timed_rotating_text_file-0.0.7.tar.gz

Download URL timed_rotating_text_file-0.0.7.tar.gz
Size 17.8 kB
Tags Source
SHA-256 checksum
How to use checksums
266375b30a877af958d5b1db8d62af81d5dbb8ff1b85d81ec00d2dbd94fd2625
BLAKE2b-256 checksum
How to use checksums
93bdb4b5258b16f71b2df0c22ab8ba0c952602a7c32aeb9df4a5b2c8c8c92a4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

Release files / timed_rotating_text_file-0.0.7-py3-none-any.whl

Download URL timed_rotating_text_file-0.0.7-py3-none-any.whl
Size 18.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
afcaadd41d116e31c2757313090facba9a2da27a04ba46130668af0d5b260f21
BLAKE2b-256 checksum
How to use checksums
898b1d01418df8280594ff5efc2b504bf69419a2a0cb166dbb44c3163a2bfb38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.5

Security for timed-rotating-text-file 0.0.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page