logger.setLevel 不是函数,而 运行 logger.js 使用 log4js

logger.setLevel is not a function while running logger.js using log4js

我正在尝试使用 log4js 创建日志文件。当 运行 节点 app.js 完成所有配置后,我收到以下错误。

错误:

TypeError: logger.setLevel is not a function
at Object.<anonymous> (E:\Angular\logging_project\routes\logger.js:14:20)
at Object.<anonymous> (E:\Angular\logging_project\routes\index.js:3:13)

请看我的代码。

logger.js

  var log4js = require('log4js');
  log4js.configure({
     appenders: {
        console: { type: 'console' },
        filelog: { type: 'file', filename: "c://test.log", category: 'my_project' }
      },
      categories: {
         file: { appenders:['filelog'],level:'error'},
         another: {appenders: ['console'],level: 'trace'},
         default: {appenders: ['console','filelog'],level: 'trace'}
      }
   });
  var logger  = log4js.getLogger('my_project');
        logger.setLevel('DEBUG');
        Object.defineProperty(exports, "LOG", {
                    value:logger,
 })

index.js:

var express = require('express');
var router = express.Router();
var logger= require('./logger.js');
var log=logger.LOG;
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

exports.index = function(req, res){
log.debug("testing the debug log here....................");
log.error("testing the error log here....................");
log.info("testing the info log here....................");
log.trace("testing the trace log here....................");
  res.render('index', { title: 'Express' });
};

module.exports = router;

app.js

var express = require('express');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var log = require('./routes/logger');
var app = express();

app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

//use logger
app.use(log4js.connectLogger(log, { level: log4js.levels.DEBUG }));

app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
console.log('node Express server listening on port ' + server.address().port);
});

module.exports = app;

我是 node js 的新手,这是我第一次使用 node js 创建日志文件。任何人都请帮我解决这个问题。如果我得到一个工作代码可能会更好。我整天都被这个困住了。

您可以使用以下代码设置记录器级别:

logger.level = 'DEBUG';