71 lines
1.7 KiB
JavaScript
71 lines
1.7 KiB
JavaScript
const log4js = require('log4js');
|
|
|
|
/**
|
|
* @module Configuring and creating a logger based on the log4js library.
|
|
*
|
|
* This module configures and creates a logger based on the log4js library. It also provides.
|
|
* The ability to redirect console output through the logger to process logs.
|
|
*/
|
|
|
|
log4js.addLayout('json', function(config) {
|
|
return (logEvent) => {
|
|
if (logEvent.error) {
|
|
logEvent.errorMessage = logEvent.error.message;
|
|
logEvent.errorStack = logEvent.error.stack;
|
|
logEvent.errorName = logEvent.error.name;
|
|
}
|
|
if (logEvent.data && logEvent.data[0] && typeof logEvent.data[0] === 'string') {
|
|
logEvent.message = logEvent.data[0];
|
|
}
|
|
if (logEvent.errorMessage) {
|
|
logEvent.message = logEvent.errorMessage;
|
|
}
|
|
let result = JSON.stringify(logEvent);
|
|
result += config.separator;
|
|
return result;
|
|
};
|
|
});
|
|
|
|
log4js.configure({
|
|
appenders: {
|
|
out: {
|
|
type: 'stdout',
|
|
layout: {
|
|
type: 'pattern',
|
|
pattern: '%[[%dZ] [%p]%] - %m',
|
|
tokens: {}
|
|
}
|
|
},
|
|
app: {
|
|
type: "file",
|
|
filename: "application.log",
|
|
layout: { type: 'json', separator: ',' }
|
|
},
|
|
},
|
|
categories: {
|
|
default: { appenders: ['out', 'app'], level: 'all' },
|
|
[`[${process.env.SERVICE_NAME}]`]: { appenders: ['out', 'app'], level: 'all' }
|
|
},
|
|
disableClustering: true
|
|
});
|
|
|
|
const logger = log4js.getLogger();
|
|
logger.level = 'all';
|
|
|
|
(function () {
|
|
console.error = function (errMessage) {
|
|
logger.error(errMessage);
|
|
};
|
|
|
|
console.log = function (logMessage) {
|
|
if (process.env.NODE_ENV === 'production') return;
|
|
logger.debug(logMessage);
|
|
};
|
|
|
|
console.warn = function (warnMessage) {
|
|
logger.info(warnMessage);
|
|
};
|
|
})();
|
|
|
|
module.exports = logger;
|