Skip to content

Commit

Permalink
Enable gunicorn logging for h11 impl
Browse files Browse the repository at this point in the history
  • Loading branch information
immerrr committed May 31, 2021
1 parent 2ef2cad commit 991d36e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
34 changes: 33 additions & 1 deletion uvicorn/protocols/http/h11_impl.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import asyncio
import http
import logging
import time
import traceback
from typing import Callable
from urllib.parse import unquote

import h11

from uvicorn.logging import GunicornSafeAtoms
from uvicorn.protocols.http.flow_control import (
CLOSE_HEADER,
HIGH_WATER_LIMIT,
Expand Down Expand Up @@ -169,6 +172,11 @@ def handle_events(self):
"raw_path": raw_path,
"query_string": query_string,
"headers": self.headers,
"extensions": {
"uvicorn_request_duration": {
"request_start_time": time.monotonic(),
}
},
}

for name, value in self.headers:
Expand Down Expand Up @@ -197,6 +205,7 @@ def handle_events(self):
logger=self.logger,
access_logger=self.access_logger,
access_log=self.access_log,
gunicorn_log=self.config.gunicorn_log,
default_headers=self.default_headers,
message_event=asyncio.Event(),
on_response=self.on_response_complete,
Expand Down Expand Up @@ -333,6 +342,7 @@ def __init__(
logger,
access_logger,
access_log,
gunicorn_log,
default_headers,
message_event,
on_response,
Expand All @@ -344,6 +354,7 @@ def __init__(
self.logger = logger
self.access_logger = access_logger
self.access_log = access_log
self.gunicorn_log = gunicorn_log
self.default_headers = default_headers
self.message_event = message_event
self.on_response = on_response
Expand All @@ -361,6 +372,12 @@ def __init__(
self.response_started = False
self.response_complete = False

# For logging
if self.gunicorn_log:
self.gunicorn_atoms = GunicornSafeAtoms(self.scope)
else:
self.gunicorn_atoms = None

# ASGI exception wrapper
async def run_asgi(self, app):
try:
Expand Down Expand Up @@ -413,6 +430,9 @@ async def send(self, message):
if self.disconnected:
return

if self.gunicorn_atoms is not None:
self.gunicorn_atoms.on_asgi_message(message)

if not self.response_started:
# Sending response status line and headers
if message_type != "http.response.start":
Expand All @@ -428,7 +448,7 @@ async def send(self, message):
if CLOSE_HEADER in self.scope["headers"] and CLOSE_HEADER not in headers:
headers = headers + [CLOSE_HEADER]

if self.access_log:
if self.access_log and self.gunicorn_log is None:
self.access_logger.info(
'%s - "%s %s HTTP/%s" %d',
get_client_addr(self.scope),
Expand Down Expand Up @@ -469,6 +489,18 @@ async def send(self, message):
self.message_event.set()
event = h11.EndOfMessage()
output = self.conn.send(event)

duration_extension = self.scope['extensions']['uvicorn_request_duration']
duration_extension["response_end_time"] = time.monotonic()
if self.gunicorn_log is not None:
try:
self.gunicorn_log.access_log.info(
self.gunicorn_log.cfg.access_log_format,
self.gunicorn_atoms,
)
except: # noqa
self.gunicorn_log.error(traceback.format_exc())

self.transport.write(output)

else:
Expand Down
13 changes: 6 additions & 7 deletions uvicorn/protocols/http/httptools_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,15 +429,15 @@ async def send_500_response(self):
async def send(self, message):
message_type = message["type"]

if self.gunicorn_atoms is not None:
self.gunicorn_atoms.on_asgi_message(message)

if self.flow.write_paused and not self.disconnected:
await self.flow.drain()

if self.disconnected:
return

if self.gunicorn_atoms is not None:
self.gunicorn_atoms.on_asgi_message(message)

if not self.response_started:
# Sending response status line and headers
if message_type != "http.response.start":
Expand All @@ -453,7 +453,7 @@ async def send(self, message):
if CLOSE_HEADER in self.scope["headers"] and CLOSE_HEADER not in headers:
headers = headers + [CLOSE_HEADER]

if self.access_log and not self.gunicorn_log:
if self.access_log and self.gunicorn_log is None:
self.access_logger.info(
'%s - "%s %s HTTP/%s" %d',
get_client_addr(self.scope),
Expand Down Expand Up @@ -531,14 +531,13 @@ async def send(self, message):
duration_extension = self.scope['extensions']['uvicorn_request_duration']
duration_extension['response_end_time'] = time.monotonic()

if self.gunicorn_log:
if self.gunicorn_log is not None:
try:
self.gunicorn_log.access_log.info(
self.gunicorn_log.cfg.access_log_format,
self.gunicorn_atoms,
)
except:
import traceback
except: # noqa
self.gunicorn_log.error(traceback.format_exc())

self.message_event.set()
Expand Down

0 comments on commit 991d36e

Please sign in to comment.