node.js学习笔记(21) express日志

创建一个新的express项目,然后运行:

mkdir study21
cd study21
express log-morgan
cd log-morgan
npm install
npm start

再用curl,测试http request:

curl http://localhost:3000
curl -d '' http://localhost:3000 
curl http://localhsot:3000/users
curl -d '' http://localhost:3000/users


最后看一下log-morgan的输出日志:

lee@mypc ~/works/nodejs/study21/log-morgan $ npm start

> log-morgan@0.0.0 start /home/lee/works/nodejs/study21/log-morgan
> node ./bin/www

GET / 200 338.136 ms - 170
POST / 404 60.506 ms - 1225
GET /users 200 2.432 ms - 23
POST /users 404 43.958 ms - 1225

Morgan是一个node.js关于http请求的日志中间件。
上例中,我们可以看到每次http请求,express实例都会输出日志,并且使用一致的格式。

这是因为express-generator生成的express项目默认使用morgan日志中间件,请看如下源码片段。

...
var logger = require('morgan');
...
app.use(logger('dev'));
...



输出格式


Morgan预定义了5中输出格式:

  • combined

:remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"


  • common

:remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length]


  • dev

:method :url :status :response-time ms - :res[content-length]


  • short

:remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms


  • tiny

:method :url :status :res[content-length] - :response-time ms


express-generator生成的express项目默认使用dev格式,我们也可以使用除5中预定义格式外的自定义格式,修改app.js如下:

...
var logger = require('morgan');
...
app.use(logger('This is a customer format. :method :url :status :response-time ms'));
...


重新运行log-morgan项目,并使用curl访问http后的log如下:

lee@mypc ~/works/nodejs/study21/log-morgan $ npm start

> log-morgan@0.0.0 start /home/lee/works/nodejs/study21/log-morgan
> node ./bin/www

This is a customer format. GET / 200 327.309 ms
This is a customer format. POST / 404 58.865 ms
This is a customer format. GET /users 200 3.753 ms
This is a customer format. POST /users 404 39.597 ms

写入日志文件


上面的日志都是在控制台输出的,morgan当然也支持将日志输出到文件。

我们可以这样修改app.js:

...
var logger = require('morgan');
var fs = require('fs')
...
// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'})

// setup the logger
app.use(logger('combined', {stream: accessLogStream}))
...
这时,重启项目,再用curl测试http访问,发现在控制已经没有日志输出了。而在log-morgan项目根目录下有一个日志文件access.log,它的内容是:

::ffff:127.0.0.1 - - [18/Jan/2016:08:41:57 +0000] "GET / HTTP/1.1" 200 170 "-" "curl/7.35.0"
::ffff:127.0.0.1 - - [18/Jan/2016:08:41:58 +0000] "POST / HTTP/1.1" 404 1225 "-" "curl/7.35.0"
::ffff:127.0.0.1 - - [18/Jan/2016:08:41:59 +0000] "GET /users HTTP/1.1" 200 23 "-" "curl/7.35.0"
::ffff:127.0.0.1 - - [18/Jan/2016:08:42:01 +0000] "POST /users HTTP/1.1" 404 1225 "-" "curl/7.35.0"

当然,它完全可以更高级一点,比如每天一个日志。

修改app.js如下:

...
var logger = require('morgan');
var fs = require('fs')
var FileStreamRotator = require('file-stream-rotator')
...
var logDirectory = __dirname + '/logs'

// ensure log directory exists
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)

// create a rotating write stream
var accessLogStream = FileStreamRotator.getStream({
  filename: logDirectory + '/access-%DATE%.log',
  frequency: 'daily',
  verbose: false
})
// setup the logger
app.use(logger('combined', {stream: accessLogStream}))
...
这样,日志都保存在logs目录下,并且每天一个access日志文件。



上一篇:node.js学习笔记(25) express与数据库


下一篇:网络攻防实战——使用wirelesskeyview轻松获取无线密码