Skip to main content

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

Project description

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 :

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

timed_rotating_text_file-0.0.5.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

timed_rotating_text_file-0.0.5-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

Details for the file timed_rotating_text_file-0.0.5.tar.gz.

File metadata

  • Download URL: timed_rotating_text_file-0.0.5.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using 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.60.0 CPython/3.9.5

File hashes

Hashes for timed_rotating_text_file-0.0.5.tar.gz
Algorithm Hash digest
SHA256 954eed33a96bc6d23b9f0d6d31c0466eb3c6ffc8677adc3da8fe9659b39dc4c3
MD5 cb8c4eaaf9b606db2b92582bfabb7c9a
BLAKE2b-256 5c7c41ca5456afc84d8849806e5993c2d9b31d6a4b7e9a4f9cdef201a36ed343

See more details on using hashes here.

File details

Details for the file timed_rotating_text_file-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: timed_rotating_text_file-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 18.7 kB
  • Tags: Python 3
  • Uploaded using 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.60.0 CPython/3.9.5

File hashes

Hashes for timed_rotating_text_file-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 27643a21cfb99b1080c3c19386527874f98c39fdbffa86eb5fa77c98d380bea2
MD5 e80412ae8ace257ce99ebabd247ed2a6
BLAKE2b-256 347c6525500e8bd3b5320b85d9c9c51ca0db7ae15a9646c1bc3b3316a15ff7d0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page