Add request logging

This commit is contained in:
Michael Jackson
2019-08-02 17:34:00 -07:00
parent 7af4d4dc58
commit 4774e61d50
14 changed files with 112 additions and 52 deletions

View File

@ -0,0 +1,71 @@
import util from 'util';
// https://cloud.google.com/appengine/docs/standard/nodejs/runtime#environment_variables
const projectId = process.env.GAE_APPLICATION;
const enableDebugging = process.env.DEBUG != null;
function noop() {}
function createLog(req) {
const traceContext = req.headers['x-cloud-trace-context'];
if (projectId && traceContext) {
const [traceId, spanId] = traceContext.split('/');
const trace = `projects/${projectId}/traces/${traceId}`;
return {
debug: enableDebugging
? (format, ...args) => {
console.log(
JSON.stringify({
severity: 'DEBUG',
'logging.googleapis.com/trace': trace,
'logging.googleapis.com/spanId': spanId,
message: util.format(format, ...args)
})
);
}
: noop,
info: (format, ...args) => {
console.log(
JSON.stringify({
severity: 'INFO',
'logging.googleapis.com/trace': trace,
'logging.googleapis.com/spanId': spanId,
message: util.format(format, ...args)
})
);
},
error: (format, ...args) => {
console.error(
JSON.stringify({
severity: 'ERROR',
'logging.googleapis.com/trace': trace,
'logging.googleapis.com/spanId': spanId,
message: util.format(format, ...args)
})
);
}
};
}
return {
debug: enableDebugging
? (format, ...args) => {
console.log(util.format(format, ...args));
}
: noop,
info: (format, ...args) => {
console.log(util.format(format, ...args));
},
error: (format, ...args) => {
console.error(util.format(format, ...args));
}
};
}
export default function requestLog(req, res, next) {
req.log = createLog(req);
next();
}