Skip to main content

Moonhub.ai logger to push logs into GCP Cloud Logging

Project description

Moonhub Logger

The mh_logger handles logging into GCP and locally in a seamless manner.

Installation

python3 -m pip install mh_logger

User guide

Basic log

A basic log is composed of a message and kwargs to be logged.

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 or logger_id is a special binded variable added by default to all logs.

Multi threading

We must explicitly propagate the binded variables (including the request_id) to other threads. Example:

logger.info("Log on the main thread")
with ThreadPoolExecutor(
    initializer=logger.bind, initargs=(logger.binded,)
) 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"
}

Contribute to the Moonhub Logger

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

Deploy to pypi

https://packaging.python.org/en/latest/tutorials/packaging-projects/#generating-distribution-archives

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.

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

mh-logger-0.1.40.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

mh_logger-0.1.40-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file mh-logger-0.1.40.tar.gz.

File metadata

  • Download URL: mh-logger-0.1.40.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for mh-logger-0.1.40.tar.gz
Algorithm Hash digest
SHA256 56dc266e0f9904d0e240dc2a689b2480e9e5e3649f3a17a68b2ead355296f2f7
MD5 8599bb74d2fc79dfc551a9f268343820
BLAKE2b-256 c3d04ba5cf5cb6a5f155510c59ce055c87f875ec5bda970c495b545c340ee908

See more details on using hashes here.

File details

Details for the file mh_logger-0.1.40-py3-none-any.whl.

File metadata

  • Download URL: mh_logger-0.1.40-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for mh_logger-0.1.40-py3-none-any.whl
Algorithm Hash digest
SHA256 1735c573a080c68081140ae3cb034e3f2e9b2b49ed58b9509bf42c28ef07f17a
MD5 a003b9b4f2f4b75dd907e337868dcab3
BLAKE2b-256 cb3b833581eee61993b16216e32ddbfee15c4ead87cb7791a75ba8231ccbadf9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page