Better logging with winston

This commit is contained in:
Florent Vilmart
2016-03-26 13:47:44 -04:00
parent d47a756f80
commit 5c1fe3a325
15 changed files with 180 additions and 255 deletions

View File

@@ -6,6 +6,7 @@
// components that external developers may be modifying.
import express from 'express';
import log from './logger';
export default class PromiseRouter {
// Each entry should be an object with:
@@ -146,9 +147,6 @@ export default class PromiseRouter {
}
}
// Global flag. Set this to true to log every request and response.
PromiseRouter.verbose = process.env.VERBOSE || false;
// A helper function to make an express handler out of a a promise
// handler.
// Express handlers should never throw; if a promise handler throws we
@@ -156,18 +154,14 @@ PromiseRouter.verbose = process.env.VERBOSE || false;
function makeExpressHandler(promiseHandler) {
return function(req, res, next) {
try {
if (PromiseRouter.verbose) {
console.log(req.method, req.originalUrl, req.headers,
JSON.stringify(req.body, null, 2));
}
log.verbose(req.method, req.originalUrl, req.headers,
JSON.stringify(req.body, null, 2));
promiseHandler(req).then((result) => {
if (!result.response && !result.location && !result.text) {
console.log('BUG: the handler did not include a "response" or a "location" field');
log.error('the handler did not include a "response" or a "location" field');
throw 'control should not get here';
}
if (PromiseRouter.verbose) {
console.log('response:', JSON.stringify(result, null, 2));
}
log.verbose(JSON.stringify(result, null, 2));
var status = result.status || 200;
res.status(status);
@@ -186,15 +180,11 @@ function makeExpressHandler(promiseHandler) {
}
res.json(result.response);
}, (e) => {
if (PromiseRouter.verbose) {
console.log('error:', e);
}
log.verbose('error:', e);
next(e);
});
} catch (e) {
if (PromiseRouter.verbose) {
console.log('error:', e);
}
log.verbose('exception:', e);
next(e);
}
}