2019-10-01 20:12:14 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
module.exports = function logger () {
|
|
|
|
if (!global.log4js) {
|
|
|
|
return function dummyLoggerMiddleware (req, res, next) {
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
level: 'info',
|
|
|
|
buffer: true,
|
2020-02-27 00:19:07 +08:00
|
|
|
// log4js provides a tokens solution as express but it does not provide the request/response in the callback.
|
2019-10-01 20:12:14 +08:00
|
|
|
// Thus it is not possible to extract relevant information from them.
|
|
|
|
// This is a workaround to be able to access request/response.
|
|
|
|
format: function (req, res, format) {
|
|
|
|
const defaultFormat = ':remote-addr :method :req[Host]:url :status :response-time ms -> :res[Content-Type]';
|
|
|
|
const logFormat = global.settings.log_format || defaultFormat;
|
|
|
|
|
|
|
|
return format(logFormat);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const logger = global.log4js.getLogger();
|
|
|
|
|
|
|
|
return global.log4js.connectLogger(logger, options);
|
|
|
|
};
|