Add body in log requests

pull/913/head
fflorent 3 months ago
parent a2a5caf85b
commit 6a37eef205

@ -405,24 +405,31 @@ export class FlexServer implements GristServer {
public addLogging() {
if (this._check('logging')) { return; }
if (process.env.GRIST_LOG_SKIP_HTTP) { return; }
if (isAffirmative(process.env.GRIST_LOG_SKIP_HTTP)) { return; }
// Add a timestamp token that matches exactly the formatting of non-morgan logs.
morganLogger.token('logTime', (req: Request) => log.timestamp());
// Add an optional gristInfo token that can replace the url, if the url is sensitive.
morganLogger.token('gristInfo', (req: RequestWithGristInfo) =>
req.gristInfo || req.originalUrl || req.url);
morganLogger.token('host', (req: express.Request) => req.get('host'));
const msg = ':logTime :host :method :gristInfo :status :response-time ms - :res[content-length]';
morganLogger.token('body', (req: express.Request) => JSON.stringify(req.body));
// For debugging, be careful not to enable logging in production (may log sensitive data)
const shouldLogBody = isAffirmative(process.env.GRIST_LOG_HTTP_BODY);
const msg = `:logTime :host :method :gristInfo ${shouldLogBody ? ':body' : ''} ` +
":status :response-time ms - :res[content-length]";
// In hosted Grist, render json so logs retain more organization.
function outputJson(tokens: any, req: any, res: any) {
return JSON.stringify({
timestamp: tokens.logTime(req, res),
host: tokens.host(req, res),
method: tokens.method(req, res),
path: tokens.gristInfo(req, res),
body: shouldLogBody ? tokens.body(req, res) : {},
status: tokens.status(req, res),
timeMs: parseFloat(tokens['response-time'](req, res)) || undefined,
contentLength: parseInt(tokens.res(req, res, 'content-length'), 10) || undefined,
host: tokens.host(req, res),
altSessionId: req.altSessionId,
});
}

@ -11,7 +11,7 @@ import {Request, Response} from 'express';
import {Writable} from 'stream';
// log api details outside of dev environment (when GRIST_HOSTED_VERSION is set)
const shouldLogApiDetails = Boolean(process.env.GRIST_HOSTED_VERSION);
const shouldLogApiDetails = gutil.isAffirmative(process.env.GRIST_HOSTED_VERSION);
// Offset to https ports in dev/testing environment.
export const TEST_HTTPS_OFFSET = process.env.GRIST_TEST_HTTPS_OFFSET ?

Loading…
Cancel
Save