2018-03-31 03:30:30 +08:00
|
|
|
import {NextFunction, Request, Response} from 'express';
|
|
|
|
import {Logger} from '../Logger';
|
2017-06-21 17:32:56 +08:00
|
|
|
|
2017-07-09 20:23:50 +08:00
|
|
|
/**
|
|
|
|
* Adds logging to express
|
|
|
|
*/
|
2017-06-21 17:32:56 +08:00
|
|
|
export class LoggerRouter {
|
|
|
|
public static route(app: any) {
|
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
app.get('/api*', (req: Request, res: Response, next: NextFunction) => {
|
2017-06-21 17:32:56 +08:00
|
|
|
req['_startTime'] = Date.now();
|
|
|
|
req['logged'] = true;
|
|
|
|
const end = res.end;
|
|
|
|
res.end = (a?: any, b?: any, c?: any) => {
|
|
|
|
res.end = end;
|
|
|
|
res.end(a, b, c);
|
2018-03-31 03:30:30 +08:00
|
|
|
Logger.verbose(req.method, req.url, res.statusCode, (Date.now() - req['_startTime']) + 'ms');
|
2017-06-21 17:32:56 +08:00
|
|
|
};
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
2018-03-31 03:30:30 +08:00
|
|
|
app.get('/node_modules*', (req: Request, res: Response, next: NextFunction) => {
|
2017-06-21 17:32:56 +08:00
|
|
|
req['_startTime'] = Date.now();
|
|
|
|
req['logged'] = true;
|
|
|
|
const end = res.end;
|
|
|
|
res.end = (a?: any, b?: any, c?: any) => {
|
|
|
|
res.end = end;
|
|
|
|
res.end(a, b, c);
|
2018-03-31 03:30:30 +08:00
|
|
|
Logger.silly(req.method, req.url, res.statusCode, (Date.now() - req['_startTime']) + 'ms');
|
2017-06-21 17:32:56 +08:00
|
|
|
};
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
|
|
|
app.use((req: Request, res: Response, next: NextFunction) => {
|
2018-03-31 03:30:30 +08:00
|
|
|
if (req['logged'] === true) {
|
2017-06-21 17:32:56 +08:00
|
|
|
return next();
|
|
|
|
}
|
|
|
|
req['_startTime'] = Date.now();
|
|
|
|
const end = res.end;
|
|
|
|
res.end = (a?: any, b?: any, c?: any) => {
|
|
|
|
res.end = end;
|
|
|
|
res.end(a, b, c);
|
2018-03-31 03:30:30 +08:00
|
|
|
Logger.debug(req.method, req.url, res.statusCode, (Date.now() - req['_startTime']) + 'ms');
|
2017-06-21 17:32:56 +08:00
|
|
|
};
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|