|
|
|
@ -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,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|