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

Ability to log the HTTP referer #773

Closed
simonw opened this issue Sep 2, 2020 · 2 comments · May be fixed by #947
Closed

Ability to log the HTTP referer #773

simonw opened this issue Sep 2, 2020 · 2 comments · May be fixed by #947

Comments

@simonw
Copy link
Contributor

simonw commented Sep 2, 2020

Unless I've missed something, it looks like it isn't currently possible to configure Uvicorn to output the HTTP Referer header in the access logs:

uvicorn/uvicorn/logging.py

Lines 109 to 132 in e77e596

def formatMessage(self, record):
recordcopy = copy(record)
scope = recordcopy.__dict__["scope"]
method = scope["method"]
path = self.get_path(scope)
full_path = self.get_full_path(scope)
client_addr = self.get_client_addr(scope)
status_code = self.get_status_code(recordcopy)
http_version = scope["http_version"]
request_line = "%s %s HTTP/%s" % (method, full_path, http_version)
if self.use_colors:
request_line = click.style(request_line, bold=True)
recordcopy.__dict__.update(
{
"method": method,
"path": path,
"full_path": full_path,
"client_addr": client_addr,
"request_line": request_line,
"status_code": status_code,
"http_version": http_version,
}
)
return super().formatMessage(recordcopy)

This would be really useful for me because I'm trying to figure out where traffic to my sites comes from without running Google Analytics!

@euri10
Copy link
Member

euri10 commented Sep 3, 2020

you're correct, it should be in the socpe["headers"] if I'm not mistaken so that sounds feasible

@Kludex
Copy link
Sponsor Member

Kludex commented May 15, 2022

I recommend using a middleware to create the access logs, and disable the uvicorn.access logger.

This project can be used as inspiration: https://github.com/Kludex/asgi-logger.

PR is welcome to improve the documentation here. About the access logger formatter discussion, we'll continue on #527.

@Kludex Kludex closed this as completed May 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants