-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Log objects rather than JSON strings and option for single line logs #2028
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,11 @@ | |
// themselves use our routing information, without disturbing express | ||
// components that external developers may be modifying. | ||
|
||
import express from 'express'; | ||
import url from 'url'; | ||
import log from './logger'; | ||
import AppCache from './cache'; | ||
import express from 'express'; | ||
import url from 'url'; | ||
import log from './logger'; | ||
import {inspect} from 'util'; | ||
|
||
export default class PromiseRouter { | ||
// Each entry should be an object with: | ||
|
@@ -19,8 +21,9 @@ export default class PromiseRouter { | |
// status: optional. the http status code. defaults to 200 | ||
// response: a json object with the content of the response | ||
// location: optional. a location header | ||
constructor(routes = []) { | ||
constructor(routes = [], appId) { | ||
this.routes = routes; | ||
this.appId = appId; | ||
this.mountRoutes(); | ||
} | ||
|
||
|
@@ -107,16 +110,16 @@ export default class PromiseRouter { | |
for (var route of this.routes) { | ||
switch(route.method) { | ||
case 'POST': | ||
expressApp.post(route.path, makeExpressHandler(route.handler)); | ||
expressApp.post(route.path, makeExpressHandler(this.appId, route.handler)); | ||
break; | ||
case 'GET': | ||
expressApp.get(route.path, makeExpressHandler(route.handler)); | ||
expressApp.get(route.path, makeExpressHandler(this.appId, route.handler)); | ||
break; | ||
case 'PUT': | ||
expressApp.put(route.path, makeExpressHandler(route.handler)); | ||
expressApp.put(route.path, makeExpressHandler(this.appId, route.handler)); | ||
break; | ||
case 'DELETE': | ||
expressApp.delete(route.path, makeExpressHandler(route.handler)); | ||
expressApp.delete(route.path, makeExpressHandler(this.appId, route.handler)); | ||
break; | ||
default: | ||
throw 'unexpected code branch'; | ||
|
@@ -129,16 +132,16 @@ export default class PromiseRouter { | |
for (var route of this.routes) { | ||
switch(route.method) { | ||
case 'POST': | ||
expressApp.post(route.path, makeExpressHandler(route.handler)); | ||
expressApp.post(route.path, makeExpressHandler(this.appId, route.handler)); | ||
break; | ||
case 'GET': | ||
expressApp.get(route.path, makeExpressHandler(route.handler)); | ||
expressApp.get(route.path, makeExpressHandler(this.appId, route.handler)); | ||
break; | ||
case 'PUT': | ||
expressApp.put(route.path, makeExpressHandler(route.handler)); | ||
expressApp.put(route.path, makeExpressHandler(this.appId, route.handler)); | ||
break; | ||
case 'DELETE': | ||
expressApp.delete(route.path, makeExpressHandler(route.handler)); | ||
expressApp.delete(route.path, makeExpressHandler(this.appId, route.handler)); | ||
break; | ||
default: | ||
throw 'unexpected code branch'; | ||
|
@@ -152,17 +155,30 @@ export default class PromiseRouter { | |
// handler. | ||
// Express handlers should never throw; if a promise handler throws we | ||
// just treat it like it resolved to an error. | ||
function makeExpressHandler(promiseHandler) { | ||
function makeExpressHandler(appId, promiseHandler) { | ||
let config = AppCache.get(appId); | ||
return function(req, res, next) { | ||
try { | ||
log.verbose(req.method, maskSensitiveUrl(req), req.headers, | ||
JSON.stringify(maskSensitiveBody(req), null, 2)); | ||
let url = maskSensitiveUrl(req); | ||
let body = maskSensitiveBody(req); | ||
let stringifiedBody = JSON.stringify(body, null, 2); | ||
log.verbose(`REQUEST for [${req.method}] ${url}: ${stringifiedBody}`, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need to put the stringified body as we log it after? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup - so it will appear in parse dashboard logs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really want to stringify the entire body of the request and masking it, if the logging is not in verbose, can we guard it inside a It seems like this would add a fair bit of time for big requests/responses. |
||
method: req.method, | ||
url: url, | ||
headers: req.headers, | ||
body: body | ||
}); | ||
promiseHandler(req).then((result) => { | ||
if (!result.response && !result.location && !result.text) { | ||
log.error('the handler did not include a "response" or a "location" field'); | ||
throw 'control should not get here'; | ||
} | ||
log.verbose(JSON.stringify(result, null, 2)); | ||
|
||
let stringifiedResponse = JSON.stringify(result, null, 2); | ||
log.verbose( | ||
`RESPONSE from [${req.method}] ${url}: ${stringifiedResponse}`, | ||
{result: result} | ||
); | ||
|
||
var status = result.status || 200; | ||
res.status(status); | ||
|
@@ -186,11 +202,11 @@ function makeExpressHandler(promiseHandler) { | |
} | ||
res.json(result.response); | ||
}, (e) => { | ||
log.verbose('error:', e); | ||
log.error(`Error generating response. ${inspect(e)}`, {error: e}); | ||
next(e); | ||
}); | ||
} catch (e) { | ||
log.verbose('exception:', e); | ||
log.error(`Error handling request: ${inspect(e)}`, {error: e}); | ||
next(e); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,6 +184,10 @@ export default { | |
env: "VERBOSE", | ||
help: "Set the logging to verbose" | ||
}, | ||
"jsonLogs": { | ||
env: "JSON_LOGS", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename to PARSE_SERVER_JSON_LOGS |
||
help: "Log as structured JSON objects" | ||
}, | ||
"revokeSessionOnPasswordReset": { | ||
env: "PARSE_SERVER_REVOKE_SESSION_ON_PASSWORD_RESET", | ||
help: "When a user changes their password, either through the reset password email or while logged in, all sessions are revoked if this is true. Set to false if you don't want to revoke sessions.", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import ParseServer from './ParseServer'; | ||
import logger from './logger'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alignement |
||
import S3Adapter from 'parse-server-s3-adapter' | ||
import FileSystemAdapter from 'parse-server-fs-adapter' | ||
import InMemoryCacheAdapter from './Adapters/Cache/InMemoryCacheAdapter' | ||
|
@@ -16,4 +17,4 @@ _ParseServer.createLiveQueryServer = ParseServer.createLiveQueryServer; | |
let GCSAdapter = useExternal('GCSAdapter', 'parse-server-gcs-adapter'); | ||
|
||
export default ParseServer; | ||
export { S3Adapter, GCSAdapter, FileSystemAdapter, InMemoryCacheAdapter, TestUtils, _ParseServer as ParseServer }; | ||
export { S3Adapter, GCSAdapter, FileSystemAdapter, InMemoryCacheAdapter, TestUtils, logger, _ParseServer as ParseServer }; |
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.
This is no longer testing the thing it should be testing, which is that passwords don't get output to the logs. The original version of this PR caused passwords to start getting output to logs, so I'd like to see these tests either not change, or be a lot more high-level/E2E and test the actual string that is logged rather than some intermediate object.
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 would agree with that. That's majorly blocking