Skip to main content

Chain Logger to distinct each request for Python Web Server (Support Flask and FastAPI) and looks like the logger is chained

Project description

Chain Logging

Downloads

Description

Distinct every request with an ID on your logger and make your log looks like chained

How to use?

Very easy to initialize for both FastAPI and Flask.

FastAPI

from fastapi import FastAPI, Request
from chain_logging.fastapi import ChainLoggerMiddleware, get_chained_logger

app = FastAPI()

app.add_middleware(ChainLoggerMiddleware, before_request=True, after_request=True)

@app.get("/home")
async def home(request: Request):
    logger = await get_chained_logger(request)
    logger.info("this is a trial 1")
    logger.error("this is a trial 2")
    logger.warning("this is a trial 3")
    logger.critical("this is a trial 3")
    return {"status": "welcome home!"}

Output

[2022-11-09 03:11:31,835][ID:1667963491835419699][fastapi.py:225][INFO] - Received GET /home
[2022-11-09 03:11:31,836][ID:1667963491835419699][__init__.py:14][INFO] - this is a trial 1
[2022-11-09 03:11:31,836][ID:1667963491835419699][__init__.py:15][ERROR] - this is a trial 2
[2022-11-09 03:11:31,836][ID:1667963491835419699][__init__.py:16][WARNING] - this is a trial 3
[2022-11-09 03:11:31,836][ID:1667963491835419699][__init__.py:17][CRITICAL] - this is a trial 3
[2022-11-09 03:11:31,836][ID:1667963491835419699][fastapi.py:236][INFO] - Request done in 1.17ms
[2022-11-09 03:12:19,152][ID:1667963539151928302][fastapi.py:225][INFO] - Received GET /home
[2022-11-09 03:12:19,152][ID:1667963539151928302][__init__.py:14][INFO] - this is a trial 1
[2022-11-09 03:12:19,152][ID:1667963539151928302][__init__.py:15][ERROR] - this is a trial 2
[2022-11-09 03:12:19,152][ID:1667963539151928302][__init__.py:16][WARNING] - this is a trial 3
[2022-11-09 03:12:19,152][ID:1667963539151928302][__init__.py:17][CRITICAL] - this is a trial 3
[2022-11-09 03:12:19,153][ID:1667963539151928302][fastapi.py:236][INFO] - Request done in 1.37ms
[2022-11-09 03:12:21,892][ID:1667963541892393491][fastapi.py:225][INFO] - Received GET /home
[2022-11-09 03:12:21,893][ID:1667963541892393491][__init__.py:14][INFO] - this is a trial 1
[2022-11-09 03:12:21,893][ID:1667963541892393491][__init__.py:15][ERROR] - this is a trial 2
[2022-11-09 03:12:21,893][ID:1667963541892393491][__init__.py:16][WARNING] - this is a trial 3
[2022-11-09 03:12:21,893][ID:1667963541892393491][__init__.py:17][CRITICAL] - this is a trial 3
[2022-11-09 03:12:21,894][ID:1667963541892393491][fastapi.py:236][INFO] - Request done in 1.62ms

Flask

from flask import Flask
from chain_logging.flask import setup_chained_logger, logger

app = Flask(__name__)

setup_chained_logger(app, before_request=True, after_request=True)

@app.get("/home")
def home():
    logger.info("this is a trial 1")
    logger.error("this is a trial 2")
    logger.warning("this is a trial 3")
    logger.critical("this is a trial 3")
    return {"status": "welcome home!"}

Output

[2022-11-09 10:23:29,769][ID:1667964209768871143][main.py:252][INFO] - Received GET /home
[2022-11-09 10:23:29,769][ID:1667964209768871143][main.py:328][INFO] - this is a trial 1
[2022-11-09 10:23:29,770][ID:1667964209768871143][main.py:329][ERROR] - this is a trial 2
[2022-11-09 10:23:29,770][ID:1667964209768871143][main.py:330][WARNING] - this is a trial 3
[2022-11-09 10:23:29,770][ID:1667964209768871143][main.py:331][CRITICAL] - this is a trial 3
[2022-11-09 10:23:29,771][ID:1667964209768871143][main.py:259][INFO] - Request done in 2.53ms
[2022-11-09 10:23:30,407][ID:1667964210407508175][main.py:252][INFO] - Received GET /home
[2022-11-09 10:23:30,408][ID:1667964210407508175][main.py:328][INFO] - this is a trial 1
[2022-11-09 10:23:30,408][ID:1667964210407508175][main.py:329][ERROR] - this is a trial 2
[2022-11-09 10:23:30,408][ID:1667964210407508175][main.py:330][WARNING] - this is a trial 3
[2022-11-09 10:23:30,409][ID:1667964210407508175][main.py:331][CRITICAL] - this is a trial 3
[2022-11-09 10:23:30,410][ID:1667964210407508175][main.py:259][INFO] - Request done in 2.41ms
[2022-11-09 10:23:30,831][ID:1667964210831595163][main.py:252][INFO] - Received GET /home
[2022-11-09 10:23:30,832][ID:1667964210831595163][main.py:328][INFO] - this is a trial 1
[2022-11-09 10:23:30,832][ID:1667964210831595163][main.py:329][ERROR] - this is a trial 2
[2022-11-09 10:23:30,832][ID:1667964210831595163][main.py:330][WARNING] - this is a trial 3
[2022-11-09 10:23:30,833][ID:1667964210831595163][main.py:331][CRITICAL] - this is a trial 3
[2022-11-09 10:23:30,833][ID:1667964210831595163][main.py:259][INFO] - Request done in 2.07ms

Change Logging format

Create your new ChainLogger using ChainFilter

FastAPI

from fastapi import FastAPI, Request
from chain_logging.fastapi import ChainLoggerMiddleware, get_chained_logger, ChainLogger, ChainFilter

app = FastAPI()

my_filter = ChainFilter(log_format="%(asctime)s - %(exec_id)s - %(levelname)s - %(message)s")
logger = ChainLogger(name="ChainLogger", filter_object=my_filter)
app.add_middleware(ChainLoggerMiddleware, logger=logger, before_request=True, after_request=True)

Flask

from flask import Flask
from chain_logging.flask import setup_chained_logger, logger, ChainLogger, ChainFiler

app = Flask(__name__)

my_filter = ChainFilter(log_format="%(asctime)s - %(exec_id)s - %(levelname)s - %(message)s")
logger = ChainLogger(name="ChainLogger", filter_object=my_filter)
setup_chained_logger(app, logger=logger, before_request=True, after_request=True)

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

chain-logging-0.1.3.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

chain_logging-0.1.3-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file chain-logging-0.1.3.tar.gz.

File metadata

  • Download URL: chain-logging-0.1.3.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.11.4 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for chain-logging-0.1.3.tar.gz
Algorithm Hash digest
SHA256 70f5d27029ab0a4664fa8e012e04ed892a50e4c5a37c6987628d12fa57354e55
MD5 078611634fcaacc6464e479370c44d63
BLAKE2b-256 3aeaaf38df76496473699730f97764c4c9914682cbe390bd7fcc5bb30e7f3453

See more details on using hashes here.

File details

Details for the file chain_logging-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: chain_logging-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.11.4 pkginfo/1.8.2 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for chain_logging-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6b645926e4fa0fe0996a39443e7866eeb1ac50273a0de423d389738bed0c9b22
MD5 7c3663f2364d12e8a5d8965a1f8bef49
BLAKE2b-256 c7b2ab9d7d26253fc61978721a1e9578e47b3b560d25bb5ca1f35eaea60b4932

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