loggi
logger boilerplate with dataclass models for parsing
Installation
Install with:
pip install loggi
Usage
>>> import loggi
>>> logger = loggi.getLogger("demo", "logs")
The file "demo.log" will be created inside a folder named "logs" in the current directory.
loggi wraps the logging level mapping so the log level can be set without importing the logging module
>>> logger.setLevel(loggi.DEBUG)
Also loggi imports the logging module when it's initialized, so all logging module features can be utilized without explicity importing logging yourself
>>> print(loggi.logging.getLevelName(loggi.INFO))
INFO
loggi uses the format {level}|-|{date}|-|{message} where date has the format %x %X
>>> logger.info("yeehaw")
produces the log
INFO|-|10/26/23 18:48:30|-|yeehaw
loggi also contains two dataclasses: Log and Event.
A Log object contains a list of Event objects that can be loaded from a log file (that uses the above format).
Each Event contains level: str, date: datetime, message: str fields.
>>> log = loggi.load_log("logs/demo.log")
>>> print(log)
INFO|-|10/26/23 18:48:30|-|yeehaw
>>> print(log.num_events)
1
>>> print(log.events[0].level)
INFO
Log objects can be added together.
Useless examples:
>>> log += log
>>> print(log)
INFO|-|2023-10-26 18:48:30|-|yeehaw
INFO|-|2023-10-26 18:48:30|-|yeehaw
New, filtered Log objects can be created using the filter_dates, filter_levels, and filter_messages functions.
>>> from datetime import datetime, timedelta
>>> log = loggi.load_log("realistic_log.log")
Filtering for events between 24 and 48 hours ago:
>>> filtered_log = log.filter_dates(datetime.now() - timedelta(days=2), datetime.now() - timedelta(days=1))
Filtering for events with critical and error levels:
>>> filtered_log = log.filter_levels(["CRITICAL", "ERROR"])
Filtering for events whose message contains "yeehaw", but not "double yeehaw" or "oopsie":
>>> filtered_log = log.filter_messages(["*yeehaw*"], ["*double yeehaw*", "*oopsie*"])
The filtering methods can be chained:
>>> log_slice = log.filter_dates(datetime.now() - timedelta(days=2), datetime.now() - timedelta(days=1)).filter_levels(["CRITICAL", "ERROR"])
When adding Log objects, the chronosort() function can be used to reorder the events by date:
>>> log = filtered_log + log_slice
>>> log.chronosort()
log now contains all critical and error level events between 24 and 48 hours ago,
as well as events from anytime of any level with "yeehaw" in the message, but not "double yeehaw" or "oopsie".
LoggerMixin
For convenience, the LoggerMixin class can be inherited from to provide initialization of a logger attribute:
class Yeehaw(loggi.LoggerMixin):
def __init__(self):
self.init_logger()
dummy = Yeehaw()
dummy.logger.info("yeet")
By default, the log file will be named after the class inheriting from LoggerMixin (Yeehaw), but lowercase and stored in a "logs" folder of the current directory. (logs/yeehaw.log)
The logger name, directory, and logging level can be specified with arguments to self.init_logger().
class Yeehaw(loggi.LoggerMixin):
def __init__(self):
self.init_logger(name="yeet", log_dir="top_secret", log_level="DEBUG")
Logged messages for the above class will be written to the path top_secret/yeet.log.
Metadata
Release files for loggi 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| loggi-0.5.0.tar.gz | 6.2 kB | Without provenance | Details for loggi-0.5.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| loggi-0.5.0-py3-none-any.whl | Python 3 | none | any | Details for loggi-0.5.0-py3-none-any.whl |
Total release size: 13.4 kB
Release files / loggi-0.5.0.tar.gz
| Download URL | loggi-0.5.0.tar.gz |
|---|---|
| Size | 6.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2bd05ec08402c07207ca076913a35a81a838e1b12164a6b603eda6e345ff9958
|
|
BLAKE2b-256 checksum How to use checksums |
87bfe442af65bb7c27ce97c735e4e007c1b0872ecffc68665bbb5828b1294249
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.0
|
Release files / loggi-0.5.0-py3-none-any.whl
| Download URL | loggi-0.5.0-py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
40e626163a7dc9c47f97a03a9b1f4208f048071784b9ab8bd37fb6842aafe817
|
|
BLAKE2b-256 checksum How to use checksums |
847dedbe59cb8c131cddc139385534bd733b8f27b7f107bb27c8dc026b54cc04
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.11.0
|
Security for loggi 0.5.0
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.