Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass request details as log record extra fields #2321

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions uvicorn/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,16 @@ def default(code: int) -> str:
return status_and_phrase

def formatMessage(self, record: logging.LogRecord) -> str:
recordcopy = copy(record)
(
client_addr,
method,
full_path,
http_version,
status_code,
) = recordcopy.args # type: ignore[misc]
status_code = self.get_status_code(int(status_code)) # type: ignore[arg-type]
status_code = self.get_status_code(int(record.__dict__["status"]))
method = record.__dict__["method"]
full_path = record.__dict__["full_path"]
http_version = record.__dict__["http_version"]
request_line = f"{method} {full_path} HTTP/{http_version}"
if self.use_colors:
request_line = click.style(request_line, bold=True)
recordcopy = copy(record)
recordcopy.__dict__.update(
{
"client_addr": client_addr,
"request_line": request_line,
"status_code": status_code,
}
Expand Down
20 changes: 16 additions & 4 deletions uvicorn/protocols/http/h11_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,13 +463,25 @@ async def send(self, message: ASGISendEvent) -> None:
headers = headers + [CLOSE_HEADER]

if self.access_log:
client_addr = get_client_addr(self.scope)
method = self.scope["method"]
http_version = self.scope["http_version"]
full_path = get_path_with_query_string(self.scope)

self.access_logger.info(
'%s - "%s %s HTTP/%s" %d',
get_client_addr(self.scope),
self.scope["method"],
get_path_with_query_string(self.scope),
self.scope["http_version"],
client_addr,
method,
full_path,
http_version,
status,
extra={
"client_addr": client_addr,
"method": method,
"full_path": full_path,
"http_version": http_version,
"status": status,
},
)

# Write response status line and headers
Expand Down
28 changes: 20 additions & 8 deletions uvicorn/protocols/http/httptools_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,24 +456,36 @@ async def send(self, message: ASGISendEvent) -> None:
self.response_started = True
self.waiting_for_100_continue = False

status_code = message["status"]
status = message["status"]
headers = self.default_headers + list(message.get("headers", []))

if CLOSE_HEADER in self.scope["headers"] and CLOSE_HEADER not in headers:
headers = headers + [CLOSE_HEADER]

if self.access_log:
client_addr = get_client_addr(self.scope)
method = self.scope["method"]
http_version = self.scope["http_version"]
full_path = get_path_with_query_string(self.scope)

self.access_logger.info(
'%s - "%s %s HTTP/%s" %d',
get_client_addr(self.scope),
self.scope["method"],
get_path_with_query_string(self.scope),
self.scope["http_version"],
status_code,
client_addr,
method,
full_path,
http_version,
status,
extra={
"client_addr": client_addr,
"method": method,
"full_path": full_path,
"http_version": http_version,
"status": status,
},
)

# Write response status line and headers
content = [STATUS_LINE[status_code]]
content = [STATUS_LINE[status]]

for name, value in headers:
if HEADER_RE.search(name):
Expand All @@ -492,7 +504,7 @@ async def send(self, message: ASGISendEvent) -> None:
self.keep_alive = False
content.extend([name, b": ", value, b"\r\n"])

if self.chunked_encoding is None and self.scope["method"] != "HEAD" and status_code not in (204, 304):
if self.chunked_encoding is None and self.scope["method"] != "HEAD" and status not in (204, 304):
# Neither content-length nor transfer-encoding specified
self.chunked_encoding = True
content.append(b"transfer-encoding: chunked\r\n")
Expand Down
Loading