-
-
Notifications
You must be signed in to change notification settings - Fork 277
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
Small: Log requests body #913
base: main
Are you sure you want to change the base?
Conversation
89a9d37
to
637f07a
Compare
637f07a
to
6a37eef
Compare
app/server/lib/FlexServer.ts
Outdated
method: tokens.method(req, res), | ||
path: tokens.gristInfo(req, res), | ||
body: shouldLogBody ? tokens.body(req, res) : {}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I don't know if this is intentional, but it looks to me like this will log { … path: "/whatever", body: {}, status: 200, … }
when body logging is disabled, and { … path: "/whatever", body: "body-content", status: 200, … }
when enabled. Having body
switch from being a string to an (empty) object doesn't seem correct.
My earlier suggestion was to use object spreading to make body
go away entirely when not enabled:
body: shouldLogBody ? tokens.body(req, res) : {}, | |
...(shouldLogBody ? { body: tokens.body(req, res) } : {}), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That being said, while it should work when logging in JSON, it still prints {}
when logging in plain text:
2024-03-28 18:20:45.193 localhost:8484 GET /o/docs/api/orgs/0/workspaces?includeSupport=1 {} 304 39.934 ms - -
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Err, this is quite weird, how would a change to the JSON logging impact the plain text logger?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't, it seems like the JSON and the plain text logger are different things.
But whereas what you suggested removes the body when we enable logging in JSON, it still prints {}
when the plain text logging is enabled.
Unless we find a way to skip the body here:
https://github.com/gristlabs/grist-core/pull/913/files#diff-cc10ec0f715f5f40f94fcf195753f0052aede521998746b91689ce96b9a30055R420-R421
If I am correct, Nginx in this case prints a -
when no values exist, maybe we could do the same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's what I'm observing when running your branch (with GRIST_LOG_SKIP_HTTP=false
and GRIST_HOSTED_VERSION
unset in all cases), when doing a POST
to /
with body {"x":1}
:
- with
GRIST_LOG_HTTP_BODY=false
:
localhost:8484 POST / 404 3.977 ms - 2347
No {}
here, but note that there's a double space here between /
and 404
, which you could get rid of by tweaking the template to include the space before :body
inside the conditional.
- with
GRIST_LOG_HTTP_BODY=true GRIST_HOSTED_VERSION=
:
localhost:8484 POST / {"x":"1"} 404 12.982 ms - 2347
The body is correctly printed here. However if I do a request without a body, e.g. GET /
this is printed:
localhost:8484 GET / {} 302 8.375 ms - 51
In fact, if I do a POST with a non-JSON request body I also get {}
printed. And "body": "{}"
if I enable JSON logging with GRIST_HOSTED_VERSION=true
(btw that variable might also benefit from consistent application of isAffirmative
— it is referenced three times through the code, but only once with isAffirmative
).
Maybe JSON.stringify(req.body)
is not the best way to get at the body for logging? Or maybe it's fine since there's not much point in logging non-JSON bodies?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I have come up with a solution for your remark here, what do you think?
9035859
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw that variable might also benefit from consistent application of isAffirmative — it is referenced three times through the code, but only once with isAffirmative
Also done in 0e2af49
Co-authored-by: Jonathan Perret <[email protected]>
9c37cd0
to
3935de0
Compare
This PR brings 2 things to improve a little bit the debugging:
GRIST_LOG_SKIP_HTTP
is read as a boolean (it defaults totrue
in the main entrypoint, which seems safe);GRIST_LOG_HTTP_BODY
, which logs the content of the request bodies, I do not document this variable on purpose because of its potential security like logging passwords or api keys (though we may find a way to redact these values, I propose to iterate);Here is how it is rendered with both
GRIST_LOG_SKIP_HTTP=false
andGRIST_LOG_HTTP_BODY=true
:And how it is rendered with just
GRIST_LOG_SKIP_HTTP=false
: