mirror of
https://github.com/MatthewScholefield/uvicorn-loguru-integration.git
synced 2023-07-20 23:10:15 +03:00
Initial commit
This commit is contained in:
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
*.pyc
|
||||
*.egg-info/
|
||||
/*venv/
|
||||
/dist/
|
||||
.idea/
|
||||
.vscode/
|
||||
|
||||
*~
|
||||
7
LICENSE
Normal file
7
LICENSE
Normal file
@@ -0,0 +1,7 @@
|
||||
Copyright 2021 Matthew D. Scholefield
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
43
README.md
Normal file
43
README.md
Normal file
@@ -0,0 +1,43 @@
|
||||
# Uvicorn Loguru Integration
|
||||
|
||||
*Code to integrate
|
||||
[uvicorn.run](https://github.com/encode/uvicorn/blob/master/uvicorn/main.py#L365) with
|
||||
[Loguru](https://github.com/Delgan/loguru) logging*
|
||||
|
||||
[Loguru](https://github.com/Delgan/loguru) is a great alternative logging library for
|
||||
Python. However, since [Uvicorn](https://www.uvicorn.org/) uses Python's standard
|
||||
logging library, using Loguru looks inconsistent. This module injects an intercept
|
||||
handler in the correct location after initializing Uvicorn so that all logs get routed
|
||||
through Loguru.
|
||||
|
||||
## Usage
|
||||
|
||||
Call `run_uvicorn_loguru` with an instance of `uvicorn.Config`:
|
||||
|
||||
```python
|
||||
from uvicorn_loguru_integration import run_uvicorn_loguru
|
||||
|
||||
|
||||
def main():
|
||||
run_uvicorn_loguru(
|
||||
uvicorn.Config(
|
||||
"myapp:app",
|
||||
host="0.0.0.0",
|
||||
port=8000,
|
||||
log_level="info",
|
||||
reload=config.debug,
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
Install via `pip`:
|
||||
|
||||
```bash
|
||||
pip3 install uvicorn-loguru-integration
|
||||
```
|
||||
25
setup.py
Normal file
25
setup.py
Normal file
@@ -0,0 +1,25 @@
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name='uvicorn-loguru-integration',
|
||||
version='0.1.0',
|
||||
description='Code to integrate uvicorn.run with Loguru logging',
|
||||
url='https://github.com/MatthewScholefield/uvicorn-loguru-integration',
|
||||
author='Matthew D. Scholefield',
|
||||
author_email='matthew331199@gmail.com',
|
||||
classifiers=[
|
||||
'Development Status :: 3 - Alpha',
|
||||
|
||||
'Intended Audience :: Developers',
|
||||
'Programming Language :: Python :: 3',
|
||||
'Programming Language :: Python :: 3.4',
|
||||
'Programming Language :: Python :: 3.5',
|
||||
'Programming Language :: Python :: 3.6',
|
||||
],
|
||||
keywords='uvicorn loguru integration',
|
||||
py_modules=['uvicorn_loguru_integration'],
|
||||
install_requires=[
|
||||
'uvicorn',
|
||||
'loguru-logging-intercept'
|
||||
],
|
||||
)
|
||||
24
uvicorn_loguru_integration.py
Normal file
24
uvicorn_loguru_integration.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import uvicorn
|
||||
from loguru_logging_intercept import setup_loguru_logging_intercept
|
||||
import logging
|
||||
from uvicorn.supervisors import Multiprocess, ChangeReload
|
||||
|
||||
|
||||
def run_uvicorn_loguru(config: uvicorn.Config):
|
||||
"""Same as uvicorn.run but injects loguru logging"""
|
||||
server = uvicorn.Server(config=config)
|
||||
setup_loguru_logging_intercept(
|
||||
level=logging.getLevelName(config.log_level.upper()),
|
||||
modules=("uvicorn.error", "uvicorn.asgi", "uvicorn.access")
|
||||
)
|
||||
supervisor_type = None
|
||||
if config.should_reload:
|
||||
supervisor_type = ChangeReload
|
||||
if config.workers > 1:
|
||||
supervisor_type = Multiprocess
|
||||
if supervisor_type:
|
||||
sock = config.bind_socket()
|
||||
supervisor = supervisor_type(config, target=server.run, sockets=[sock])
|
||||
supervisor.run()
|
||||
else:
|
||||
server.run()
|
||||
Reference in New Issue
Block a user