Comparing version 1.0.1 to 1.0.2
28
index.js
@@ -10,6 +10,12 @@ const ilog = require('ilog') | ||
* 日志中间件 | ||
* @param {String} serviceName 服务名称 | ||
* @param {function} parseClientFunction 解析客户端的函数 | ||
* @param {String} name 服务名称 | ||
* @param {Function} parseClientFunction 解析客户端的函数 | ||
* @param {Array} ignores 忽略的路由规则 | ||
* @param {Array} ignoreMethods 忽略的方法, 默认是 OPTIONS | ||
*/ | ||
module.exports = function (serviceName, parseClientFunction = getClient) { | ||
module.exports = function (options = {}) { | ||
let serviceName = options.name || UNKNOWN_SERVICE | ||
let parseClientFunction = options.parseClientFunction || getClient | ||
let ignoreRoutes = options.ignores || ['/favicon.ico', '/version', '/assets'] | ||
let ignoreMethods = options.ignoreMethods || ['options'] | ||
if (typeof parseClientFunction !== 'function') { | ||
@@ -26,10 +32,10 @@ throw new Error('getClientFunction must be a function') | ||
// log uncaught downstream errors | ||
log.call(this, start) | ||
log.call(this, start, ignoreRoutes, ignoreMethods) | ||
throw err | ||
} | ||
log.call(this, start) | ||
log.call(this, start, ignoreRoutes, ignoreMethods) | ||
} | ||
} | ||
function log (start) { | ||
function log (start, ignoreRoutes, ignoreMethods) { | ||
let ctx = this | ||
@@ -50,2 +56,12 @@ let client = ctx.state.client | ||
} | ||
// 忽略的地址不做日志记录 | ||
if (ignoreRoutes.length && ignoreRoutes.some((ele) => { | ||
return api.includes(ele) | ||
})) { | ||
return | ||
} | ||
// 跨域忽略 | ||
if (ignoreMethods.includes(method.toLowerCase())) return | ||
ilog.info({ | ||
@@ -52,0 +68,0 @@ class: 'ilog', |
{ | ||
"name": "@tng/ilog", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,3 +8,3 @@ TNG ilog 中间件 | ||
require('@tng/ilog)('Teambition X App') | ||
require('@tng/ilog)({ name: 'Service Name' }) | ||
``` | ||
@@ -16,8 +16,13 @@ | ||
ilog('Client Name', function (ctx) { | ||
if (_.result(ctx.state, 'consumer._id')) { | ||
return ctx.state.consumer.name + '_' + ctx.state.consumer.clientKey | ||
ilog({ | ||
name: 'Client Name', | ||
ignoreRoutes: ['/version'], // 不打印日志的路由 | ||
ignoreMethods: ['GET', 'OPTIONS'], // 默认是 ['OPTIONS'] | ||
parseClientFunction: function (ctx) { | ||
if (_.result(ctx.state, 'consumer._id')) { | ||
return ctx.state.consumer.name + '_' + ctx.state.consumer.clientKey | ||
} | ||
return null | ||
} | ||
return null | ||
}) | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3290
73
27