https://github.com/suraj-arya/chandler
Combined python log handler to rotate log files based on both size and time.
https://github.com/suraj-arya/chandler
logging loghandler logrotation python python3
Last synced: about 1 year ago
JSON representation
Combined python log handler to rotate log files based on both size and time.
- Host: GitHub
- URL: https://github.com/suraj-arya/chandler
- Owner: suraj-arya
- License: mit
- Created: 2022-09-21T11:14:52.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-10-06T11:57:53.000Z (over 3 years ago)
- Last Synced: 2025-03-16T18:17:26.439Z (over 1 year ago)
- Topics: logging, loghandler, logrotation, python, python3
- Language: Python
- Homepage:
- Size: 399 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Python Size and Time Based Logging File Rotating Handler
Python logging's file based [handlers](https://docs.python.org/3/library/logging.handlers.html) have two different kinds of rotation.
* [Size based rotation](https://docs.python.org/3/library/logging.handlers.html#logging.handlers.RotatingFileHandler)
* [Time based rotation](https://docs.python.org/3/library/logging.handlers.html#logging.handlers.TimedRotatingFileHandler)
However, these rotations work in isolation. Only one handler can be attached to a log file.
One can only add either Size based rotation or Time based rotation.
Using `chandler.handler.SizedAndTimedRotatingHandler` you can rotate the files based on both time and size. Files will be rotated whenever either of the conditions are met.
### How to Use
import the handler
```python
from chandler.handler import SizedAndTimedRotatingHandler
```
Then you can initialise your loggers and append this handler.
```python
logger = logging.getLogger('test-logger')
log_file_path = '/var/log/test/logging.log'
rotating_handler = SizedAndTimedRotatingHandler(log_file_path, when='h', interval=1, max_bytes=50000, backup_count=3)
logger.addHandler(rotating_handler)
```
In the above example the handler is configured to rotate every one hour or whenever the file size reaches 50k bytes.
This handler is built on top of [TimedRotatingFileHandler](https://docs.python.org/3/library/logging.handlers.html#logging.handlers.TimedRotatingFileHandler), so most of the arguments are similar to that of TimedRotatingFileHandler.
### installation
You can install with pip
```bash
$ pip install chandler-handler
```
### Contribution
#### Authors
* [Suraj Arya](https://github.com/suraj-arya)