I'm new to JavaScript, and in Java I probably would do this as a singleton. I need something that I can call from anywhere and instantiate only once.
This is what I have done, but I need some advice to write it in the right way in this new prototype based and functional JavaScript.
var winston = require('winston');
var genericLogger = null,
dbLogger = null,
viewLogger = null,
networkLogger = null;
function initLogger() {
console.log('logger.js > initLogger');
genericLogger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: './log/error.log' })
]
});
dbLogger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: './log/db.log' })
]
});
viewLogger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: './log/view.log' })
]
});
networkLogger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: './log/network.log' })
]
});
}
initLogger();
var Logger = {};
Logger.error = function(n) {
genericLogger.log('error', n);
};
Logger.info = function(n) {
genericLogger.log('info', n);
};
Logger.db = function(t, n) {
if(n) {
dbLogger.log(t, n);
} else {
dbLogger.info(t);
}
};
Logger.view = function(t, n) {
if(n) {
viewLogger.log(t, n);
} else {
viewLogger.info(t);
}
};
Logger.network = function(t, n) {
if(n) {
networkLogger.log(t, n);
} else {
networkLogger.info(t);
}
};
module.exports = Logger;
I can call it from anywhere with require
, and I expose only few public methods with module.exports
and due to the require mechanism there is only one instance.
Is there a cleaner way to do it? More OOP? More functional?