chain-logging


Namechain-logging JSON
Version 0.1.9 PyPI version JSON
download
home_pagehttps://github.com/Danangjoyoo/chain-logging
SummaryChain Logger to distinct each request for Python Web Server (Support Flask and FastAPI) and looks like the logger is chained
upload_time2024-07-14 02:32:49
maintainerNone
docs_urlNone
authordanangjoyoo (Agus Danangjoyo)
requires_pythonNone
licenseNone
keywords fastapi logging
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Chain Logging

[![Downloads](https://static.pepy.tech/personalized-badge/flask-toolkits?period=total&units=international_system&left_color=black&right_color=blue&left_text=Downloads)](https://pepy.tech/project/chain-logging)

## 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)
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Danangjoyoo/chain-logging",
    "name": "chain-logging",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "fastapi, logging",
    "author": "danangjoyoo (Agus Danangjoyo)",
    "author_email": "<agus.danangjoyo.blog@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/f2/10/2b26a28194204696613965676eaa61e6f030540e7f29baf2cd76d184a4fd/chain-logging-0.1.9.tar.gz",
    "platform": null,
    "description": "# Chain Logging\n\n[![Downloads](https://static.pepy.tech/personalized-badge/flask-toolkits?period=total&units=international_system&left_color=black&right_color=blue&left_text=Downloads)](https://pepy.tech/project/chain-logging)\n\n## Description\nDistinct every request with an `ID` on your logger and make your log looks like chained\n\n# How to use?\nVery easy to initialize for both FastAPI and Flask.\n\n## FastAPI\n```\nfrom fastapi import FastAPI, Request\nfrom chain_logging.fastapi import ChainLoggerMiddleware, get_chained_logger\n\napp = FastAPI()\n\napp.add_middleware(ChainLoggerMiddleware, before_request=True, after_request=True)\n\n@app.get(\"/home\")\nasync def home(request: Request):\n    logger = await get_chained_logger(request)\n    logger.info(\"this is a trial 1\")\n    logger.error(\"this is a trial 2\")\n    logger.warning(\"this is a trial 3\")\n    logger.critical(\"this is a trial 3\")\n    return {\"status\": \"welcome home!\"}\n```\n\nOutput\n```\n[2022-11-09 03:11:31,835][ID:1667963491835419699][fastapi.py:225][INFO] - Received GET /home\n[2022-11-09 03:11:31,836][ID:1667963491835419699][__init__.py:14][INFO] - this is a trial 1\n[2022-11-09 03:11:31,836][ID:1667963491835419699][__init__.py:15][ERROR] - this is a trial 2\n[2022-11-09 03:11:31,836][ID:1667963491835419699][__init__.py:16][WARNING] - this is a trial 3\n[2022-11-09 03:11:31,836][ID:1667963491835419699][__init__.py:17][CRITICAL] - this is a trial 3\n[2022-11-09 03:11:31,836][ID:1667963491835419699][fastapi.py:236][INFO] - Request done in 1.17ms\n[2022-11-09 03:12:19,152][ID:1667963539151928302][fastapi.py:225][INFO] - Received GET /home\n[2022-11-09 03:12:19,152][ID:1667963539151928302][__init__.py:14][INFO] - this is a trial 1\n[2022-11-09 03:12:19,152][ID:1667963539151928302][__init__.py:15][ERROR] - this is a trial 2\n[2022-11-09 03:12:19,152][ID:1667963539151928302][__init__.py:16][WARNING] - this is a trial 3\n[2022-11-09 03:12:19,152][ID:1667963539151928302][__init__.py:17][CRITICAL] - this is a trial 3\n[2022-11-09 03:12:19,153][ID:1667963539151928302][fastapi.py:236][INFO] - Request done in 1.37ms\n[2022-11-09 03:12:21,892][ID:1667963541892393491][fastapi.py:225][INFO] - Received GET /home\n[2022-11-09 03:12:21,893][ID:1667963541892393491][__init__.py:14][INFO] - this is a trial 1\n[2022-11-09 03:12:21,893][ID:1667963541892393491][__init__.py:15][ERROR] - this is a trial 2\n[2022-11-09 03:12:21,893][ID:1667963541892393491][__init__.py:16][WARNING] - this is a trial 3\n[2022-11-09 03:12:21,893][ID:1667963541892393491][__init__.py:17][CRITICAL] - this is a trial 3\n[2022-11-09 03:12:21,894][ID:1667963541892393491][fastapi.py:236][INFO] - Request done in 1.62ms\n\n```\n\n## Flask\n```\nfrom flask import Flask\nfrom chain_logging.flask import setup_chained_logger, logger\n\napp = Flask(__name__)\n\nsetup_chained_logger(app, before_request=True, after_request=True)\n\n@app.get(\"/home\")\ndef home():\n    logger.info(\"this is a trial 1\")\n    logger.error(\"this is a trial 2\")\n    logger.warning(\"this is a trial 3\")\n    logger.critical(\"this is a trial 3\")\n    return {\"status\": \"welcome home!\"}\n```\n\nOutput\n```\n[2022-11-09 10:23:29,769][ID:1667964209768871143][main.py:252][INFO] - Received GET /home\n[2022-11-09 10:23:29,769][ID:1667964209768871143][main.py:328][INFO] - this is a trial 1\n[2022-11-09 10:23:29,770][ID:1667964209768871143][main.py:329][ERROR] - this is a trial 2\n[2022-11-09 10:23:29,770][ID:1667964209768871143][main.py:330][WARNING] - this is a trial 3\n[2022-11-09 10:23:29,770][ID:1667964209768871143][main.py:331][CRITICAL] - this is a trial 3\n[2022-11-09 10:23:29,771][ID:1667964209768871143][main.py:259][INFO] - Request done in 2.53ms\n[2022-11-09 10:23:30,407][ID:1667964210407508175][main.py:252][INFO] - Received GET /home\n[2022-11-09 10:23:30,408][ID:1667964210407508175][main.py:328][INFO] - this is a trial 1\n[2022-11-09 10:23:30,408][ID:1667964210407508175][main.py:329][ERROR] - this is a trial 2\n[2022-11-09 10:23:30,408][ID:1667964210407508175][main.py:330][WARNING] - this is a trial 3\n[2022-11-09 10:23:30,409][ID:1667964210407508175][main.py:331][CRITICAL] - this is a trial 3\n[2022-11-09 10:23:30,410][ID:1667964210407508175][main.py:259][INFO] - Request done in 2.41ms\n[2022-11-09 10:23:30,831][ID:1667964210831595163][main.py:252][INFO] - Received GET /home\n[2022-11-09 10:23:30,832][ID:1667964210831595163][main.py:328][INFO] - this is a trial 1\n[2022-11-09 10:23:30,832][ID:1667964210831595163][main.py:329][ERROR] - this is a trial 2\n[2022-11-09 10:23:30,832][ID:1667964210831595163][main.py:330][WARNING] - this is a trial 3\n[2022-11-09 10:23:30,833][ID:1667964210831595163][main.py:331][CRITICAL] - this is a trial 3\n[2022-11-09 10:23:30,833][ID:1667964210831595163][main.py:259][INFO] - Request done in 2.07ms\n```\n\n---\n\n# Change Logging format\nCreate your new `ChainLogger` using `ChainFilter`\n\n## FastAPI\n```\nfrom fastapi import FastAPI, Request\nfrom chain_logging.fastapi import ChainLoggerMiddleware, get_chained_logger, ChainLogger, ChainFilter\n\napp = FastAPI()\n\nmy_filter = ChainFilter(log_format=\"%(asctime)s - %(exec_id)s - %(levelname)s - %(message)s\")\nlogger = ChainLogger(name=\"ChainLogger\", filter_object=my_filter)\napp.add_middleware(ChainLoggerMiddleware, logger=logger, before_request=True, after_request=True)\n```\n\n\n## Flask\n```\nfrom flask import Flask\nfrom chain_logging.flask import setup_chained_logger, logger, ChainLogger, ChainFiler\n\napp = Flask(__name__)\n\nmy_filter = ChainFilter(log_format=\"%(asctime)s - %(exec_id)s - %(levelname)s - %(message)s\")\nlogger = ChainLogger(name=\"ChainLogger\", filter_object=my_filter)\nsetup_chained_logger(app, logger=logger, before_request=True, after_request=True)\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Chain Logger to distinct each request for Python Web Server (Support Flask and FastAPI) and looks like the logger is chained",
    "version": "0.1.9",
    "project_urls": {
        "Homepage": "https://github.com/Danangjoyoo/chain-logging"
    },
    "split_keywords": [
        "fastapi",
        " logging"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dfa796ce5a3bbbc333366f58701bd9ad25972c6c5a757bc8ba6c30f28d4fb552",
                "md5": "d22b23093458ca274bfd763137dc12f3",
                "sha256": "a1dd8005b9cfadbb767f47d1a08aa67cc7b19f03445ba9155b35879443914a7d"
            },
            "downloads": -1,
            "filename": "chain_logging-0.1.9-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d22b23093458ca274bfd763137dc12f3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 8302,
            "upload_time": "2024-07-14T02:32:47",
            "upload_time_iso_8601": "2024-07-14T02:32:47.493577Z",
            "url": "https://files.pythonhosted.org/packages/df/a7/96ce5a3bbbc333366f58701bd9ad25972c6c5a757bc8ba6c30f28d4fb552/chain_logging-0.1.9-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f2102b26a28194204696613965676eaa61e6f030540e7f29baf2cd76d184a4fd",
                "md5": "39bf51cb2fe659d6b9053611353daf8e",
                "sha256": "f95eef09abe9312d16b41f2e36825d3b33457db1090a55204a9fe7e41c54a349"
            },
            "downloads": -1,
            "filename": "chain-logging-0.1.9.tar.gz",
            "has_sig": false,
            "md5_digest": "39bf51cb2fe659d6b9053611353daf8e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 7647,
            "upload_time": "2024-07-14T02:32:49",
            "upload_time_iso_8601": "2024-07-14T02:32:49.270274Z",
            "url": "https://files.pythonhosted.org/packages/f2/10/2b26a28194204696613965676eaa61e6f030540e7f29baf2cd76d184a4fd/chain-logging-0.1.9.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-14 02:32:49",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Danangjoyoo",
    "github_project": "chain-logging",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "chain-logging"
}
        
Elapsed time: 0.61924s