我有我的nodejs项目,我想从我的route.js中分离记录器以获得干净的代码。
但我有下一个问题,当我尝试将我的记录器加载到route.js文件时,它会显示下一个错误:
TypeError: loggers.getTicketLogger is not a function
at Object.<anonymous> (/home/ismael/projects/nodejs-ticketing/routes/ticket.js:3:23)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (/home/ismael/projects/nodejs-ticketing/server.js:49:1)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:141:18)
at node.js:933:3
我的记录器dir中有下一个代码:
记录器/ ticket.js
var log4js = require('log4js');
module.exports = function(){
getTicketLogger = function(){
var logger = null;
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file(__dirname + '/../log/ticket.log'), 'ticket-log');
logger = log4js.getLogger('ticket-log');
logger.setLevel('DEBUG');
return logger;
}
};
我的route.js文件有下一个头
路由/ ticket.js
var Ticket = require('../models/ticket'),
loggers = require('../loggers/ticket.js'),
log = loggers.getTicketLogger();
module.exports = function(app){
}
我做错了什么,以及如何改进代码以将记录器与route.js文件分开。
谢谢你。
答案 0 :(得分:1)
loggers / ticket.js 应该返回一个包含try catch
方法的对象。
getTicketLogger
然后在 routes / ticket.js
中var log4js = require('log4js');
module.exports = function() {
var getTicketLogger = function(){
var logger = null;
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file(__dirname + '/../log/ticket.log'), 'ticket-log');
logger = log4js.getLogger('ticket-log');
logger.setLevel('DEBUG');
return logger;
}
return { getTicketLogger: getTicketLogger };
};