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.5.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: chain-logging-0.1.5.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.5.tar.gz
Algorithm Hash digest
SHA256 4e6ca3db2192b96438138b72172f7fab541e1266315d03a47eda12e213fd050c
MD5 e3b1bfbd9c5b5549a1b119b866992198
BLAKE2b-256 0d1f9659c9032a0450f09c54d3df8a9385a5ec2ee826cbfaaa5ed1bc72f9c351

See more details on using hashes here.

File details

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

File metadata

  • Download URL: chain_logging-0.1.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 54beed9b6cded550ca64c599a96d0a8c76ef404adf6f66ab4eb4d6ce1d424246
MD5 9225ec3d708b8cce9c8bc616a3eb2371
BLAKE2b-256 e73788afcca25fa6bf67a8af6062e91b2a74474a720d66dcd57b5b3cab19c3f3

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