Skip to main content

Wrapper around Google Cloud Platform (GCP) logging library to simplify creation of traced logs.

Project description

GitHub GitHub Workflow Status (with branch) Coverage PyPI - Downloads PyPI

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

surquest_gcp_logger-0.1.1rc1.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

surquest_gcp_logger-0.1.1rc1-py2.py3-none-any.whl (5.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file surquest_gcp_logger-0.1.1rc1.tar.gz.

File metadata

File hashes

Hashes for surquest_gcp_logger-0.1.1rc1.tar.gz
Algorithm Hash digest
SHA256 6d5ab6c5fc6f2a33c4269ac7f07973f4807ed150719e14cd3ce52d903dc58c94
MD5 583e82b8a9465307beff66ca64bb71ed
BLAKE2b-256 a69ffb313cf8295b89e9b83f9d32318ff34e98362327a38cbc40c2644f7b9fb3

See more details on using hashes here.

File details

Details for the file surquest_gcp_logger-0.1.1rc1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for surquest_gcp_logger-0.1.1rc1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5f01bd009e73162578c5dd261852adcb3e1fcee570cdad6fc5d070e5c6e669ca
MD5 90c03bb4367d1727b27be559d315ffa0
BLAKE2b-256 853e2d9533242617b7e5c8a99d051ba0a8fad0c7b408a65e5b153429a7898f92

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