logger boilerplate with dataclass models for parsing
Project description
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.
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 loggi-0.5.0.tar.gz.
File metadata
- Download URL: loggi-0.5.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2bd05ec08402c07207ca076913a35a81a838e1b12164a6b603eda6e345ff9958
|
|
| MD5 |
8851ddb703264b648c5dcf87c9770f1b
|
|
| BLAKE2b-256 |
87bfe442af65bb7c27ce97c735e4e007c1b0872ecffc68665bbb5828b1294249
|
File details
Details for the file loggi-0.5.0-py3-none-any.whl.
File metadata
- Download URL: loggi-0.5.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40e626163a7dc9c47f97a03a9b1f4208f048071784b9ab8bd37fb6842aafe817
|
|
| MD5 |
c127e2b07d4a203999173f535fda6a9d
|
|
| BLAKE2b-256 |
847dedbe59cb8c131cddc139385534bd733b8f27b7f107bb27c8dc026b54cc04
|