0.0.1 • Published 7 years ago

http-logs v0.0.1

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

http-logs

HTTP request log middleware

Usage

npm i -S http-logs
const app = require('express')()
const logger = require('http-logs')
app.use(logger())
app.listen(3000)

> GET / HTTP/1.1 200 1ms

config

logger([preset | token][,options])

第一个参数是一个字符串,可以是preset,也可以是自定义的token

preset

presetoutputdescription
normalGET / HTTP/1.1 200 218ms默认
tinyGET / 200简单
long2017-05-14 21:03:25 周日 ::ffff:192.168.0.100 /index.htmlHTTP/1.1 304 23ms Mozilla/5.0 (Windows NT 6.1; Win64;x64) AppleWebkit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36详细

token

tokenoutputdescription
methodGET请求方法
url/path/to/resource请求路径
httpVersionHTTP/1.1http版本
status200响应状态码
duration28ms请求到输出完成的持续时间
ip::ffff:192.168.0.100IP地址
date2017-05-14请求开始日期
time19:00:00请求开始时间
day周日请求开始星期
userAgentMozilla/5.0客户端用户代理内容
const token = '[:date :time] :method :url :httpVersion :status'
app.use(logger(token))

// [2017-05-14 19:23:33] GET /index.html HTTP/1.1 200

options

重定义token

const token = '[:date :when :time] :method :url'
const options = {
  // 增加新的token类型when
  when: function (req, res) {
    return (new Date().getHours() >= 12) ? '下午' : '上午'
  },
  // 重写method
  method: function (req, res) {
    return req.method.toLowerCase()
  }
}
app.use(logger(token, options))
// [2017-05-14 上午 10:22:29] get /index.html