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.6.tar.gz (17.7 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.6-py3-none-any.whl (18.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: timed_rotating_text_file-0.0.6.tar.gz
  • Upload date:
  • Size: 17.7 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.61.0 CPython/3.9.5

File hashes

Hashes for timed_rotating_text_file-0.0.6.tar.gz
Algorithm Hash digest
SHA256 47af01f0447827024d587245e82174a750beb17d610e4488f8d778fa5ffc3ae5
MD5 ba2cf3f9a4cc8f5a954a174fe331015f
BLAKE2b-256 ae2c99f74fb3bc8b6621e66e2402a0d02fa7e00e75f4d9930e3565774e1dce47

See more details on using hashes here.

File details

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

File metadata

  • Download URL: timed_rotating_text_file-0.0.6-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.61.0 CPython/3.9.5

File hashes

Hashes for timed_rotating_text_file-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 27d592970425e920583718fdfb79b680f345d914017ecdca0a15d4eb56a3e9e7
MD5 45bfed0b0843ea1909d992df12b539f4
BLAKE2b-256 0f927de0e100f2f71adaeb778f39f9781da6e697f89a9e12d9a8a0ed0a5ee004

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