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

feat: JSON logging for Gunicorn #3672

Merged
merged 6 commits into from
Mar 26, 2024
Merged

feat: JSON logging for Gunicorn #3672

merged 6 commits into from
Mar 26, 2024

Conversation

khvn26
Copy link
Member

@khvn26 khvn26 commented Mar 26, 2024

Thanks for submitting a PR! Please check the boxes below:

  • I have run pre-commit to check linting
  • I have added information to docs/ if required so people know about the feature!
  • I have filled in the "Changes" section below?
  • I have filled in the "How did you test this code" section below?
  • I have used a Conventional Commit title for this Pull Request

Changes

This PR does the following:

  • Move default Gunicorn settings to a gunicorn.conf.py file. This allows users to override Gunicorn settings using the GUNICORN_CMD_ARGS environment variable.
  • Add GunicornAccessLogJsonFormatter and GunicornJsonLogger to make Gunicorn respect the LOG_FORMAT setting.
  • Expose the ACCESS_LOG_FORMAT environment variable.
  • make serve now configures Gunicorn with gunicorn.conf.py.

Closes #3637.

How did you test this code?

Ran make serve locally with LOG_FORMAT, ACCESS_LOG_FORMAT and ACCESS_LOG_LOCATION set to various values.

@khvn26 khvn26 requested review from a team and gagantrivedi and removed request for a team March 26, 2024 11:26
Copy link

vercel bot commented Mar 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 26, 2024 1:02pm
flagsmith-frontend-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 26, 2024 1:02pm
flagsmith-frontend-staging ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 26, 2024 1:02pm

@github-actions github-actions bot added api Issue related to the REST API docs Documentation updates labels Mar 26, 2024
Copy link
Contributor

@zachaysan zachaysan left a comment

Choose a reason for hiding this comment

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

Overall looks good, just some minor comments.

"thread_name": record.threadName,
}

def format(self, record: logging.LogRecord) -> str:
Copy link
Contributor

Choose a reason for hiding this comment

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

The docstring of this function is now out of date.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, removed!

class GunicornJsonLogger(GunicornLogger):
def setup(self, cfg: Config) -> None:
super().setup(cfg)
if settings.LOG_FORMAT == "json":
Copy link
Contributor

Choose a reason for hiding this comment

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

If it's not set to JSON then the logger won't really be a JSON logger, right? It's weird to let it operate as a normal logger with the name of the class GunicornJsonLogger

Copy link
Member Author

Choose a reason for hiding this comment

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

Renamed to GunicornJsonCapableLogger.

Comment on lines 157 to 158
- `ACCESS_LOG_LOCATION`: The location to store web logs generated by Gunicorn if running as a Docker container. If not
set, no logs will be stored. If set to `-` the logs will be sent to `stdout`.
Copy link
Contributor

Choose a reason for hiding this comment

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

If not set, no logs will be stored.

I think if no logs will be stored then stdout will actually be used, which is what we should say here, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! Updated the docs.

@codecov-commenter
Copy link

codecov-commenter commented Mar 26, 2024

Codecov Report

Attention: Patch coverage is 66.66667% with 10 lines in your changes are missing coverage. Please review.

Project coverage is 95.84%. Comparing base (21cc2d3) to head (a562539).
Report is 5 commits behind head on main.

Files Patch % Lines
api/util/logging.py 60.00% 10 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3672      +/-   ##
==========================================
- Coverage   95.86%   95.84%   -0.02%     
==========================================
  Files        1099     1099              
  Lines       34422    34428       +6     
==========================================
- Hits        33000    32999       -1     
- Misses       1422     1429       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@khvn26 khvn26 deleted the feat/json-logging-for-gunicorn branch March 26, 2024 13:14
matthewelwell added a commit that referenced this pull request Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issue related to the REST API docs Documentation updates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make LOG_FORMAT work for access logs, allow custom access log format
3 participants