Comparing version 1.11.0 to 2.0.0
228
lib/index.js
@@ -6,2 +6,3 @@ 'use strict'; | ||
const Os = require('os'); | ||
const Hoek = require('hoek'); | ||
@@ -17,3 +18,2 @@ const Wreck = require('wreck'); | ||
intervalMsec: 1000, // 1 second | ||
root: false, | ||
exclude: [], | ||
@@ -28,163 +28,159 @@ uncaughtException: false, | ||
exports.register = function (server, options, next) { | ||
exports.plugin = { | ||
pkg: require('../package.json'), | ||
register: async function (server, options) { | ||
Hoek.assert(options.token, 'Missing Loggly API token'); | ||
Hoek.assert(options.token, 'Missing Loggly API token'); | ||
const settings = Hoek.applyToDefaults(internals.defaults, options); | ||
server = (settings.root ? server.root : server); | ||
const settings = Hoek.applyToDefaults(internals.defaults, options); | ||
// Setup log queue | ||
// Setup log queue | ||
const uri = `https://logs-01.loggly.com/bulk/${settings.token}`; | ||
let updates = []; | ||
const flush = function (callback) { | ||
const uri = `https://logs-01.loggly.com/bulk/${settings.token}`; | ||
let updates = []; | ||
const flush = function () { | ||
callback = callback || Hoek.ignore; | ||
if (!updates.length) { | ||
return; | ||
} | ||
if (!updates.length) { | ||
return callback(); | ||
} | ||
const holder = updates; | ||
updates = []; | ||
const holder = updates; | ||
updates = []; | ||
holder.forEach((update) => { | ||
holder.forEach((update) => { | ||
if (settings.tags) { | ||
update.tags = (update.tags ? settings.tags.concat(update.tags) : settings.tags); | ||
} | ||
}); | ||
const payload = holder.map(FastSafeStringify).join('\n'); | ||
const headers = { 'content-type': 'application/json' }; | ||
if (settings.tags) { | ||
update.tags = (update.tags ? settings.tags.concat(update.tags) : settings.tags); | ||
headers['x-loggly-tag'] = settings.tags.join(','); | ||
} | ||
}); | ||
const payload = holder.map(FastSafeStringify).join('\n'); | ||
const headers = { 'content-type': 'application/json' }; | ||
if (settings.tags) { | ||
headers['x-loggly-tag'] = settings.tags.join(','); | ||
} | ||
Wreck.post(uri, { payload, headers, json: true }, callback); | ||
}; | ||
return Wreck.post(uri, { payload, headers, json: true }); | ||
}; | ||
// Setup flush intervals | ||
// Setup flush intervals | ||
const timerId = setInterval(flush, settings.intervalMsec); | ||
const timerId = setInterval(flush, settings.intervalMsec); | ||
// Listen to system exceptions and signals | ||
// Listen to system exceptions and signals | ||
if (settings.uncaughtException) { | ||
process.once('uncaughtException', (err) => { | ||
let onException; | ||
if (settings.uncaughtException) { | ||
onException = async (err) => { | ||
const uncaught = internals.update('error', null, settings); | ||
uncaught.error = { | ||
message: err.message, | ||
stack: err.stack, | ||
data: err.data | ||
}; | ||
const uncaught = internals.update('error', null, settings); | ||
uncaught.error = { | ||
message: err.message, | ||
stack: err.stack, | ||
data: err.data | ||
}; | ||
uncaught.tags = ['bananas', 'uncaught', 'error']; | ||
updates.push(uncaught); | ||
uncaught.tags = ['bananas', 'uncaught', 'error']; | ||
updates.push(uncaught); | ||
return flush((ignore) => { | ||
await flush(); | ||
process.exit(1); | ||
}); | ||
}); | ||
process.on('unhandledRejection', (err, promise) => { | ||
const uncaught = internals.update('error', null, settings); | ||
uncaught.error = { | ||
message: err.message, | ||
stack: err.stack, | ||
data: err.data | ||
}; | ||
uncaught.tags = ['bananas', 'uncaught', 'promise', 'error']; | ||
updates.push(uncaught); | ||
}); | ||
} | ||
process.on('uncaughtException', onException); | ||
process.on('unhandledRejection', onException); | ||
} | ||
if (settings.signals) { | ||
const shutdown = (signal) => { | ||
if (settings.signals) { | ||
const shutdown = (signal) => { | ||
return () => { | ||
return async () => { | ||
const end = internals.update('server', null, settings); | ||
end.tags = ['bananas', 'signal', signal]; | ||
updates.push(end); | ||
server.root.stop({ timeout: settings.stopTimeoutMsec }, process.exit); | ||
const end = internals.update('server', null, settings); | ||
end.tags = ['bananas', 'signal', signal]; | ||
updates.push(end); | ||
await server.stop({ timeout: settings.stopTimeoutMsec }); | ||
process.exit(); | ||
}; | ||
}; | ||
}; | ||
process.once('SIGTERM', shutdown('SIGTERM')); | ||
process.once('SIGINT', shutdown('SIGINT')); | ||
} | ||
process.once('SIGTERM', shutdown('SIGTERM')); | ||
process.once('SIGINT', shutdown('SIGINT')); | ||
} | ||
// Listen to server events | ||
// Listen to server events | ||
const onPostStop = function (srv, nextExt) { | ||
const onPostStop = async function (srv) { | ||
clearInterval(timerId); | ||
clearInterval(timerId); | ||
if (settings.signals) { | ||
process.removeAllListeners('SIGTERM'); | ||
process.removeAllListeners('SIGINT'); | ||
} | ||
if (settings.uncaughtException) { | ||
process.removeListener('uncaughtException', onException); | ||
process.removeListener('unhandledRejection', onException); | ||
} | ||
const end = internals.update('server', null, settings); | ||
end.tags = ['bananas', 'stopped']; | ||
updates.push(end); | ||
return flush(nextExt); | ||
}; | ||
if (settings.signals) { | ||
process.removeAllListeners('SIGTERM'); | ||
process.removeAllListeners('SIGINT'); | ||
} | ||
server.ext('onPostStop', onPostStop); | ||
const end = internals.update('server', null, settings); | ||
end.tags = ['bananas', 'stopped']; | ||
updates.push(end); | ||
await flush(); | ||
}; | ||
// Subscribe to server events | ||
server.ext('onPostStop', onPostStop); | ||
server.on('log', (event, tags) => { | ||
// Subscribe to server events | ||
const update = internals.update('server', null, settings); | ||
update.tags = event.tags; | ||
update.data = internals.error(event.data); | ||
server.events.on('log', (event, tags) => { | ||
updates.push(update); | ||
}); | ||
const update = internals.update('server', null, settings); | ||
update.tags = event.tags; | ||
if (event.error) { | ||
update.error = internals.error(event.error); | ||
} | ||
else if (event.data) { | ||
update.data = event.data; | ||
} | ||
server.on('response', (request) => { | ||
updates.push(update); | ||
}); | ||
const routeSettings = request.route.settings.plugins.bananas || {}; | ||
server.events.on({ name: 'request', channels: 'error' }, (request, event) => { | ||
if (settings.exclude.indexOf(request.path) !== -1 || | ||
routeSettings.exclude) { | ||
const update = internals.update('error', request, settings); | ||
update.error = internals.error(event.error); | ||
updates.push(update); | ||
}); | ||
return; | ||
} | ||
server.events.on('response', (request) => { | ||
const update = internals.update('response', request, settings); | ||
update.code = request.raw.res.statusCode; | ||
const routeSettings = request.route.settings.plugins.bananas || {}; | ||
if (update.code >= 400) { | ||
update.error = request.response.source; | ||
} | ||
if (settings.exclude.indexOf(request.path) !== -1 || | ||
routeSettings.exclude) { | ||
updates.push(update); | ||
}); | ||
return; | ||
} | ||
server.on('request-error', (request, err) => { | ||
const update = internals.update('response', request, settings); | ||
update.code = request.raw.res.statusCode; | ||
const update = internals.update('error', request, settings); | ||
update.error = internals.error(err); | ||
updates.push(update); | ||
}); | ||
if (update.code >= 400) { | ||
update.error = request.response.source; | ||
} | ||
// Log initialization | ||
updates.push(update); | ||
}); | ||
const init = internals.update('server', null, settings); | ||
init.tags = ['bananas', 'initialized']; | ||
init.env = process.env; | ||
updates.push(init); | ||
return flush(next); | ||
}; | ||
// Log initialization | ||
exports.register.attributes = { | ||
pkg: require('../package.json') | ||
const init = internals.update('server', null, settings); | ||
init.tags = ['bananas', 'initialized']; | ||
init.env = process.env; | ||
updates.push(init); | ||
await flush(); | ||
} | ||
}; | ||
@@ -210,3 +206,3 @@ | ||
update.request = { | ||
id: request.id, | ||
id: request.info.id, | ||
received: request.info.received, | ||
@@ -230,6 +226,2 @@ elapsed: now - request.info.received, | ||
if (data instanceof Error === false) { | ||
return data; | ||
} | ||
const error = { | ||
@@ -236,0 +228,0 @@ message: data.message, |
{ | ||
"name": "bananas", | ||
"description": "Minimal Loggly hapi plugin", | ||
"version": "1.11.0", | ||
"version": "2.0.0", | ||
"author": "Eran Hammer <eran@hammer.io> (http://hueniverse.com)", | ||
@@ -14,13 +14,14 @@ "repository": "git://github.com/hueniverse/bananas", | ||
"engines": { | ||
"node": ">=4.x.x" | ||
"node": ">=8.8.0" | ||
}, | ||
"dependencies": { | ||
"hoek": "4.x.x", | ||
"wreck": "10.x.x", | ||
"hoek": "5.x.x", | ||
"wreck": "14.x.x", | ||
"fast-safe-stringify": "1.x.x" | ||
}, | ||
"devDependencies": { | ||
"code": "4.x.x", | ||
"hapi": "16.x.x", | ||
"lab": "11.x.x" | ||
"code": "5.x.x", | ||
"hapi": "17.0.0-rc9", | ||
"lab": "15.x.x", | ||
"teamwork": "3.x.x" | ||
}, | ||
@@ -27,0 +28,0 @@ "scripts": { |
Sorry, the diff of this file is not supported yet
8773
4
167
+ Addedboom@7.3.0(transitive)
+ Addedbourne@1.3.3(transitive)
+ Addedhoek@5.0.46.1.3(transitive)
+ Addedwreck@14.2.0(transitive)
- Removedboom@4.3.1(transitive)
- Removedhoek@4.3.1(transitive)
- Removedwreck@10.0.0(transitive)
Updatedhoek@5.x.x
Updatedwreck@14.x.x