niveau
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -14,3 +14,2 @@ 'use strict'; | ||
module.exports = function create(options) { | ||
let logConfig; | ||
options = options || readRedisOptions(); | ||
@@ -21,6 +20,5 @@ _.defaults(options, { redisKey: LOG_CONFIG_KEY }); | ||
store.on('config', config => { | ||
logConfig = config; | ||
try { | ||
if (logConfig && logConfig.request) { | ||
const re = logConfig.request; | ||
if (config && config.request) { | ||
const re = config.request; | ||
if (re.url) re.url = RegExp(re.url); | ||
@@ -30,2 +28,3 @@ if (re.ip) re.ip = RegExp(re.ip); | ||
} | ||
middleware.logConfig = config; | ||
} catch (e) { | ||
@@ -37,3 +36,4 @@ store.emit('error', new VError(e, 'Failed to parse log config')); | ||
function middleware(req, res, next) { | ||
if (logConfig && logConfig.request && match(req, logConfig.request)) { | ||
let logConfig = middleware.logConfig; | ||
if (logConfig && match(req, logConfig.request)) { | ||
debug('Request matched'); | ||
@@ -40,0 +40,0 @@ req.logLevel = logConfig.level; |
'use strict'; | ||
module.exports = function match(req, criteria) { | ||
return (!criteria.ip || criteria.ip.test(clientAddress(req))) && | ||
return !criteria || | ||
(!criteria.ip || criteria.ip.test(clientAddress(req))) && | ||
(!criteria.url || criteria.url.test(req.url)) && | ||
@@ -6,0 +7,0 @@ matchObject(criteria.headers, req.headers); |
{ | ||
"name": "niveau", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Node.js package to switch log level per request in Cloud Foundry", | ||
@@ -56,6 +56,6 @@ "main": "lib", | ||
"nyc": "^11.4.1", | ||
"sinon": "^4.2.0", | ||
"sinon-chai": "^2.14.0", | ||
"sinon": "^5.0.0", | ||
"sinon-chai": "^3.0.0", | ||
"supertest": "^3.0.0" | ||
} | ||
} |
@@ -95,2 +95,3 @@ [![Build Status](https://travis-ci.org/niveau/niveau.svg?branch=master)](https://travis-ci.org/niveau/niveau) | ||
// req.logLevel - the log level to be used for this request (if present) | ||
// nv.logConfig - the current log configuration | ||
}); | ||
@@ -107,2 +108,5 @@ ``` | ||
If `options` is not provided, use bound Redis service. | ||
If no Redis service is bound, use local Redis on default port. | ||
The middleware also listens for log configuration changes and emits some events. | ||
@@ -118,3 +122,3 @@ | ||
Event arguments: | ||
* `config` [log configuration](#log-configuration) object | ||
* `config` [log configuration](#log-configuration) object, `null` if no configuration is set | ||
@@ -130,9 +134,13 @@ Emitted when log configuration is changed or deleted. | ||
#### nv.logConfig | ||
Contains the current [log configuration](#log-configuration). | ||
It is `null` if no log configuration is set. | ||
#### Log configuration | ||
Log configuration object: | ||
* `request` request matching criteria, | ||
* `request` (optional) request matching criteria, | ||
if missing or empty, the log level should be used for all requests | ||
* `url` `RegExp` to match against the request URL | ||
* `ip` `RegExp` to match against the client IP address | ||
* `headers` an object to match against request headers, each values is a `RegExp` | ||
* `url` (optional) `RegExp` to match against the request URL | ||
* `ip` (optional) `RegExp` to match against the client IP address | ||
* `headers` (optional) an object to match against request headers, each values is a `RegExp` | ||
* `level` log level as a string to use for matching requests | ||
@@ -139,0 +147,0 @@ |
@@ -79,2 +79,3 @@ #!/usr/bin/env node | ||
}, {}); | ||
headers = _.isEmpty(headers) ? undefined : headers; | ||
@@ -88,8 +89,10 @@ let expire; | ||
let request = { | ||
url: cmdOptions.url, | ||
ip: cmdOptions.ip, | ||
headers | ||
}; | ||
request = _.some(request, v => v !== undefined) ? request : undefined; | ||
let logConfig = { | ||
request: { | ||
url: cmdOptions.url, | ||
ip: cmdOptions.ip, | ||
headers | ||
}, | ||
request, | ||
// requestCounterKey: 'counter-name', | ||
@@ -116,4 +119,5 @@ level | ||
} else { | ||
debug('redis SET %s', LOG_CONFIG_KEY, logConfig); | ||
let params = [LOG_CONFIG_KEY, JSON.stringify(logConfig)]; | ||
let value = JSON.stringify(logConfig); | ||
debug('redis SET %s %s', LOG_CONFIG_KEY, value); | ||
let params = [LOG_CONFIG_KEY, value]; | ||
expire && params.push('EX', expire); | ||
@@ -120,0 +124,0 @@ client.set(params, (err, reply) => { |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
18335
257
221
2