litestar icon indicating copy to clipboard operation
litestar copied to clipboard

Add logging middleware (411) draft

Open Swannbm opened this issue 1 year ago • 7 comments

PR Checklist

  • [x] Have you followed the guidelines in CONTRIBUTING.md?
  • [x] Have you got 100% test coverage on new code?
  • [x] Have you updated the prose documentation?
  • [x] Have you updated the reference documentation?

Hello,

This is a draft PR for #411 because I am unsure of what is required to satisfy the issue. It's actuall my my first PR on public repo and I've got a lot of questions.

  • I've chosen to configure logging at startup => is it a good idea ?
  • Logging configuration is in starlite.logging not in starlite.config => should we move it ?
  • I've chose to put Logging middleware in starlite.middlewares but I could have put it in starlite.logging => what would be the best ?
  • My test, although it cover my code at 100% is very small (but the also is the middleware) => do you have any idea of edge case that I should add ?
  • I log both incoming and outgoing event. Most web framework log only outgoing. What do you think should be the default ? Should I add specific configuration in LoggingConfig for activate incoming, outgoing, both ?
  • I log those informations : '{hostname}:{port} - {method} {path} {scheme}/{http_version} {result}' (outgoing example: testserver:8000 - GET /my/path HTTPS/1.1 200), do you want more or less informations ?
  • I've got the port from request.base_url.port, is it the good one (there also are ports in scope with server and client) ?

I need help on one failing test: test_request_body_logging_middleware in tests/middleware/test_middleware_handling.py. Initialize logging breaks capslog fixture. Anyone encouter this situation and knows how to make capslog working ?

Improvements:

  • In order to ease making custom LoggingMiddleware, I am thinking adding a get_logging_middleware() to LoggingConfig. Is it a good idea ? Should I do that in that PR or make another one ?

Thank you all for your help and recommandations.

Swann

Swannbm avatar Sep 06 '22 09:09 Swannbm

I'll try to take a look at this tonight if this is still an issue.

We probably also need to add tests using the picologging logger as well.

cofin avatar Sep 07 '22 23:09 cofin

@Swannbm please convert this PR to a draft (it should be on the right side pane)l.

Goldziher avatar Sep 08 '22 14:09 Goldziher

Converted this PR to a draft

cofin avatar Sep 09 '22 16:09 cofin

Here the minutes of my (quick) discussion with Goldziher:

  1. Copy SessionMiddleware to fetch the middleware from Config instance
  2. Do not init logging up front, let the user do the job
  3. Extend LoggingConfig with properties only for the middleware
  4. keep current file organization to avoid breaking change (LoggingConfig will stay in logging.init.py)

All this points are opened to discussion.

Swannbm avatar Sep 12 '22 14:09 Swannbm

Here the minutes of my (quick) discussion with Goldziher:

  1. Copy SessionMiddleware to fetch the middleware from Config instance
  2. Do not init logging up front, let the user do the job
  3. Extend LoggingConfig with properties only for the middleware
  4. keep current file organization to avoid breaking change (LoggingConfig will stay in logging.init.py)

All this points are opened to discussion.

Rebase on development 😉, the LoggingConfig was moved.

Goldziher avatar Sep 12 '22 14:09 Goldziher

I have made the change (and necessary revert). I havn't check test coverage yet, I will do it later, my free time is comming to an end for today. @Goldziher and @cofin (even @john-ingles ) could you have a code review please ?

Swannbm avatar Sep 12 '22 16:09 Swannbm

any updates?

Goldziher avatar Sep 17 '22 17:09 Goldziher