在前面的笔记里讲过express-genrator创建的express项目使用morgan日志,将morgan作为应用级中间件,express项目就会自动记录http访问日志。
Morgan默认了5中日志的格式,也可以让用户自定义日志格式,是一个简单使用的日志中间件。
但是,我用java编程多年,非常清楚log4j的强大与好用。因此,仅仅log4js这个名字就已经让我先入为主了,尽管morgan也是一个不错的日志中间件。
Log4js相比morgan,更加强大,更加贴近java程序员的习惯。
Section 1
让我们从一个简单是例子来开始log4js的体验:
mkdir log4js-examples
cd log4js-examples
npm init
npm install log4js
创建一个log4js-minimal.js文件:
var log4js = require('log4js');
var logger = log4js.getLogger();
logger.debug("Some debug messages");
lee@mypc ~/works/nodejs/study23/log4js-examples $ node log4js-minimal.js
[2016-01-19 19:07:08.258] [DEBUG] [default] - Some debug messages
这就是一个最最简单的log4js实例:启动,debug信息输出到控制台,over。
Section 2
再来一个输出到文件,log4js-file.js:
var log4js = require('log4js');
//console log is loaded by default, so you won't normally need to do this
//log4js.loadAppender('console');
log4js.loadAppender('file');
//log4js.addAppender(log4js.appenders.console());
log4js.addAppender(log4js.appenders.file('logs/cheese.log'), 'cheese');
var logger = log4js.getLogger('cheese');
logger.setLevel('ERROR');
logger.trace('Entering cheese testing');
logger.debug('Got cheese.');
logger.info('Cheese is Gouda.');
logger.warn('Cheese is quite smelly.');
logger.error('Cheese is too ripe!');
logger.fatal('Cheese was breeding ground for listeria.');
运行:
lee@mypc ~/works/nodejs/study23/log4js-examples $ node log4js-file.js
[2016-01-19 19:11:16.447] [ERROR] cheese - Cheese is too ripe!
[2016-01-19 19:11:16.452] [FATAL] cheese - Cheese was breeding ground for listeria.
lee@mypc ~/works/nodejs/study23/log4js-examples $ ls logs
cheese.log
lee@mypc ~/works/nodejs/study23/log4js-examples $ cat logs/cheese.log
[2016-01-19 14:02:40.929] [ERROR] cheese - Cheese is too ripe!
[2016-01-19 14:02:40.933] [FATAL] cheese - Cheese was breeding ground for listeria.
运行完,我们可以看到日志输出到文件logs/cheese.log
Section 3
log4js-file.js也可以这样配置,效果是一样的。
log4js-configure.js:
var log4js = require('log4js');
log4js.configure({
appenders: [
{ type: 'console' },
{ type: 'file', filename: 'logs/cheese-configure.log', category: 'cheese' }
]
});
var logger = log4js.getLogger('cheese');
logger.setLevel('ERROR');
logger.trace('Entering cheese testing');
logger.debug('Got cheese.');
logger.info('Cheese is Gouda.');
logger.warn('Cheese is quite smelly.');
logger.error('Cheese is too ripe!');
logger.fatal('Cheese was breeding ground for listeria.');
我们在javaEE中使用log4j的时候,总是习惯配置log4j.properties,有没有?!
Log4js也可以从配置文件来读取配置。
configfile.json:
{
"appenders": [
{ "type": "console" },
{ "type": "file", "filename": "logs/cheese-configfile.log", "category": "cheese" }
]
}
log4js-configfile.js:
var log4js = require('log4js');
log4js.configure('configfile.json');
var logger = log4js.getLogger('cheese');
logger.setLevel('ERROR');
logger.trace('Entering cheese testing');
logger.debug('Got cheese.');
logger.info('Cheese is Gouda.');
logger.warn('Cheese is quite smelly.');
logger.error('Cheese is too ripe!');
logger.fatal('Cheese was breeding ground for listeria.');
Section 5
log4js完全想morgan一样当express的中间件用。
来试一下
创建一个项目log-log4js:
express log-log4js
cd log-log4js
#install node-modules
npm install
#install log4js
npm install log4js
#create a directory for save log files
mkdir logs
可以这样修改app.js:
...
//var logger = require('morgan');
var log4js = require('log4js');
...
//app.use(logger('dev'));
log4js.configure({
appenders: [
{ type: 'console' }, //控制台输出
{
type: 'file', //文件输出
filename: 'logs/access.log',
maxLogSize: 1024,
backups:3,
category: 'normal'
}
]
});
var logger = log4js.getLogger('normal');
logger.setLevel(log4js.levels.INFO);
app.use(log4js.connectLogger(logger, {level:log4js.levels.INFO}));
...
npm start
curl http://localhost:3000/
curl http://localhost:3000/users
这时,可以看到logs有一个日志文件access.log
lee@mypc ~/works/nodejs/study23/log-log4js $ ls logs
access.log
lee@mypc ~/works/nodejs/study23/log-log4js $ cat logs/access.log
[2016-01-22 20:40:54.977] [INFO] normal - ::ffff:127.0.0.1 - - "GET / HTTP/1.1" 200 170 "" "curl/7.35.0"
[2016-01-22 20:41:03.245] [INFO] normal - ::ffff:127.0.0.1 - - "GET /users HTTP/1.1" 200 23 "" "curl/7.35.0"
[2016-01-22 20:41:05.817] [INFO] normal - ::ffff:127.0.0.1 - - "GET / HTTP/1.1" 200 170 "" "curl/7.35.0"
[2016-01-22 20:41:07.422] [INFO] normal - ::ffff:127.0.0.1 - - "GET /users HTTP/1.1" 200 23 "" "curl/7.35.0"
Section 6
看到这儿,有没有发现section2中的代码输出了很多日志信息,而在日志文件中却只有两行。
Log4js一定有比morgan更强大的地方,否则它就不配叫log4js。
Log4js的输出级别有6个,从低到高分别是 trace, debug, info, warn, error, fatal。
在section2的实例中,设置了log的输出级别是error,所以低于error级别的trace,debug,info和warn都没有输出。
设置输出级别的好处尤其体现在测试环境和生产环境,测试环境中跟踪测试,往往需要非常详细的debug信息,而在生产环境中运行稳定版本则不需要过多的日志信息,只需要必要的warn,error或fetal。