Spaces:
Sleeping
Sleeping
alessandro trinca tornidor
commited on
Commit
·
9c07bc7
1
Parent(s):
c7d60e1
refactor: rename logger
Browse files
app.py
CHANGED
@@ -7,24 +7,22 @@ import structlog
|
|
7 |
import uvicorn
|
8 |
from asgi_correlation_id import CorrelationIdMiddleware
|
9 |
from asgi_correlation_id.context import correlation_id
|
10 |
-
from
|
|
|
11 |
from uvicorn.protocols.utils import get_path_with_query_string
|
12 |
|
13 |
-
from helpers.structlog_setup import setup_logging
|
14 |
from helpers import formatters
|
15 |
-
from
|
16 |
-
|
17 |
from routes import router
|
18 |
|
|
|
19 |
load_dotenv()
|
20 |
|
21 |
# LOG_JSON_FORMAT = parse_obj_as(bool, os.getenv("LOG_JSON_FORMAT", False))
|
22 |
LOG_JSON_FORMAT = bool(os.getenv("LOG_JSON_FORMAT", False))
|
23 |
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
|
24 |
setup_logging(json_logs=LOG_JSON_FORMAT, log_level=LOG_LEVEL)
|
25 |
-
|
26 |
-
access_logger = structlog.stdlib.get_logger(__name__)
|
27 |
-
|
28 |
app = FastAPI(title="Example API", version="1.0.0")
|
29 |
|
30 |
|
@@ -55,7 +53,7 @@ async def logging_middleware(request: Request, call_next) -> Response:
|
|
55 |
http_method = request.method
|
56 |
http_version = request.scope["http_version"]
|
57 |
# Recreate the Uvicorn access log format, but add all parameters as structured information
|
58 |
-
|
59 |
f"""{client_host}:{client_port} - "{http_method} {url} HTTP/{http_version}" {status_code}""",
|
60 |
http={
|
61 |
"url": str(request.url),
|
@@ -72,13 +70,13 @@ async def logging_middleware(request: Request, call_next) -> Response:
|
|
72 |
|
73 |
|
74 |
app.include_router(router)
|
75 |
-
|
76 |
CUSTOM_GRADIO_PATH = "/"
|
77 |
|
78 |
|
79 |
def get_gradio_app():
|
80 |
with gr.Blocks() as gradio_app:
|
81 |
-
|
82 |
gr.Markdown(
|
83 |
"""
|
84 |
# Hello World!
|
@@ -104,11 +102,11 @@ def get_gradio_app():
|
|
104 |
return gradio_app
|
105 |
|
106 |
|
107 |
-
|
108 |
gradio_app_md = get_gradio_app()
|
109 |
app.add_middleware(CorrelationIdMiddleware)
|
110 |
app = gr.mount_gradio_app(app, gradio_app_md, path=CUSTOM_GRADIO_PATH)
|
111 |
-
|
112 |
|
113 |
if __name__ == "__main__":
|
114 |
try:
|
|
|
7 |
import uvicorn
|
8 |
from asgi_correlation_id import CorrelationIdMiddleware
|
9 |
from asgi_correlation_id.context import correlation_id
|
10 |
+
from dotenv import load_dotenv
|
11 |
+
from fastapi import FastAPI, Request, Response
|
12 |
from uvicorn.protocols.utils import get_path_with_query_string
|
13 |
|
|
|
14 |
from helpers import formatters
|
15 |
+
from helpers.structlog_setup import setup_logging
|
|
|
16 |
from routes import router
|
17 |
|
18 |
+
|
19 |
load_dotenv()
|
20 |
|
21 |
# LOG_JSON_FORMAT = parse_obj_as(bool, os.getenv("LOG_JSON_FORMAT", False))
|
22 |
LOG_JSON_FORMAT = bool(os.getenv("LOG_JSON_FORMAT", False))
|
23 |
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
|
24 |
setup_logging(json_logs=LOG_JSON_FORMAT, log_level=LOG_LEVEL)
|
25 |
+
logger = structlog.stdlib.get_logger(__name__)
|
|
|
|
|
26 |
app = FastAPI(title="Example API", version="1.0.0")
|
27 |
|
28 |
|
|
|
53 |
http_method = request.method
|
54 |
http_version = request.scope["http_version"]
|
55 |
# Recreate the Uvicorn access log format, but add all parameters as structured information
|
56 |
+
logger.info(
|
57 |
f"""{client_host}:{client_port} - "{http_method} {url} HTTP/{http_version}" {status_code}""",
|
58 |
http={
|
59 |
"url": str(request.url),
|
|
|
70 |
|
71 |
|
72 |
app.include_router(router)
|
73 |
+
logger.info("routes included, creating gradio app")
|
74 |
CUSTOM_GRADIO_PATH = "/"
|
75 |
|
76 |
|
77 |
def get_gradio_app():
|
78 |
with gr.Blocks() as gradio_app:
|
79 |
+
logger.info("start gradio app building...")
|
80 |
gr.Markdown(
|
81 |
"""
|
82 |
# Hello World!
|
|
|
102 |
return gradio_app
|
103 |
|
104 |
|
105 |
+
logger.info("mounting gradio app within FastAPI...")
|
106 |
gradio_app_md = get_gradio_app()
|
107 |
app.add_middleware(CorrelationIdMiddleware)
|
108 |
app = gr.mount_gradio_app(app, gradio_app_md, path=CUSTOM_GRADIO_PATH)
|
109 |
+
logger.info("gradio app mounted")
|
110 |
|
111 |
if __name__ == "__main__":
|
112 |
try:
|