天天看點

nodejs中間件詳解

1.logger

作用:用來輸出使用者請求日志

參數:options或者format字元串

  • format:參考下面的tokens
  • stream:輸出流,預設是stdout
  • buffer:緩沖時間,預設是1000ms
  • immediate:立刻列印日志

tokens: format格式

  • :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.js

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

connect.logger()

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’)

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’)

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就好了。

版權聲明:本文為CSDN部落客「weixin_33875564」的原創文章,遵循CC 4.0 BY-SA版權協定,轉載請附上原文出處連結及本聲明。

原文連結:https://blog.csdn.net/weixin_33875564/article/details/92077872