beans-logging-fastapi 1.1.1__py3-none-any.whl → 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,385 +0,0 @@
1
- Metadata-Version: 2.1
2
- Name: beans-logging-fastapi
3
- Version: 1.1.1
4
- Summary: 'beans_logging_fastapi' is a middleware for FastAPI to log HTTP access. It is based on 'beans-logging' package.
5
- Home-page: https://github.com/bybatkhuu/module.fastapi-logging
6
- Download-URL: https://github.com/bybatkhuu/module.fastapi-logging/archive/v1.1.1.tar.gz
7
- Author: Batkhuu Byambajav
8
- Author-email: batkhuu10@gmail.com
9
- License: MIT
10
- Keywords: beans_logging_fastapi,fastapi-logging,fastapi-logging-middleware,fastapi-middleware,logging-middleware,middleware,beans-logging,http-access-logging,logging,logger,loguru
11
- Classifier: Development Status :: 4 - Beta
12
- Classifier: Intended Audience :: Developers
13
- Classifier: Topic :: Software Development :: Build Tools
14
- Classifier: License :: OSI Approved :: MIT License
15
- Classifier: Programming Language :: Python :: 3.8
16
- Classifier: Programming Language :: Python :: 3.9
17
- Classifier: Programming Language :: Python :: 3.10
18
- Classifier: Programming Language :: Python :: 3.11
19
- Requires-Python: >=3.8
20
- Description-Content-Type: text/markdown
21
- License-File: LICENCE.txt
22
- Requires-Dist: fastapi <1.0.0,>=0.99.1
23
- Requires-Dist: beans-logging <7.0.0,>=6.0.0
24
-
25
- # beans_logging_fastapi
26
-
27
- [![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://choosealicense.com/licenses/mit)
28
- [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/bybatkhuu/module.fastapi-logging/2.build-publish.yml?logo=GitHub)](https://github.com/bybatkhuu/module.fastapi-logging/actions/workflows/2.build-publish.yml)
29
- [![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/bybatkhuu/module.fastapi-logging?logo=GitHub)](https://github.com/bybatkhuu/module.fastapi-logging/releases)
30
- [![PyPI](https://img.shields.io/pypi/v/beans-logging-fastapi?logo=PyPi)](https://pypi.org/project/beans-logging-fastapi)
31
- [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/beans-logging-fastapi?logo=Python)](https://docs.conda.io/en/latest/miniconda.html)
32
-
33
- `beans_logging_fastapi` is a middleware for FastAPI to log HTTP access.
34
-
35
- It is based on **'beans-logging'** package.
36
-
37
- ## Features
38
-
39
- - **Logger** based on **'beans-logging'** package
40
- - **FastAPI** HTTP access logging **middleware**
41
-
42
- ---
43
-
44
- ## Installation
45
-
46
- ### 1. Prerequisites
47
-
48
- - **Python (>= v3.8)**
49
- - **PyPi (>= v23)**
50
-
51
- ### 2. Install beans-logging-fastapi package
52
-
53
- Choose one of the following methods to install the package **[A ~ F]**:
54
-
55
- **A.** [**RECOMMENDED**] Install from **PyPi**
56
-
57
- ```sh
58
- # Install or upgrade package:
59
- pip install -U beans-logging-fastapi
60
- ```
61
-
62
- **B.** Install latest version from **GitHub**
63
-
64
- ```sh
65
- # Install package by git:
66
- pip install git+https://github.com/bybatkhuu/module.fastapi-logging.git
67
- ```
68
-
69
- **C.** Install from **pre-built release** files
70
-
71
- 1. Download **`.whl`** or **`.tar.gz`** file from **releases** - <https://github.com/bybatkhuu/module.fastapi-logging/releases>
72
- 2. Install with pip:
73
-
74
- ```sh
75
- # Install from .whl file:
76
- pip install ./beans_logging_fastapi-[VERSION]-py3-none-any.whl
77
- # Or install from .tar.gz file:
78
- pip install ./beans_logging_fastapi-[VERSION].tar.gz
79
- ```
80
-
81
- **D.** Install from **source code** by building package
82
-
83
- ```sh
84
- # Clone repository by git:
85
- git clone https://github.com/bybatkhuu/module.fastapi-logging.git beans_logging_fastapi
86
- cd ./beans_logging_fastapi
87
-
88
- # Install python build tool:
89
- pip install -U pip build
90
-
91
- # Build python package:
92
- python -m build
93
-
94
- _VERSION=$(./scripts/get-version.sh)
95
-
96
- # Install from .whl file:
97
- pip install ./dist/beans_logging_fastapi-${_VERSION}-py3-none-any.whl
98
- # Or install from .tar.gz file:
99
- pip install ./dist/beans_logging_fastapi-${_VERSION}.tar.gz
100
- ```
101
-
102
- **E.** Install with pip editable **development mode** (from source code)
103
-
104
- ```sh
105
- # Clone repository by git:
106
- git clone https://github.com/bybatkhuu/module.fastapi-logging.git beans_logging_fastapi
107
- cd ./beans_logging_fastapi
108
-
109
- # Install with editable development mode:
110
- pip install -e .
111
- ```
112
-
113
- **F.** Manually add to **PYTHONPATH** (not recommended)
114
-
115
- ```sh
116
- # Clone repository by git:
117
- git clone https://github.com/bybatkhuu/module.fastapi-logging.git beans_logging_fastapi
118
- cd ./beans_logging_fastapi
119
-
120
- # Install python dependencies:
121
- pip install -r ./requirements.txt
122
-
123
- # Add current path to PYTHONPATH:
124
- export PYTHONPATH="${PWD}:${PYTHONPATH}"
125
- ```
126
-
127
- ## Usage/Examples
128
-
129
- To use `beans_logging_fastapi`:
130
-
131
- ### **FastAPI**
132
-
133
- [**`configs/logger.yml`**](https://github.com/bybatkhuu/module.fastapi-logging/blob/main/examples/configs/logger.yml):
134
-
135
- ```yaml
136
- logger:
137
- app_name: "fastapi-app"
138
- level: "TRACE"
139
- use_diagnose: false
140
- stream:
141
- use_color: true
142
- use_icon: false
143
- format_str: "[<c>{time:YYYY-MM-DD HH:mm:ss.SSS Z}</c> | <level>{level_short:<5}</level> | <w>{name}:{line}</w>]: <level>{message}</level>"
144
- std_handler:
145
- enabled: true
146
- file:
147
- logs_dir: "./logs"
148
- rotate_size: 10000000 # 10MB
149
- rotate_time: "00:00:00"
150
- backup_count: 90
151
- log_handlers:
152
- enabled: true
153
- format_str: "[{time:YYYY-MM-DD HH:mm:ss.SSS Z} | {level_short:<5} | {name}:{line}]: {message}"
154
- log_path: "{app_name}.std.all.log"
155
- err_path: "{app_name}.std.err.log"
156
- json_handlers:
157
- enabled: true
158
- use_custom: false
159
- log_path: "json/{app_name}.json.all.log"
160
- err_path: "json/{app_name}.json.err.log"
161
- intercept:
162
- auto_load:
163
- enabled: true
164
- only_base: false
165
- ignore_modules: []
166
- include_modules: []
167
- mute_modules: ["uvicorn.access"]
168
- extra:
169
- http_std_debug_format: '<n>[{request_id}]</n> {client_host} {user_id} "<u>{method} {url_path}</u> HTTP/{http_version}"'
170
- http_std_msg_format: '<n><w>[{request_id}]</w></n> {client_host} {user_id} "<u>{method} {url_path}</u> HTTP/{http_version}" {status_code} {content_length}B {response_time}ms'
171
- http_file_enabled: true
172
- http_file_format: '{client_host} {request_id} {user_id} [{datetime}] "{method} {url_path} HTTP/{http_version}" {status_code} {content_length} "{h_referer}" "{h_user_agent}" {response_time}'
173
- http_file_tz: "localtime"
174
- http_log_path: "http/{app_name}.http.access.log"
175
- http_err_path: "http/{app_name}.http.err.log"
176
- http_json_enabled: true
177
- http_json_path: "json.http/{app_name}.json.http.access.log"
178
- http_json_err_path: "json.http/{app_name}.json.http.err.log"
179
- ```
180
-
181
- [**`.env`**](https://github.com/bybatkhuu/module.fastapi-logging/blob/main/examples/.env):
182
-
183
- ```sh
184
- ENV=development
185
- DEBUG=true
186
-
187
- BEANS_LOGGING_DISABLE_DEFAULT=false
188
- BEANS_LOGGING_CONFIG_PATH="./configs/logger.yml"
189
- BEANS_LOGGING_LOGS_DIR="./logs"
190
- ```
191
-
192
- [**`logger.py`**](https://github.com/bybatkhuu/module.fastapi-logging/blob/main/examples/logger.py):
193
-
194
- ```python
195
- from beans_logging import Logger, LoggerLoader
196
- from beans_logging_fastapi import (
197
- add_http_file_handler,
198
- add_http_file_json_handler,
199
- http_file_format,
200
- )
201
-
202
- logger_loader = LoggerLoader()
203
- logger: Logger = logger_loader.load()
204
-
205
-
206
- def _http_file_format(record: dict) -> str:
207
- _format = http_file_format(
208
- record=record,
209
- msg_format=logger_loader.config.extra.http_file_format,
210
- tz=logger_loader.config.extra.http_file_tz,
211
- )
212
- return _format
213
-
214
-
215
- if logger_loader.config.extra.http_file_enabled:
216
- add_http_file_handler(
217
- logger_loader=logger_loader,
218
- log_path=logger_loader.config.extra.http_log_path,
219
- err_path=logger_loader.config.extra.http_err_path,
220
- formatter=_http_file_format,
221
- )
222
-
223
- if logger_loader.config.extra.http_json_enabled:
224
- add_http_file_json_handler(
225
- logger_loader=logger_loader,
226
- log_path=logger_loader.config.extra.http_json_path,
227
- err_path=logger_loader.config.extra.http_json_err_path,
228
- )
229
- ```
230
-
231
- [**`main.py`**](https://github.com/bybatkhuu/module.fastapi-logging/blob/main/examples/main.py):
232
-
233
- ```python
234
- from typing import Union
235
- from contextlib import asynccontextmanager
236
-
237
- import uvicorn
238
- from dotenv import load_dotenv
239
- from fastapi import FastAPI, HTTPException
240
- from fastapi.responses import RedirectResponse
241
-
242
- load_dotenv()
243
-
244
- from beans_logging_fastapi import (
245
- HttpAccessLogMiddleware,
246
- RequestHTTPInfoMiddleware,
247
- ResponseHTTPInfoMiddleware,
248
- )
249
-
250
- from logger import logger, logger_loader
251
- from __version__ import __version__
252
-
253
-
254
- @asynccontextmanager
255
- async def lifespan(app: FastAPI):
256
- logger.info("Preparing to startup...")
257
- logger.success("Finished preparation to startup.")
258
- logger.info(f"API version: {__version__}")
259
-
260
- yield
261
- logger.info("Praparing to shutdown...")
262
- logger.success("Finished preparation to shutdown.")
263
-
264
-
265
- app = FastAPI(lifespan=lifespan, version=__version__)
266
-
267
- app.add_middleware(ResponseHTTPInfoMiddleware)
268
- app.add_middleware(
269
- HttpAccessLogMiddleware,
270
- debug_format=logger_loader.config.extra.http_std_debug_format,
271
- msg_format=logger_loader.config.extra.http_std_msg_format,
272
- )
273
- app.add_middleware(
274
- RequestHTTPInfoMiddleware, has_proxy_headers=True, has_cf_headers=True
275
- )
276
-
277
-
278
- @app.get("/")
279
- def root():
280
- return {"Hello": "World"}
281
-
282
-
283
- @app.get("/items/{item_id}")
284
- def read_item(item_id: int, q: Union[str, None] = None):
285
- return {"item_id": item_id, "q": q}
286
-
287
-
288
- @app.get("/continue", status_code=100)
289
- def get_continue():
290
- return {}
291
-
292
-
293
- @app.get("/redirect")
294
- def redirect():
295
- return RedirectResponse("/")
296
-
297
-
298
- @app.get("/error")
299
- def error():
300
- raise HTTPException(status_code=500)
301
-
302
-
303
- if __name__ == "__main__":
304
- uvicorn.run(
305
- app="main:app",
306
- host="0.0.0.0",
307
- port=8000,
308
- access_log=False,
309
- server_header=False,
310
- proxy_headers=True,
311
- forwarded_allow_ips="*",
312
- )
313
- ```
314
-
315
- Run the [**`examples`**](https://github.com/bybatkhuu/module.fastapi-logging/tree/main/examples):
316
-
317
- ```sh
318
- cd ./examples
319
- # Install python dependencies for examples:
320
- pip install -r ./requirements.txt
321
-
322
- uvicorn main:app --host=0.0.0.0 --port=8000
323
- ```
324
-
325
- **Output**:
326
-
327
- ```txt
328
- [2023-10-31 12:38:46.733 +09:00 | TRACE | beans_logging._base:578]: Intercepted modules: ['concurrent', 'concurrent.futures', 'watchfiles.watcher', 'dotenv.main', 'watchfiles.main', 'watchfiles', 'asyncio', 'fastapi', 'uvicorn.error', 'uvicorn', 'dotenv']; Muted modules: ['uvicorn.access'];
329
- [2023-10-31 12:38:46.749 +09:00 | INFO | uvicorn.server:76]: Started server process [56216]
330
- [2023-10-31 12:38:46.749 +09:00 | INFO | uvicorn.lifespan.on:46]: Waiting for application startup.
331
- [2023-10-31 12:38:46.750 +09:00 | INFO | main:26]: Preparing to startup...
332
- [2023-10-31 12:38:46.750 +09:00 | OK | main:27]: Finished preparation to startup.
333
- [2023-10-31 12:38:46.750 +09:00 | INFO | main:28]: API version: 0.0.0-000000
334
- [2023-10-31 12:38:46.750 +09:00 | INFO | uvicorn.lifespan.on:60]: Application startup complete.
335
- [2023-10-31 12:38:46.752 +09:00 | INFO | uvicorn.server:218]: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
336
- [2023-10-31 12:38:48.701 +09:00 | DEBUG | anyio._backends._asyncio:807]: [c3d76377250947c29559cb103a6d2c56] 127.0.0.1 - "GET / HTTP/1.1"
337
- [2023-10-31 12:38:48.703 +09:00 | OK | anyio._backends._asyncio:807]: [c3d76377250947c29559cb103a6d2c56] 127.0.0.1 - "GET / HTTP/1.1" 200 17B 0.8ms
338
- ^C[2023-10-31 12:38:49.793 +09:00 | INFO | uvicorn.server:264]: Shutting down
339
- [2023-10-31 12:38:49.903 +09:00 | INFO | uvicorn.lifespan.on:65]: Waiting for application shutdown.
340
- [2023-10-31 12:38:49.903 +09:00 | INFO | main:31]: Praparing to shutdown...
341
- [2023-10-31 12:38:49.904 +09:00 | OK | main:32]: Finished preparation to shutdown.
342
- [2023-10-31 12:38:49.904 +09:00 | INFO | uvicorn.lifespan.on:76]: Application shutdown complete.
343
- [2023-10-31 12:38:49.904 +09:00 | INFO | uvicorn.server:86]: Finished server process [56216]
344
- ```
345
-
346
- ---
347
-
348
- ## Running Tests
349
-
350
- To run tests, run the following command:
351
-
352
- ```sh
353
- # Install python test dependencies:
354
- pip install -r ./requirements.test.txt
355
-
356
- # Run tests:
357
- python -m pytest -sv
358
- ```
359
-
360
- ## Environment Variables
361
-
362
- You can use the following environment variables inside [**`.env.example`**](https://github.com/bybatkhuu/module.fastapi-logging/blob/main/.env.example) file:
363
-
364
- ```sh
365
- ENV=development
366
- DEBUG=true
367
-
368
- BEANS_LOGGING_DISABLE_DEFAULT=false
369
- BEANS_LOGGING_CONFIG_PATH="./configs/logger.yml"
370
- BEANS_LOGGING_LOGS_DIR="./logs"
371
- ```
372
-
373
- ## Documentation
374
-
375
- - [docs](https://github.com/bybatkhuu/module.fastapi-logging/blob/main/docs/README.md)
376
- - [scripts](https://github.com/bybatkhuu/module.fastapi-logging/blob/main/docs/scripts/README.md)
377
-
378
- ---
379
-
380
- ## References
381
-
382
- - <https://github.com/bybatkhuu/module.python-logging>
383
- - <https://github.com/Delgan/loguru>
384
- - <https://loguru.readthedocs.io/en/stable/api/logger.html>
385
- - <https://loguru.readthedocs.io/en/stable/resources/recipes.html>
@@ -1,16 +0,0 @@
1
- beans_logging_fastapi/__init__.py,sha256=BRaF1AtJeVBvYtDyvx9YUBUe7Ez632UAwVW6rUQU72I,874
2
- beans_logging_fastapi/__version__.py,sha256=vaO-yG68LsdKNrjwKiIqxA34q000eeRpru3EyLGSxsY,47
3
- beans_logging_fastapi/_async_log.py,sha256=2J-LBW0IQRCtZOfcYHZSUSbzQipWF-MuWbXgd6tqP9Q,3334
4
- beans_logging_fastapi/_base.py,sha256=ChpSFJ0yXUUdgV9Y-2SyrjPRuGHdYCf5UvMDxnPRAGg,4186
5
- beans_logging_fastapi/_filters.py,sha256=SmluD4RDMk_wsRJVSpE5jp7AylotYJy2N6aw9AVWwFI,541
6
- beans_logging_fastapi/_formats.py,sha256=MdS5x5EV7BVWEjVstQCeQn8XUz3qW12ltQS8-rNrCKc,2166
7
- beans_logging_fastapi/_handlers.py,sha256=eEgQD7ho6dqLzUfoN7CNAq7hD-fd97oeo-HROb6x1xk,2794
8
- beans_logging_fastapi/_middlewares.py,sha256=h9-turKSGnK__4mLAId3Slno3avz7Hu9lNpdmoepjY0,9278
9
- tests/__init__.py,sha256=iwhKnzeBJLKxpRVjvzwiRE63_zNpIBfaKLITauVph-0,24
10
- tests/conftest.py,sha256=ycEL83-UMU-fcXQUZWTCNEPcBOZ38pzhoCPpXpCjIEM,319
11
- tests/test_beans_logging_fastapi.py,sha256=khxZzk-UZJU4BfM3KLytzKZATnsjsYts0UJqn1UB5cU,489
12
- beans_logging_fastapi-1.1.1.dist-info/LICENCE.txt,sha256=8jrXqC7FZbke39LPGo_mUFR81CkoUCP_vyefZjlQDOg,1074
13
- beans_logging_fastapi-1.1.1.dist-info/METADATA,sha256=_Jw2qKu3bDukUEkmJI8goSLgIsoalx5G4D7wqqWrfTE,12502
14
- beans_logging_fastapi-1.1.1.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
15
- beans_logging_fastapi-1.1.1.dist-info/top_level.txt,sha256=i97pAfk2NcT9DA7cPsBmF6_90GzNObQ9UJpZFuXdt1s,28
16
- beans_logging_fastapi-1.1.1.dist-info/RECORD,,
tests/__init__.py DELETED
@@ -1 +0,0 @@
1
- # -*- coding: utf-8 -*-
tests/conftest.py DELETED
@@ -1,16 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- import pytest
4
-
5
- from beans_logging import logger
6
-
7
-
8
- @pytest.fixture(scope="session", autouse=True)
9
- def setup_and_teardown():
10
- # Equivalent of setUp
11
- logger.info("Setting up...")
12
-
13
- yield # This is where the testing happens!
14
-
15
- # Equivalent of tearDown
16
- logger.info("Tearing down!")
@@ -1,24 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
- import pytest
4
-
5
- from beans_logging import Logger, LoggerConfigPM, LoggerLoader
6
- from beans_logging_fastapi import HttpAccessLogMiddleware
7
-
8
-
9
- # @pytest.fixture
10
- # def my_base():
11
- # _my_base = MyBase()
12
-
13
- # yield _my_base
14
-
15
- # del _my_base
16
-
17
-
18
- # def test_init(my_base):
19
- # logger.info("Testing initialization of 'MyBase'...")
20
-
21
- # assert isinstance(my_base, MyBase)
22
- # assert my_base.item == "item"
23
-
24
- # logger.info("Done: Initialization of 'MyBase'.\n")