beans-logging-fastapi 1.0.1__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Batkhuu Byambajav
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,380 @@
1
+ Metadata-Version: 2.1
2
+ Name: beans_logging_fastapi
3
+ Version: 1.0.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.0.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
+
31
+ `beans_logging_fastapi` is a middleware for FastAPI to log HTTP access.
32
+
33
+ It is based on **'beans-logging'** package.
34
+
35
+ ## Features
36
+
37
+ - **Logger** based on **'beans-logging'** package
38
+ - **FastAPI** HTTP access logging **middleware**
39
+
40
+ ---
41
+
42
+ ## Installation
43
+
44
+ ### 1. Prerequisites
45
+
46
+ - **Python (>= v3.8)**
47
+ - **PyPi (>= v23)**
48
+
49
+ ### 2. Install beans-logging-fastapi package
50
+
51
+ Choose one of the following methods to install the package **[A ~ F]**:
52
+
53
+ **A.** [**RECOMMENDED**] Install from **PyPi**
54
+
55
+ ```sh
56
+ # Install or upgrade package:
57
+ pip install -U beans-logging-fastapi
58
+ ```
59
+
60
+ **B.** Install latest version from **GitHub**
61
+
62
+ ```sh
63
+ # Install package by git:
64
+ pip install git+https://github.com/bybatkhuu/module.fastapi-logging.git
65
+ ```
66
+
67
+ **C.** Install from **pre-built release** files
68
+
69
+ 1. Download **`.whl`** or **`.tar.gz`** file from **releases** - <https://github.com/bybatkhuu/module.fastapi-logging/releases>
70
+ 2. Install with pip:
71
+
72
+ ```sh
73
+ # Install from .whl file:
74
+ pip install ./beans_logging_fastapi-[VERSION]-py3-none-any.whl
75
+ # Or install from .tar.gz file:
76
+ pip install ./beans_logging_fastapi-[VERSION].tar.gz
77
+ ```
78
+
79
+ **D.** Install from **source code** by building package
80
+
81
+ ```sh
82
+ # Clone repository by git:
83
+ git clone https://github.com/bybatkhuu/module.fastapi-logging.git beans_logging_fastapi
84
+ cd ./beans_logging_fastapi
85
+
86
+ # Install python build tool:
87
+ pip install -U pip build
88
+
89
+ # Build python package:
90
+ python -m build
91
+
92
+ _VERSION=$(./scripts/get-version.sh)
93
+
94
+ # Install from .whl file:
95
+ pip install ./dist/beans_logging_fastapi-${_VERSION}-py3-none-any.whl
96
+ # Or install from .tar.gz file:
97
+ pip install ./dist/beans_logging_fastapi-${_VERSION}.tar.gz
98
+ ```
99
+
100
+ **E.** Install with pip editable **development mode** (from source code)
101
+
102
+ ```sh
103
+ # Clone repository by git:
104
+ git clone https://github.com/bybatkhuu/module.fastapi-logging.git beans_logging_fastapi
105
+ cd ./beans_logging_fastapi
106
+
107
+ # Install with editable development mode:
108
+ pip install -e .
109
+ ```
110
+
111
+ **F.** Manually add to **PYTHONPATH** (not recommended)
112
+
113
+ ```sh
114
+ # Clone repository by git:
115
+ git clone https://github.com/bybatkhuu/module.fastapi-logging.git beans_logging_fastapi
116
+ cd ./beans_logging_fastapi
117
+
118
+ # Install python dependencies:
119
+ pip install -r ./requirements.txt
120
+
121
+ # Add current path to PYTHONPATH:
122
+ export PYTHONPATH="${PWD}:${PYTHONPATH}"
123
+ ```
124
+
125
+ ## Usage/Examples
126
+
127
+ To use `beans_logging_fastapi`:
128
+
129
+ ### **FastAPI**
130
+
131
+ [**`configs/logger.yml`**](https://github.com/bybatkhuu/module.fastapi-logging/blob/main/examples/configs/logger.yml):
132
+
133
+ ```yaml
134
+ logger:
135
+ app_name: "fastapi-app"
136
+ level: "TRACE"
137
+ use_diagnose: false
138
+ stream:
139
+ use_color: true
140
+ use_icon: false
141
+ 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>"
142
+ std_handler:
143
+ enabled: true
144
+ file:
145
+ logs_dir: "./logs"
146
+ rotate_size: 10000000 # 10MB
147
+ rotate_time: "00:00:00"
148
+ backup_count: 90
149
+ log_handlers:
150
+ enabled: true
151
+ format_str: "[{time:YYYY-MM-DD HH:mm:ss.SSS Z} | {level_short:<5} | {name}:{line}]: {message}"
152
+ log_path: "{app_name}.std.all.log"
153
+ err_path: "{app_name}.std.err.log"
154
+ json_handlers:
155
+ enabled: true
156
+ use_custom: false
157
+ log_path: "json/{app_name}.json.all.log"
158
+ err_path: "json/{app_name}.json.err.log"
159
+ intercept:
160
+ auto_load:
161
+ enabled: true
162
+ only_base: false
163
+ ignore_modules: []
164
+ include_modules: []
165
+ mute_modules: ["uvicorn.access"]
166
+ extra:
167
+ http_std_debug_format: '<n>[{request_id}]</n> {client_host} {user_id} "<u>{method} {url_path}</u> HTTP/{http_version}"'
168
+ 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'
169
+ http_file_enabled: true
170
+ 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}'
171
+ http_log_path: "http/{app_name}.http.access.log"
172
+ http_err_path: "http/{app_name}.http.err.log"
173
+ http_json_enabled: true
174
+ http_json_path: "json.http/{app_name}.json.http.access.log"
175
+ http_json_err_path: "json.http/{app_name}.json.http.err.log"
176
+ ```
177
+
178
+ [**`.env`**](https://github.com/bybatkhuu/module.fastapi-logging/blob/main/examples/.env):
179
+
180
+ ```sh
181
+ ENV=development
182
+ DEBUG=true
183
+
184
+ BEANS_LOGGING_DISABLE_DEFAULT=false
185
+ BEANS_LOGGING_CONFIG_PATH="./configs/logger.yml"
186
+ BEANS_LOGGING_LOGS_DIR="./logs"
187
+ ```
188
+
189
+ [**`logger.py`**](https://github.com/bybatkhuu/module.fastapi-logging/blob/main/examples/logger.py):
190
+
191
+ ```python
192
+ from beans_logging import Logger, LoggerLoader
193
+ from beans_logging_fastapi import (
194
+ add_http_file_handler,
195
+ add_http_file_json_handler,
196
+ http_file_format,
197
+ )
198
+
199
+ logger_loader = LoggerLoader()
200
+ logger: Logger = logger_loader.load()
201
+
202
+
203
+ def _http_file_format(record: dict) -> str:
204
+ _format = http_file_format(
205
+ record=record, msg_format=logger_loader.config.extra.http_file_format
206
+ )
207
+ return _format
208
+
209
+
210
+ if logger_loader.config.extra.http_file_enabled:
211
+ add_http_file_handler(
212
+ logger_loader=logger_loader,
213
+ log_path=logger_loader.config.extra.http_log_path,
214
+ err_path=logger_loader.config.extra.http_err_path,
215
+ formatter=_http_file_format,
216
+ )
217
+
218
+ if logger_loader.config.extra.http_json_enabled:
219
+ add_http_file_json_handler(
220
+ logger_loader=logger_loader,
221
+ log_path=logger_loader.config.extra.http_json_path,
222
+ err_path=logger_loader.config.extra.http_json_err_path,
223
+ )
224
+ ```
225
+
226
+ [**`main.py`**](https://github.com/bybatkhuu/module.fastapi-logging/blob/main/examples/main.py):
227
+
228
+ ```python
229
+ from typing import Union
230
+ from contextlib import asynccontextmanager
231
+
232
+ import uvicorn
233
+ from dotenv import load_dotenv
234
+ from fastapi import FastAPI, HTTPException
235
+ from fastapi.responses import RedirectResponse
236
+
237
+ load_dotenv()
238
+
239
+ from beans_logging_fastapi import (
240
+ HttpAccessLogMiddleware,
241
+ RequestHTTPInfoMiddleware,
242
+ ResponseHTTPInfoMiddleware,
243
+ )
244
+
245
+ from logger import logger, logger_loader
246
+ from __version__ import __version__
247
+
248
+
249
+ @asynccontextmanager
250
+ async def lifespan(app: FastAPI):
251
+ logger.info("Preparing to startup...")
252
+ logger.success("Finished preparation to startup.")
253
+ logger.info(f"API version: {__version__}")
254
+
255
+ yield
256
+ logger.info("Praparing to shutdown...")
257
+ logger.success("Finished preparation to shutdown.")
258
+
259
+
260
+ app = FastAPI(lifespan=lifespan, version=__version__)
261
+
262
+ app.add_middleware(ResponseHTTPInfoMiddleware)
263
+ app.add_middleware(
264
+ HttpAccessLogMiddleware,
265
+ debug_format=logger_loader.config.extra.http_std_debug_format,
266
+ msg_format=logger_loader.config.extra.http_std_msg_format,
267
+ )
268
+ app.add_middleware(
269
+ RequestHTTPInfoMiddleware, has_proxy_headers=True, has_cf_headers=True
270
+ )
271
+
272
+
273
+ @app.get("/")
274
+ def root():
275
+ return {"Hello": "World"}
276
+
277
+
278
+ @app.get("/items/{item_id}")
279
+ def read_item(item_id: int, q: Union[str, None] = None):
280
+ return {"item_id": item_id, "q": q}
281
+
282
+
283
+ @app.get("/continue", status_code=100)
284
+ def get_continue():
285
+ return {}
286
+
287
+
288
+ @app.get("/redirect")
289
+ def redirect():
290
+ return RedirectResponse("/")
291
+
292
+
293
+ @app.get("/error")
294
+ def error():
295
+ raise HTTPException(status_code=500)
296
+
297
+
298
+ if __name__ == "__main__":
299
+ uvicorn.run(
300
+ app="main:app",
301
+ host="0.0.0.0",
302
+ port=8000,
303
+ access_log=False,
304
+ server_header=False,
305
+ proxy_headers=True,
306
+ forwarded_allow_ips="*",
307
+ )
308
+ ```
309
+
310
+ Run the [**`examples`**](https://github.com/bybatkhuu/module.fastapi-logging/tree/main/examples):
311
+
312
+ ```sh
313
+ cd ./examples
314
+ # Install python dependencies for examples:
315
+ pip install -r ./requirements.txt
316
+
317
+ uvicorn main:app --host=0.0.0.0 --port=8000
318
+ ```
319
+
320
+ **Output**:
321
+
322
+ ```txt
323
+ [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'];
324
+ [2023-10-31 12:38:46.749 +09:00 | INFO | uvicorn.server:76]: Started server process [56216]
325
+ [2023-10-31 12:38:46.749 +09:00 | INFO | uvicorn.lifespan.on:46]: Waiting for application startup.
326
+ [2023-10-31 12:38:46.750 +09:00 | INFO | main:26]: Preparing to startup...
327
+ [2023-10-31 12:38:46.750 +09:00 | OK | main:27]: Finished preparation to startup.
328
+ [2023-10-31 12:38:46.750 +09:00 | INFO | main:28]: API version: 0.0.0-000000
329
+ [2023-10-31 12:38:46.750 +09:00 | INFO | uvicorn.lifespan.on:60]: Application startup complete.
330
+ [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)
331
+ [2023-10-31 12:38:48.701 +09:00 | DEBUG | anyio._backends._asyncio:807]: [c3d76377250947c29559cb103a6d2c56] 127.0.0.1 - "GET / HTTP/1.1"
332
+ [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
333
+ ^C[2023-10-31 12:38:49.793 +09:00 | INFO | uvicorn.server:264]: Shutting down
334
+ [2023-10-31 12:38:49.903 +09:00 | INFO | uvicorn.lifespan.on:65]: Waiting for application shutdown.
335
+ [2023-10-31 12:38:49.903 +09:00 | INFO | main:31]: Praparing to shutdown...
336
+ [2023-10-31 12:38:49.904 +09:00 | OK | main:32]: Finished preparation to shutdown.
337
+ [2023-10-31 12:38:49.904 +09:00 | INFO | uvicorn.lifespan.on:76]: Application shutdown complete.
338
+ [2023-10-31 12:38:49.904 +09:00 | INFO | uvicorn.server:86]: Finished server process [56216]
339
+ ```
340
+
341
+ ---
342
+
343
+ ## Running Tests
344
+
345
+ To run tests, run the following command:
346
+
347
+ ```sh
348
+ # Install python test dependencies:
349
+ pip install -r ./requirements.test.txt
350
+
351
+ # Run tests:
352
+ python -m pytest -sv
353
+ ```
354
+
355
+ ## Environment Variables
356
+
357
+ You can use the following environment variables inside [**`.env.example`**](https://github.com/bybatkhuu/module.fastapi-logging/blob/main/.env.example) file:
358
+
359
+ ```sh
360
+ ENV=development
361
+ DEBUG=true
362
+
363
+ BEANS_LOGGING_DISABLE_DEFAULT=false
364
+ BEANS_LOGGING_CONFIG_PATH="./configs/logger.yml"
365
+ BEANS_LOGGING_LOGS_DIR="./logs"
366
+ ```
367
+
368
+ ## Documentation
369
+
370
+ - [docs](https://github.com/bybatkhuu/module.fastapi-logging/blob/main/docs/README.md)
371
+ - [scripts](https://github.com/bybatkhuu/module.fastapi-logging/blob/main/docs/scripts/README.md)
372
+
373
+ ---
374
+
375
+ ## References
376
+
377
+ - <https://github.com/bybatkhuu/module.python-logging>
378
+ - <https://github.com/Delgan/loguru>
379
+ - <https://loguru.readthedocs.io/en/stable/api/logger.html>
380
+ - <https://loguru.readthedocs.io/en/stable/resources/recipes.html>