2021-08-12 15:04:37 +02:00
|
|
|
import url from 'url';
|
2024-03-18 13:58:05 +01:00
|
|
|
import type { RequestHandler } from 'express';
|
|
|
|
import type { IUnleashConfig } from '../types/option';
|
2017-11-16 15:41:33 +01:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
const requestLogger: (config: IUnleashConfig) => RequestHandler = (config) => {
|
2019-04-30 21:14:23 +02:00
|
|
|
const logger = config.getLogger('HTTP');
|
2021-04-23 15:31:12 +02:00
|
|
|
const enable = config.server.enableRequestLogger;
|
2017-11-16 15:41:33 +01:00
|
|
|
return (req, res, next) => {
|
2021-04-23 15:31:12 +02:00
|
|
|
if (enable) {
|
2020-04-30 23:04:06 +02:00
|
|
|
res.on('finish', () => {
|
|
|
|
const { pathname } = url.parse(req.originalUrl);
|
|
|
|
logger.info(`${res.statusCode} ${req.method} ${pathname}`);
|
|
|
|
});
|
2017-11-16 15:41:33 +01:00
|
|
|
}
|
2020-04-30 23:04:06 +02:00
|
|
|
next();
|
2017-11-16 15:41:33 +01:00
|
|
|
};
|
|
|
|
};
|
2021-08-12 15:04:37 +02:00
|
|
|
|
|
|
|
export default requestLogger;
|