Metadata-Version: 2.1
Name: logger_extras
Version: 0.4.2
Summary: A collection of useful logging tools
Author: Will Barber
License: MIT License
        
        Copyright (c) 2023 Will Barber
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Repository, https://github.com/WillB97/logger-extras
Project-URL: Homepage, https://github.com/WillB97/logger-extras
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: OS Independent
Classifier: Development Status :: 4 - Beta
Classifier: Typing :: Typed
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: typing-extensions; python_version < "3.10"
Provides-Extra: dev
Requires-Dist: poethepoet<1,>=0.0.1; extra == "dev"
Requires-Dist: ruff<0.4,>=0.3.0; extra == "dev"
Requires-Dist: mypy==1.9.0; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: pytest; extra == "dev"
Requires-Dist: pytest-cov; extra == "dev"
Requires-Dist: paho-mqtt<3,>=2; extra == "dev"
Provides-Extra: mqtt
Requires-Dist: paho-mqtt<3,>=2; extra == "mqtt"

# logging_extras

A collection of useful logging tools for Python 3.

## Installation

The majority of the tools in this library require no additional dependencies. To install the library with the base functionality use the following command.

```bash
pip install logging-extras
```

To use the `MQTT*` classes, install the library with the following command.

```bash
pip install logging-extras[mqtt]
```

## Tools

- `MQTThandler` - A logging handler that publishes logs to an MQTT broker.
- `RelativeTimeFilter` - A logging filter that adds a relative time to the log record.
- `DiffTimeFilter` - A logging filter that adds a time difference to the log record.
- `TieredFormatter` - A logging formatter that allows for different formatting based on the log level.
- `log_function_call` - A decorator that logs the arguments and return value of a function call.

Upcomming tools:

- `MQTTSubscriber` - A logging listener that subscribes to an MQTT broker and logs messages.

## Examples

### `MQTThandler`

```python
import logging

from paho.mqtt.client import Client
from logger_extras import MQTTConfig, MQTThandler

handler = MQTThandler(
    host="localhost",
    topic="logs",
    append_logger_name=True,
)
handler.setFormatter(logging.Formatter("%(levelname)s - %(message)s)"))

logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

logger.info("Hello World!")

# This will publish the following message to the MQTT broker on the topic "logs/__main__":
# {
#   "timestamp": 1690226241.740354,
#   "message": "INFO - Hello World!",
#   "raw_message": "Hello World!",
#   "level": "INFO", "name": "__main__"
# }

```

### `RelativeTimeFilter`

```python
import logging
from time import sleep

from logger_extras import RelativeTimeFilter

handler = logging.StreamHandler()
relative_time_filter = RelativeTimeFilter()
handler.addFilter(relative_time_filter)
handler.setFormatter(logging.Formatter("%(reltime).3f - %(message)s"))

logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

logger.info("Hello World!")
sleep(2)
logger.info("Hello World!")
relative_time_filter.reset_time_reference()
sleep(1)
logger.info("Hello World!")

# This will log the following:
# 0:00:00 - Hello World!
# 0:00:02 - Hello World!
# 0:00:01 - Hello World!
```

### `DiffTimeFilter`

```python
import logging
from time import sleep

from logger_extras import DiffTimeFilter

handler = logging.StreamHandler()
handler.addFilter(DiffTimeFilter())
handler.setFormatter(logging.Formatter("%(difftime).3f - %(message)s"))

logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

logger.info("Hello World!")
sleep(2)
logger.info("Hello World!")
sleep(1)
logger.info("Hello World!")

# This will log the following:
# 0:00:00 - Hello World!
# 0:00:02 - Hello World!
# 0:00:01 - Hello World!
```

### `TieredFormatter`

```python
import logging

from logger_extras import TieredFormatter

handler = logging.StreamHandler()
handler.setFormatter(
    TieredFormatter(
        fmt="%(levelname)s - %(message)s",
        level_fmts={
            logging.DEBUG: "%(levelname)s - %(funcName)s:%(lineno)d - %(message)s",
            logging.INFO: "%(message)s",
        },
        datefmt=None,
        style="%",
        validate=True,
    )
)

logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)

logger.debug("Hello World!")
logger.info("Hello World!")
logger.warning("Hello World!")
logger.error("Hello World!")
logger.critical("Hello World!")

# This will log the following:
# DEBUG - __main__:19 - Hello World!
# Hello World!
# WARNING - Hello World!
# ERROR - Hello World!
# CRITICAL - Hello World!
```

### `log_function_call`

```python
from logger_extras import log_function_call

@log_function_call(level=logging.INFO)
def add(a, b):
    return a + b

_ = add(1, 2)
# This will log the following:
# 2021-08-03 12:00:00,000 - INFO - Calling __main__.add(a=1, b=2)
# 2021-08-03 12:00:00,000 - INFO - '__main__.add' returned 3
```
