Metadata-Version: 2.1
Name: mh-logger
Version: 0.0.40
Summary: MoonHub.ai logging manager to push logs into GCP Cloud Logging
Home-page: https://github.com/MoonhubTeam/logger
Author: Luis Galeana
Author-email: luis.galeana@moonhub.ai
Project-URL: Bug Tracker, https://github.com/MoonhubTeam/logger/issues
Project-URL: repository, https://github.com/MoonhubTeam/logger
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE.txt

# Moonhub Logger

## Installation

```
python3 -m pip install mh_logger
```

## User guide

```
from mh_logger import LoggingManager
logger = LoggingManager(__name__)
logger.info("This is a sample log", foo="bar")
```
Output:
```
2023-03-16 20:22:36,561 [INFO] - Test: This is a sample log - JSON Payload: {
    'foo': 'bar',
    "_message": "This is a sample log",
    "request_id": "b30cb564210a4172b00d273ad441f9e2"
}
```

### Binded variables

Binded variables are variables preserved across different modules and functions within the same thread (from the `threading` module) or within the same Task (from the `asyncio` module). Example:

```
logger.bind(foo="bar", hello="world")

def a():
    logger.info("My first log")

def b():
    logger.info("My second log")

a()
b()
```
Output:
```
2023-03-16 20:22:36,561 [INFO] - Test: My first log - JSON Payload: {
    "foo": "bar",
    "hello": "world",
    "_message": "My first log",
    "request_id": "b30cb564210a4172b00d273ad441f9e2"
}
2023-03-16 20:22:37,561 [INFO] - Test: My second log - JSON Payload: {
    "foo": "bar",
    "hello": "world",
    "_message": "My second log",
    "request_id": "b30cb564210a4172b00d273ad441f9e2"
}
```

### The `request_id`

The `request_id` (aka `logger_id`) is an unique identifer for all logs within a Threard (from the `threading` module) or within a Task (from the `asyncio` module). Example:

### Multi threading

We must explicitly propagate the `logger_id` to other threads. Example:

```
logger.info("Log on the main thread")
with ThreadPoolExecutor(
    initializer=logger._set_logger_id, initargs=(logger.logger_id,)
) as executor:
    executor.submit(logger.info, "Log on a different thread")
```
Output:
```
2023-03-16 20:22:36,561 [INFO] - Test: Log on the main thread - JSON Payload: {
    "_message": "Log on the main thread",
    "request_id": "b30cb564210a4172b00d273ad441f9e2"
}
2023-03-16 20:22:37,561 [INFO] - Test: Log on a different thread - JSON Payload: {
    "_message": "Log on a different thread",
    "request_id": "b30cb564210a4172b00d273ad441f9e2"
}
```

## Binded vars

You can bind variables to

Set following environment variable if you want to push logs to GCP Cloud Logging service while testing locally.
```
GCP_SERVICE_KEY=google-service-key.json
```

## Create and upload the mh_logger package
https://packaging.python.org/en/latest/tutorials/packaging-projects/

You will need to add `--skip-existing`.

MIT License

Copyright (c) [year] [fullname]

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.
