Simple python logs with file rotation
Project description
Simple python logs with file rotation
Notes
- Arguments for all classes are declared as OPTIONAL
- arguments takes priority over environment variables
- If any .env variable is omitted, it falls back to default values here: settings.py
- Timezone parameter can also accept
localtime, default toUTC- This parameter is only to display the timezone datetime inside the log file
- For timed rotation, only UTC and localtime are supported, meaning it will rotate at UTC or localtime
- env variable to change between UTC and localtime is
LOG_ROTATE_AT_UTCand default to True
- env variable to change between UTC and localtime is
- Streamhandler parameter will add stream handler along with file handler
- Showlocation parameter will show the filename and the line number where the message originated
Install
pip install pythonLogs
BasicLog
- Setup Logging
- This is just a basic log, it does not use any file
from pythonLogs import BasicLog
logger = BasicLog(
level="debug",
name="app",
timezone="America/Sao_Paulo",
showlocation=False,
).init()
logger.warning("This is a warning example")
Example of output
[2024-10-08T19:08:56.918-0300]:[WARNING]:[app]:This is a warning example
SizeRotatingLog
- Setup Logging
- Logs will rotate based on the file size using the
maxmbytesvariable - Rotated logs will have a sequence number starting from 1:
app.log_1.gz, app.log_2.gz - Logs will be deleted based on the
daystokeepvariable, defaults to 30
- Logs will rotate based on the file size using the
from pythonLogs import SizeRotatingLog
logger = SizeRotatingLog(
level="debug",
name="app",
directory="/app/logs",
filenames=["main.log", "app1.log"],
maxmbytes=5,
daystokeep=7,
timezone="America/Chicago",
streamhandler=True,
showlocation=False
).init()
logger.warning("This is a warning example")
Example of output
[2024-10-08T19:08:56.918-0500]:[WARNING]:[app]:This is a warning example
TimedRotatingLog
- Setup Logging
- Logs will rotate based on
whenvariable to a.gzfile, defaults tomidnight - Rotated log will have the sufix variable on its name:
app_20240816.log.gz - Logs will be deleted based on the
daystokeepvariable, defaults to 30 - Current 'when' events supported:
- midnight — roll over at midnight
- W{0-6} - roll over on a certain day; 0 - Monday
- Logs will rotate based on
from pythonLogs import TimedRotatingLog
logger = TimedRotatingLog(
level="debug",
name="app",
directory="/app/logs",
filenames=["main.log", "app2.log"],
when="midnight",
daystokeep=7,
timezone="UTC",
streamhandler=True,
showlocation=False
).init()
logger.warning("This is a warning example")
Example of output
[2024-10-08T19:08:56.918-0000]:[WARNING]:[app]:This is a warning example
Env Variables (Optional)
LOG_LEVEL=DEBUG
LOG_TIMEZONE=America/Chicago
LOG_ENCODING=UTF-8
LOG_APPNAME=app
LOG_FILENAME=app.log
LOG_DIRECTORY=/app/logs
LOG_DAYS_TO_KEEP=30
LOG_STREAM_HANDLER=True
LOG_SHOW_LOCATION=False
LOG_DATE_FORMAT=%Y-%m-%dT%H:%M:%S
# SizeRotatingLog
LOG_MAX_FILE_SIZE_MB=10
# TimedRotatingLog
LOG_ROTATE_WHEN=midnight
LOG_ROTATE_AT_UTC=True
Source Code
Build
poetry build -f wheel
Run Tests and Get Coverage Report using Poe
poetry update --with test
poe test
License
Released under the MIT License
Buy me a cup of coffee
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pythonlogs-3.0.13.tar.gz.
File metadata
- Download URL: pythonlogs-3.0.13.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a779da4d2f29dce16c9449aab1aa3c32b5b2f3889756264f708ab9013ae32d8
|
|
| MD5 |
64348f6a759feda0e8ec89c38e3a7b20
|
|
| BLAKE2b-256 |
98825db22a0be75fe19ab59b1d1b2d82aaf3a2b06e5b379c95d91db39bcc91b0
|
Provenance
The following attestation bundles were made for pythonlogs-3.0.13.tar.gz:
Publisher:
workflow.yml on ddc/pythonLogs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pythonlogs-3.0.13.tar.gz -
Subject digest:
5a779da4d2f29dce16c9449aab1aa3c32b5b2f3889756264f708ab9013ae32d8 - Sigstore transparency entry: 158494818
- Sigstore integration time:
-
Permalink:
ddc/pythonLogs@66f0c8a158c99653045814b1f494685314bb2bde -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ddc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@66f0c8a158c99653045814b1f494685314bb2bde -
Trigger Event:
push
-
Statement type:
File details
Details for the file pythonlogs-3.0.13-py3-none-any.whl.
File metadata
- Download URL: pythonlogs-3.0.13-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
992a4a96dd4b9e26ada39fc0f7a8dcaf735d9c3b34d1fbf8f0593c916978ca74
|
|
| MD5 |
2a55c6640064f0c1ec7aeffe467044e5
|
|
| BLAKE2b-256 |
35de39bf56ee84be57236eb2422f4be2d400e42902dabfe102eeb64011f426f2
|
Provenance
The following attestation bundles were made for pythonlogs-3.0.13-py3-none-any.whl:
Publisher:
workflow.yml on ddc/pythonLogs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pythonlogs-3.0.13-py3-none-any.whl -
Subject digest:
992a4a96dd4b9e26ada39fc0f7a8dcaf735d9c3b34d1fbf8f0593c916978ca74 - Sigstore transparency entry: 158494821
- Sigstore integration time:
-
Permalink:
ddc/pythonLogs@66f0c8a158c99653045814b1f494685314bb2bde -
Branch / Tag:
refs/heads/main - Owner: https://github.com/ddc
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
workflow.yml@66f0c8a158c99653045814b1f494685314bb2bde -
Trigger Event:
push
-
Statement type: