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

Update log format to include X-Forwarded-For #605

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

jonespm
Copy link
Member

@jonespm jonespm commented Jan 21, 2025

Fixes #359

@jonespm jonespm force-pushed the issue_359-2 branch 3 times, most recently from f3f5bb1 to 506cc97 Compare January 22, 2025 21:56
@jonespm jonespm marked this pull request as ready for review January 22, 2025 23:17
@jonespm
Copy link
Member Author

jonespm commented Jan 22, 2025

I think this is ready for review. Running this locally should give you logs for both access and server that are in a similar format.

If you run this
curl -H "X-Forwarded-For: 10.11.12.13" http://localhost:8003/
it would simulate sending the X-Forwarded-For header and should update the IP address in the logs to 10.11.12.13.

@jonespm
Copy link
Member Author

jonespm commented Jan 22, 2025

Deploying to prod will need to get the new secret FORWARDED_ALLOW_IPS as only some IP's are allowed to be forwarded.

@jonespm jonespm requested review from zqian and lsloan January 22, 2025 23:22
@zqian
Copy link
Member

zqian commented Jan 23, 2025

Is there any changes needed in the .env file? I got " Error: Got unexpected extra arguments (# Trust all IPs in development for testing)" and the app is not up running

@jonespm
Copy link
Member Author

jonespm commented Jan 23, 2025

No, it doesn't need any locally as * is in the docker-compose, it just looks like it's pulling the comments into the array, I'll move that 😁

docker-compose.yaml Outdated Show resolved Hide resolved
record.request = f"{method} {path} HTTP/{http_version}"
record.status = status
else:
# Regular Python log
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether the regular Python log can be of any usage. Currently I only see patterns of:

web-1 | 2025-01-23 14:03:29,760 [DEBUG] [-] "% received keepalive pong" 760ms
web-1 | 2025-01-23 14:03:49,764 [DEBUG] [-] "% sending keepalive ping" 764ms
web-1 | 2025-01-23 14:03:49,764 [DEBUG] [-] "> %s" 764ms
web-1 | 2025-01-23 14:03:49,766 [DEBUG] [-] "< %s" 766ms
web-1 | 2025-01-23 14:02:29,752 [INFO] [-] "connection open" 752ms
web-1 | 2025-01-23 14:02:29,752 [DEBUG] [-] "= connection is OPEN" 752ms

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this patch current format looks like

[2025-01-23 19:37:09 -0500] [12] [INFO] ('1.2.3.4', 53268) - "WebSocket /ws/users/59784/" [accepted]
[2025-01-23 19:37:09 -0500] [12] [INFO] connection open
[2025-01-23 19:37:18 -0500] [12] [INFO] connection closed

These noisy DEBUG messages here shouldn't be displayed if "DEBUG=False". You could test this.

So new consistent format in this patch for Python and access logs is
2025-01-23 14:02:29,752 [INFO] [-] "connection open" 752ms

The [-] would have an IP address if it was from the web with the number being like 200 or 404.

Which is similar but not identicalto the old format
[2025-01-23 19:37:09 -0500] [12] [INFO] connection open

Happy to make additional adjustments if we want.

Some changes -

  • The time looks like it was changed to remove the TZ information
  • The time it took to execute the request is attached now
  • The message is quoted
  • There is no process ID

Copy link
Member

@zqian zqian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a minor comment

@jonespm jonespm added the config change Involves changes that would require edits to configuration variables/files label Jan 27, 2025
Copy link
Member

@lsloan lsloan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like these changes. I'd like to suggest that before merging, make sure you're happy with the log format, too. If possible, make the information in the line as compact as possible if you want to leave more line space available for the actual log message. In the timestamps, removing TZ info helps. Maybe removing the milliseconds would help, too, if the server doesn't seem busy enough to warrant it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
config change Involves changes that would require edits to configuration variables/files
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update log format to include X-Forwarded-For to capture users IP in logs
3 participants