loggi


Nameloggi JSON
Version 0.5.0 PyPI version JSON
download
home_pageNone
Summarylogger boilerplate with dataclass models for parsing
upload_time2024-05-01 19:23:42
maintainerNone
docs_urlNone
authorNone
requires_python<3.12,>=3.10
licenseNone
keywords log logger logging
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # loggi

logger boilerplate with dataclass models for parsing

## Installation

Install with:

```console
pip install loggi
```

## Usage

```python
>>> 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

```python
>>> 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

```python
>>> print(loggi.logging.getLevelName(loggi.INFO))
INFO
```

loggi uses the format `{level}|-|{date}|-|{message}` where date has the format `%x %X`

```python
>>> logger.info("yeehaw")
```

produces the log

```python
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.

```python
>>> 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:

```python
>>> 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.

```python
>>> from datetime import datetime, timedelta
>>> log = loggi.load_log("realistic_log.log")
```

Filtering for events between 24 and 48 hours ago:

```python
>>> filtered_log = log.filter_dates(datetime.now() - timedelta(days=2), datetime.now() - timedelta(days=1))
```

Filtering for events with critical and error levels:

```python
>>> filtered_log = log.filter_levels(["CRITICAL", "ERROR"])
```

Filtering for events whose message contains "yeehaw", but not "double yeehaw" or "oopsie":

```python
>>> filtered_log = log.filter_messages(["*yeehaw*"], ["*double yeehaw*", "*oopsie*"])
```

The filtering methods can be chained:

```python
>>> 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:

```python
>>> 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:

```python
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()`.  

```python
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`.  

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "loggi",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<3.12,>=3.10",
    "maintainer_email": null,
    "keywords": "log, logger, logging",
    "author": null,
    "author_email": "Matt Manes <mattmanes@pm.me>",
    "download_url": "https://files.pythonhosted.org/packages/87/bf/e442af65bb7c27ce97c735e4e007c1b0872ecffc68665bbb5828b1294249/loggi-0.5.0.tar.gz",
    "platform": null,
    "description": "# loggi\n\nlogger boilerplate with dataclass models for parsing\n\n## Installation\n\nInstall with:\n\n```console\npip install loggi\n```\n\n## Usage\n\n```python\n>>> import loggi\n>>> logger = loggi.getLogger(\"demo\", \"logs\") \n```\n\nThe file \"demo.log\" will be created inside a folder named \"logs\" in the current directory.  \n\nloggi wraps the logging level mapping so the log level can be set without importing the logging module\n\n```python\n>>> logger.setLevel(loggi.DEBUG)\n```\n\nAlso loggi imports the logging module when it's initialized, so all logging module features can be utilized without explicity importing logging yourself\n\n```python\n>>> print(loggi.logging.getLevelName(loggi.INFO))\nINFO\n```\n\nloggi uses the format `{level}|-|{date}|-|{message}` where date has the format `%x %X`\n\n```python\n>>> logger.info(\"yeehaw\")\n```\n\nproduces the log\n\n```python\nINFO|-|10/26/23 18:48:30|-|yeehaw\n```\n\nloggi also contains two dataclasses: `Log` and `Event`.  \nA `Log` object contains a list of `Event` objects that can be loaded from a log file (that uses the above format).  \nEach `Event` contains `level: str`, `date: datetime`, `message: str` fields.\n\n```python\n>>> log = loggi.load_log(\"logs/demo.log\")\n>>> print(log)\nINFO|-|10/26/23 18:48:30|-|yeehaw\n>>> print(log.num_events)\n1\n>>> print(log.events[0].level)\nINFO\n```\n\n`Log` objects can be added together.  \n\nUseless examples:\n\n```python\n>>> log += log\n>>> print(log)\nINFO|-|2023-10-26 18:48:30|-|yeehaw\nINFO|-|2023-10-26 18:48:30|-|yeehaw\n```\n\nNew, filtered `Log` objects can be created using the `filter_dates`, `filter_levels`, and `filter_messages` functions.\n\n```python\n>>> from datetime import datetime, timedelta\n>>> log = loggi.load_log(\"realistic_log.log\")\n```\n\nFiltering for events between 24 and 48 hours ago:\n\n```python\n>>> filtered_log = log.filter_dates(datetime.now() - timedelta(days=2), datetime.now() - timedelta(days=1))\n```\n\nFiltering for events with critical and error levels:\n\n```python\n>>> filtered_log = log.filter_levels([\"CRITICAL\", \"ERROR\"])\n```\n\nFiltering for events whose message contains \"yeehaw\", but not \"double yeehaw\" or \"oopsie\":\n\n```python\n>>> filtered_log = log.filter_messages([\"*yeehaw*\"], [\"*double yeehaw*\", \"*oopsie*\"])\n```\n\nThe filtering methods can be chained:\n\n```python\n>>> log_slice = log.filter_dates(datetime.now() - timedelta(days=2), datetime.now() - timedelta(days=1)).filter_levels([\"CRITICAL\", \"ERROR\"])\n```\n\nWhen adding `Log` objects, the `chronosort()` function can be used to reorder the events by date:\n\n```python\n>>> log = filtered_log + log_slice\n>>> log.chronosort()\n```\n\n`log` now contains all critical and error level events between 24 and 48 hours ago,\nas well as events from anytime of any level with \"yeehaw\" in the message, but not \"double yeehaw\" or \"oopsie\".  \n\n### LoggerMixin\n\nFor convenience, the `LoggerMixin` class can be inherited from to provide initialization of a `logger` attribute:\n\n```python\nclass Yeehaw(loggi.LoggerMixin):\n    def __init__(self):\n        self.init_logger()\n\ndummy = Yeehaw()\ndummy.logger.info(\"yeet\")\n```\n\nBy 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`)  \nThe logger name, directory, and logging level can be specified with arguments to `self.init_logger()`.  \n\n```python\nclass Yeehaw(loggi.LoggerMixin):\n    def __init__(self):\n        self.init_logger(name=\"yeet\", log_dir=\"top_secret\", log_level=\"DEBUG\")\n```\n\nLogged messages for the above class will be written to the path `top_secret/yeet.log`.  \n",
    "bugtrack_url": null,
    "license": null,
    "summary": "logger boilerplate with dataclass models for parsing",
    "version": "0.5.0",
    "project_urls": {
        "Documentation": "https://github.com/matt-manes/loggi/tree/main/docs",
        "Homepage": "https://github.com/matt-manes/loggi",
        "Source code": "https://github.com/matt-manes/loggi/tree/main/src/loggi"
    },
    "split_keywords": [
        "log",
        " logger",
        " logging"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "847dedbe59cb8c131cddc139385534bd733b8f27b7f107bb27c8dc026b54cc04",
                "md5": "c127e2b07d4a203999173f535fda6a9d",
                "sha256": "40e626163a7dc9c47f97a03a9b1f4208f048071784b9ab8bd37fb6842aafe817"
            },
            "downloads": -1,
            "filename": "loggi-0.5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c127e2b07d4a203999173f535fda6a9d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<3.12,>=3.10",
            "size": 7187,
            "upload_time": "2024-05-01T19:23:41",
            "upload_time_iso_8601": "2024-05-01T19:23:41.826164Z",
            "url": "https://files.pythonhosted.org/packages/84/7d/edbe59cb8c131cddc139385534bd733b8f27b7f107bb27c8dc026b54cc04/loggi-0.5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "87bfe442af65bb7c27ce97c735e4e007c1b0872ecffc68665bbb5828b1294249",
                "md5": "8851ddb703264b648c5dcf87c9770f1b",
                "sha256": "2bd05ec08402c07207ca076913a35a81a838e1b12164a6b603eda6e345ff9958"
            },
            "downloads": -1,
            "filename": "loggi-0.5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "8851ddb703264b648c5dcf87c9770f1b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.12,>=3.10",
            "size": 6221,
            "upload_time": "2024-05-01T19:23:42",
            "upload_time_iso_8601": "2024-05-01T19:23:42.782173Z",
            "url": "https://files.pythonhosted.org/packages/87/bf/e442af65bb7c27ce97c735e4e007c1b0872ecffc68665bbb5828b1294249/loggi-0.5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-01 19:23:42",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "matt-manes",
    "github_project": "loggi",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "loggi"
}
        
Elapsed time: 0.29907s