log only sdapi

This commit is contained in:
Vladimir Mandic 2023-01-03 10:58:52 -05:00 committed by GitHub
parent aaa4c2aacb
commit cec209981e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -68,22 +68,23 @@ def encode_pil_to_base64(image):
bytes_data = output_bytes.getvalue() bytes_data = output_bytes.getvalue()
return base64.b64encode(bytes_data) return base64.b64encode(bytes_data)
def init_api_middleware(app: FastAPI): def api_middleware(app: FastAPI):
@app.middleware("http") @app.middleware("http")
async def log_and_time(req: Request, call_next): async def log_and_time(req: Request, call_next):
ts = time.time() ts = time.time()
res: Response = await call_next(req) res: Response = await call_next(req)
duration = str(round(time.time() - ts, 4)) duration = str(round(time.time() - ts, 4))
res.headers["X-Process-Time"] = duration res.headers["X-Process-Time"] = duration
if shared.cmd_opts.api_log: endpoint = req.scope.get('path', 'err')
print('API {t} {code} {prot}/{ver} {method} {p} {cli} {duration}'.format( if shared.cmd_opts.api_log and endpoint.startswith('/sdapi'):
print('API {t} {code} {prot}/{ver} {method} {endpoint} {cli} {duration}'.format(
t = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"), t = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"),
code = res.status_code, code = res.status_code,
ver = req.scope.get('http_version', '0.0'), ver = req.scope.get('http_version', '0.0'),
cli = req.scope.get('client', ('0:0.0.0', 0))[0], cli = req.scope.get('client', ('0:0.0.0', 0))[0],
prot = req.scope.get('scheme', 'err'), prot = req.scope.get('scheme', 'err'),
method = req.scope.get('method', 'err'), method = req.scope.get('method', 'err'),
p = req.scope.get('path', 'err'), endpoint = endpoint,
duration = duration, duration = duration,
)) ))
return res return res