nodejs中間件詳解

1.logger

做用:用來輸出用戶請求日誌app

參數:options或者format字符串ui

  • format:參考下面的tokens
  • stream:輸出流,默認是stdout
  • buffer:緩衝時間,默認是1000ms
  • immediate:馬上打印日誌

tokens: format格式url

  • :req[header] ex: :req[Accept]
  • :res[header] ex: :res[Content-Length]
  • :http-version
  • :response-time
  • :remote-addr
  • :date
  • :method
  • :url
  • :referrer
  • :user-agent
  • :status

Formats:縮寫日誌

default ‘:remote-addr – – [:date] 「:method :url HTTP/:http-version」 :status :res[content-length] 「:referrer」 「:user-agent」‘
short ‘:remote-addr – :method :url HTTP/:http-version :status :res[content-length] – :response-time ms’
tiny ‘:method :url :status :res[content-length] – :response-time ms’
dev concise output colored by response status for development use

例子:新建logger.jscode

var connect = require('connect');
var app = connect()
   .use(connect.logger())
   .use(function (req, res) {
       res.end('hello world\n');
   })
   .listen(3000);

connect.logger()orm

127.0.0.1 - - [Mon, 23 Sep 2013 05:14:18 GMT] "GET / HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKi
t/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"
127.0.0.1 - - [Mon, 23 Sep 2013 05:14:18 GMT] "GET /favicon.ico HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64)
 AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36"

connect.logger(‘short’)token

127.0.0.1 - GET / HTTP/1.1 200 - - 9 ms
127.0.0.1 - GET /favicon.ico HTTP/1.1 200 - - 1 ms

connect.logger(‘dev’)ci

GET / 200 5ms
GET /favicon.ico 200 1ms

connect.logger(function(tokens, req, res){ return ‘some format string’ })開發

some format string
some format string

因此在開發環境,咱們日誌設置成dev就行了。rem

相關文章
相關標籤/搜索