Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@zhike/log-express-middleware

Package Overview
Dependencies
Maintainers
13
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@zhike/log-express-middleware

日志打印中间件

  • 0.0.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
13
Created
Source

log-express-middleware

日志打印中间件

image

Install

$ npm install @zhike/log-express-middleware

Example

const logger = require('@zhike/log-express-middleware')
const Express = require('express')
var bodyParser = require('body-parser')

const app = new Express()

app.use(bodyParser.json())

//只打印requestBody
app.use(logger())

// 打印所有路径的headers中的content-type。只打印以/player2开头的路径的responseBody
app.use(logger({
    requestHeaders: ['content-type'],
    responseHeaders: ['content-type'],
    responseBodyWhiteList: ['/^\/player2/']
}))

// 对密码进行隐藏
app.use(logger({
  handler: function(req, res, _log) {
    if (req.path === '/signin') {
      _log.requestBody.password = '****1**'
    }
  }
}))

Configuration Options

参数名类型说明
reqIdString响应标识id(使用自定义的reqId需放在日志中间件前。如果为空且ctx.reqId为空则使用uuidv4自动生成reqId)
requestHeadersArray需要打印的request头部 (默认不打印)
responseHeadersArray需要打印的response头部 (默认不打印)
responseBodyWhiteListArray白名单,字符串数组,可为正则(对于正则匹配剔除部分路由,可在业务代码中的ctx.useResponseBodyOption=false跳过路由规则)
responseBodyBlackListArray黑名单,字符串数组,可为正则(对于正则匹配剔除部分路由,可在业务代码中使用ctx.useResponseBodyOption=false跳过路由规则)
handlerFunction(req, res, _log)特殊处理函数。_log目前有requestBody属性,可以用来调整requestBody打印(是req.body的副本,进行修改,不会影响req.body)

注意:

  1. 必会打印reqId,路径,方法,响应时间。
  2. 默认打印requestBody。
  3. 默认不打印responseBody。
  4. responseBodyWhiteList和不能和responseBodyBlackList同时传值
  5. responseBodyWhiteList和responseBodyBlackList暂未支持同一path不同method,后续会完善。
  6. 在express的中间件函数中 ,可以通过 req._reqId 取到reqId。可以在使用console.log时带上。

Keywords

FAQs

Package last updated on 15 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc