Wrapper around Google Cloud Platform (GCP) logging library to simplify creation of traced logs.
Project description
Introduction
This project provides easy to use wrapper around the Google Cloud Platform logging library to simplify creation of the traced logs.
Quick Start
from surquest.GCP.logger import Logger
logger = Logger()
logger.log(
severity="INFO",
msg="This is a test message", # log message
**{ # context information as KWARGS
"key1": "value1",
"key2": "value2"
}
)
# Alternatively you can set the severity via the method name
logger.debug(msg="This is a DEBUG message")
logger.info(msg="This is a INFO message")
logger.warning(msg="This is a WARNING message")
logger.error(msg="This is a ERROR message")
logger.critical(msg="This is a CRITICAL message")
FastAPI integration
Following example shows how to integrate the Tracer class with FastAPI endpoints:
from surquest.GCP.logger import Logger
import requests
from fastapi import FastAPI, Depends, Query, Path
app = FastAPI()
@app.get("/exchange/currencies/{base}/{quote}")
async def exchange(
base: str = Path(..., description="Base currency"),
quote: str = Path(..., description="Quote currency"),
amount: float = Query(..., gt=0, description="Amount to exchange"),
logger: Logger = Depends(Logger)
):
# get exchange rate from external API
response = requests.get(
f"https://api.exchangeratesapi.io/v1/latest?base={base}&symbols={quote}",
params={
"base": base,
"symbols": quote,
"access_key": "<your_api_key>" # please set your own API key
}
).json()["rates"][quote]
if 200 <= response.status_code < 300:
exchange_rate = response.json()["rates"][quote]
logger.info(
msg="Retrieval of Exchange rates finished successfully",
**{
"currencies": {
"base": base,
"quote": quote
},
"exchangeRate": exchange_rate
}
)
else:
logger.error(
msg="Failed to retrieve exchange rate",
base=base,
quote=quote,
amount=amount,
response=response
)
return {
"info": "Failed to retrieve exchange rate",
"error": response.text
}
result = amount * exchange_rate
return {
"currencies": {
"base": base,
"quote": quote
},
"amount": {
"base": amount,
"quote": result
}
}
Local development
You are more than welcome to contribute to this project. To make your start easier we have prepared a docker image with all the necessary tools to run it as interpreter for Pycharm or to run tests.
Build docker image
docker build `
--tag surquest/gcp/logger `
--file package.base.dockerfile `
--target test .
Run tests
docker run --rm -it `
-v "${pwd}:/opt/project" `
-e "GOOGLE_APPLICATION_CREDENTIALS=/opt/project/credentials/keyfile.json" `
-w "/opt/project/test" `
surquest/gcp/logger pytest
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
File details
Details for the file surquest_gcp_logger-0.1.1rc2.tar.gz
.
File metadata
- Download URL: surquest_gcp_logger-0.1.1rc2.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25335d518c23a52f461cb5af448cf749e0f63256dd22d5220f00fd42b30cc904 |
|
MD5 | 12fc928a50d3e1f9ccf1b9238229718d |
|
BLAKE2b-256 | 4a805e367800b40416a953b024796165819c8838080407569ecc9ea5ee1196e5 |
File details
Details for the file surquest_gcp_logger-0.1.1rc2-py2.py3-none-any.whl
.
File metadata
- Download URL: surquest_gcp_logger-0.1.1rc2-py2.py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23028750592b2e52c027db9921409b3f9b1baf2dd12a9b13756e5088fc90e7cd |
|
MD5 | 05afffda7b0689513a3973b5463aa96d |
|
BLAKE2b-256 | 4028f39b90d9499d625804df0ebe905a1ec4312656f3677e003d8e90670a504b |