我是节点js的新手,我正在为我的项目尝试winston logger,一切正常但是当我试图创建我自己的记录器对象并将日志传输到文件名时我收到错误:TypeError:无法读取属性'filename'未定义的 我觉得我做了什么傻事,因为当我只是尝试使用winston默认对象将日志写入文件时,它可以工作,我发布我的代码与错误的快照.
我创建了简单的快速js项目,并在app.js中记录日志代码,如下所示:
/** * Module dependencies. */ var express = require('express'), routes = require('./routes'), user = require('./routes/user'), http = require('http'), path = require('path'); var app = express(); // all environments app.set('port', process.env.PORT || 3000); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.favicon()); app.use(express.logger('dev')); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(app.router); app.use(express.static(path.join(__dirname, 'public'))); // development only if ('development' == app.get('env')) { app.use(express.errorHandler()); } app.get('/', routes.index); app.get('/users', user.list); // adding winston logger code-------------------- var winston = require('winston'); /* * winston.add(winston.transports.File, { filename: 'winstonLogs.log' }); * * winston.log('info', 'Hello distributed log files!'); winston.info('Hello * again distributed logs'); * * winston.level = 'debug'; winston.log('debug', 'Now my debug messages are * written to console!'); * * //winston.remove(winston.transports.Console); */ // ****************************************************************************** var logger = new (winston.Logger)({ transports: [ new (winston.transports.File)({ name: 'info-file', filename: 'filelog-info.log', level: 'info' }), new (winston.transports.File)({ name: 'error-file', filename: 'filelog-error.log', level: 'error' }) ] }); logger.add(winston.transports.File); logger.log('info', 'Hello distributed log files!'); logger.info('Hello again distributed logs'); //logger.add(winston.transports.File).remove(winston.transports.Console); // ending winston logger code-------------------- http.createServer(app).listen(app.get('port'), function() { console.log('Express server listening on port ' + app.get('port')); });
我已注释掉的代码正在运行.
提前致谢.
实际上我添加了不应该在那里的线.
logger.add(winston.transports.File);
删除后它开始工作正常...