logger.js
const { createLogger, format, transports } = require('winston');
const { combine, timestamp, label, printf } = format;
const path = require('path');
const myFormat = printf(({ level, message, label, timestamp }) => {
return `${timestamp} [${label}] ${level}: ${message}`;
});
const getLabel = function(callingModule) {
const parts = callingModule.filename.split('/');
return path.join(parts[parts.length - 2], parts.pop());
};
const logger = (callingModule)=> createLogger({
format: combine(
label({ label: getLabel(callingModule) }),
timestamp(),
myFormat
),
transports: [new transports.Console()]
});
module.exports = {
logger:logger
}
use as below:
const logger = require('../util/logger').logger(module);
logger.info('logger file..');
//util/logger is my file path of logger.js
output:
2020-01-16T08:28:41.893Z [controller/shop.js] info: logger file..
const { createLogger, format, transports } = require('winston');
const { combine, timestamp, label, printf } = format;
const path = require('path');
const myFormat = printf(({ level, message, label, timestamp }) => {
return `${timestamp} [${label}] ${level}: ${message}`;
});
const getLabel = function(callingModule) {
const parts = callingModule.filename.split('/');
return path.join(parts[parts.length - 2], parts.pop());
};
const logger = (callingModule)=> createLogger({
format: combine(
label({ label: getLabel(callingModule) }),
timestamp(),
myFormat
),
transports: [new transports.Console()]
});
module.exports = {
logger:logger
}
use as below:
const logger = require('../util/logger').logger(module);
logger.info('logger file..');
//util/logger is my file path of logger.js
output:
2020-01-16T08:28:41.893Z [controller/shop.js] info: logger file..
No comments:
Post a Comment