hmpo-logger
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -43,3 +43,3 @@ var winston = require('winston'), | ||
_.extend(meta, { | ||
name: this._name | ||
label: this._name | ||
}); | ||
@@ -119,2 +119,8 @@ | ||
pid: process.pid, | ||
env: new TokenFn(function (arg) { | ||
return process.env[arg]; | ||
}), | ||
res: new TokenFn(function (arg) { | ||
@@ -121,0 +127,0 @@ if (!arg || !this.res || !this.res.getHeader) { return; } |
var winston = require('winston'), | ||
morgan = require('morgan'), | ||
_ = require('underscore'), | ||
Logger = require('./logger'), | ||
moduleName = require('./module-name'); | ||
moduleName = require('./module-name'), | ||
onFinished = require('on-finished'), | ||
onHeaders = require('on-headers'); | ||
/* eslint "no-console":0 */ | ||
@@ -16,3 +18,2 @@ | ||
Manager.defaultOptions = { | ||
logPublicRequests: false, | ||
console: true, | ||
@@ -26,6 +27,7 @@ connsoleJSON: false, | ||
error: './error.log', | ||
errorJSON: false, | ||
errorLevel: 'error', | ||
errorJSON: true, | ||
errorLevel: 'exceptions', | ||
meta: { | ||
host: 'host', | ||
pm: 'env[pm_id]', | ||
sessionID: 'sessionID', | ||
@@ -45,2 +47,4 @@ verb: 'method', | ||
}, | ||
logPublicRequests: false, | ||
logHealthcheckRequests: false, | ||
format: ':clientip :sessionID :method :request HTTP/:httpVersion :statusCode :res[content-length] - :responseTime ms' | ||
@@ -157,2 +161,4 @@ }; | ||
Manager.rePublicRequests = /\/public\//; | ||
Manager.reHealthcheckRequests = /^\/healthcheck(\/|$)/; | ||
@@ -164,27 +170,52 @@ Manager.prototype.middleware = function () { | ||
var middleware = function (req, res, next) { | ||
var timeDiff = function (from, to, digits) { | ||
if (digits === undefined) { digits = 3; } | ||
var ms = (to[0] - from[0]) * 1e3 | ||
+ (to[1] - from[1]) * 1e-6; | ||
return +ms.toFixed(digits); | ||
}; | ||
morgan(' ', { | ||
stream: { | ||
write: function () { | ||
res.responseTime = res.responseTime || morgan['response-time'](req, res); | ||
var handleHeaders = function (req, res) { | ||
res._startAt = process.hrtime(); | ||
if (req._startAt && res._startAt) { | ||
res.responseTime = timeDiff(req._startAt, res._startAt); | ||
} | ||
}; | ||
if (options.logPublicRequests === false) { | ||
if (req.originalUrl.match(/\/public/)) { | ||
return; | ||
} | ||
} | ||
var handleFinished = function (req, res) { | ||
res._endAt = process.hrtime(); | ||
if (res._startAt && res._endAt) { | ||
res.transferTime = timeDiff(res._startAt, res._endAt); | ||
} | ||
logger.request(options.format, { | ||
req: req, | ||
res: res | ||
}); | ||
} | ||
var url = req.originalUrl || req.url || ''; | ||
if (options.logPublicRequests === false) { | ||
if (url.match(Manager.rePublicRequests)) { | ||
return; | ||
} | ||
})(req, res, next); | ||
} | ||
if (options.logHealthcheckRequests === false) { | ||
if (url.match(Manager.reHealthcheckRequests)) { | ||
return; | ||
} | ||
} | ||
logger.request(options.format, { | ||
req: req, | ||
res: res | ||
}); | ||
}; | ||
return middleware; | ||
return function (req, res, next) { | ||
req._startAt = process.hrtime(); | ||
onHeaders(res, _.partial(handleHeaders, req, res)); | ||
onFinished(res, _.partial(handleFinished, req, res)); | ||
next(); | ||
}; | ||
}; | ||
module.exports = Manager; |
{ | ||
"name": "hmpo-logger", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Consistent logging for hmpo apps", | ||
@@ -22,3 +22,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"morgan": "^1.7.0", | ||
"on-finished": "^2.3.0", | ||
"on-headers": "^1.0.1", | ||
"underscore": "^1.8.3", | ||
@@ -25,0 +26,0 @@ "winston": "^2.2.0" |
@@ -53,4 +53,3 @@ # hmpo-logger | ||
var hmpoLogger = require('hmpo-logger'); | ||
hmpoLogger.config({ | ||
logPublicRequests: false, | ||
hmpoLogger.config({ // defaults: | ||
console: true, | ||
@@ -64,6 +63,7 @@ connsoleJSON: false, | ||
error: './error.log', | ||
errorJSON: false, | ||
errorLevel: ['error', 'warn'], | ||
errorJSON: true, | ||
errorLevel: 'exceptions', | ||
meta: { | ||
host: 'host', | ||
pm: 'env[pm_id]', | ||
sessionID: 'sessionID', | ||
@@ -83,2 +83,4 @@ verb: 'method', | ||
}, | ||
logPublicRequests: false, | ||
logHealthcheckRequests: false, | ||
format: ':clientip :sessionID :method :request HTTP/:httpVersion :statusCode :res[content-length] - :responseTime ms' | ||
@@ -85,0 +87,0 @@ }); |
@@ -42,3 +42,3 @@ | ||
sinon.match({ | ||
name: 'test', | ||
label: 'test', | ||
host: sinon.match.string | ||
@@ -55,3 +55,3 @@ })); | ||
sinon.match({ | ||
name: 'test', | ||
label: 'test', | ||
host: sinon.match.string, | ||
@@ -74,3 +74,3 @@ test1: 'metadata', | ||
sinon.match({ | ||
name: 'test', | ||
label: 'test', | ||
host: sinon.match.string, | ||
@@ -90,3 +90,3 @@ sessionID: 'abc123', | ||
sinon.match({ | ||
name: 'test', | ||
label: 'test', | ||
host: sinon.match.string, | ||
@@ -103,2 +103,15 @@ request: '' | ||
describe('env', function () { | ||
it('should return an environment variable', function () { | ||
Logger.tokens.env.fn.call(null, 'USER') | ||
.should.equal(process.env.USER); | ||
}); | ||
it('should return undefined for unlikely env variable', function () { | ||
delete process.env.NON_EXISTANT_ENV_VAR; | ||
expect(Logger.tokens.res.fn.call(null, 'NON_EXISTANT_ENV_VAR')) | ||
.to.be.undefined; | ||
}); | ||
}); | ||
describe('res', function () { | ||
@@ -105,0 +118,0 @@ var getHeader = sinon.stub(); |
@@ -57,3 +57,3 @@ | ||
t[2].name.should.equal('error'); | ||
t[2].level.should.equal('error'); | ||
t[2].level.should.equal('exceptions'); | ||
t[2].filename.should.equal('error.log'); | ||
@@ -60,0 +60,0 @@ }); |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances 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
36041
808
103
4
5
+ Addedon-finished@^2.3.0
+ Addedon-headers@^1.0.1
+ Addedon-finished@2.4.1(transitive)
- Removedmorgan@^1.7.0
- Removedbasic-auth@2.0.1(transitive)
- Removeddebug@2.6.9(transitive)
- Removeddepd@2.0.0(transitive)
- Removedmorgan@1.10.0(transitive)
- Removedms@2.0.0(transitive)
- Removedon-finished@2.3.0(transitive)
- Removedsafe-buffer@5.1.2(transitive)