chain-logging


Namechain-logging JSON
Version 0.1.7 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-03-08 04:14:11
maintainer
docs_urlNone
authordanangjoyoo (Agus Danangjoyo)
requires_python
license
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": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "fastapi,logging",
    "author": "danangjoyoo (Agus Danangjoyo)",
    "author_email": "<agus.danangjoyo.blog@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/34/47/c2c2b2a9bf6d91ba59f036db87e172318550f2664aa49a19e7ac7654035c/chain-logging-0.1.7.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": "",
    "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.7",
    "project_urls": {
        "Homepage": "https://github.com/Danangjoyoo/chain-logging"
    },
    "split_keywords": [
        "fastapi",
        "logging"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dee91cceddb0da3462b0111d857a82d86735dbb36a42f353771033651ca7c6e5",
                "md5": "6b99f35d635871ac867f41bd251e76e9",
                "sha256": "22ac185e93b7c5364fea09f12740cf4c7b3b6938b0243e53c1ef09b0feae7a0d"
            },
            "downloads": -1,
            "filename": "chain_logging-0.1.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6b99f35d635871ac867f41bd251e76e9",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 8288,
            "upload_time": "2024-03-08T04:14:10",
            "upload_time_iso_8601": "2024-03-08T04:14:10.054490Z",
            "url": "https://files.pythonhosted.org/packages/de/e9/1cceddb0da3462b0111d857a82d86735dbb36a42f353771033651ca7c6e5/chain_logging-0.1.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3447c2c2b2a9bf6d91ba59f036db87e172318550f2664aa49a19e7ac7654035c",
                "md5": "5d4ddd525181403b0dd8d02500eba107",
                "sha256": "6ad3885eae9180c37cb63c74417aa13bff6655bf8f09c060f3fc0dae35c92a16"
            },
            "downloads": -1,
            "filename": "chain-logging-0.1.7.tar.gz",
            "has_sig": false,
            "md5_digest": "5d4ddd525181403b0dd8d02500eba107",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 7613,
            "upload_time": "2024-03-08T04:14:11",
            "upload_time_iso_8601": "2024-03-08T04:14:11.980509Z",
            "url": "https://files.pythonhosted.org/packages/34/47/c2c2b2a9bf6d91ba59f036db87e172318550f2664aa49a19e7ac7654035c/chain-logging-0.1.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-08 04:14:11",
    "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.18404s