bunyan-loggly
Advanced tools
Comparing version 0.0.5 to 1.0.0
92
index.js
@@ -0,64 +1,68 @@ | ||
var loggly = require('loggly'); | ||
var loggly = require('loggly'), | ||
util = require('util'); | ||
function Bunyan2Loggly(logglyConfig, bufferLength, bufferTimeout){ | ||
if(!logglyConfig || !logglyConfig.token || !logglyConfig.subdomain){ | ||
throw new Error('bunyan-loggly requires a config object with token and subdomain'); | ||
} | ||
function Bunyan2Loggly (logglyConfig, buffer) { | ||
logglyConfig.json = true; | ||
this.logglyConfig = logglyConfig || {}; | ||
this.logglyClient = loggly.createClient(logglyConfig); | ||
// define the log as being json (because bunyan is a json logger) | ||
this.logglyConfig.json = true; | ||
// define the buffer count, unless one has already been defined | ||
this.buffer = buffer || 1; | ||
this._buffer = []; | ||
// add the https tag by default, just to make the loggly source setup work as expect | ||
this.logglyConfig.tags = this.logglyConfig.tags || []; | ||
this.logglyConfig.tags.push('https'); | ||
// create the client | ||
this.client = loggly.createClient(logglyConfig); | ||
this._buffer = []; | ||
this.bufferLength = bufferLength || 1; | ||
this.bufferTimeout = bufferTimeout; | ||
} | ||
Bunyan2Loggly.prototype.write = function(rec) { | ||
Bunyan2Loggly.prototype.write = function(data){ | ||
if (typeof data !== 'object') { | ||
throw new Error('bunyan-loggly requires a raw stream. Please define the type as raw when setting up the bunyan stream.'); | ||
} | ||
if (typeof rec !== 'object' && !Array.isArray(rec)) { | ||
throw new Error('bunyan-loggly requires a raw stream. Please define the type as raw when setting up the bunyan stream.'); | ||
} | ||
// loggly prefers timestamp over time | ||
if (data.time) { | ||
data = JSON.parse(JSON.stringify(data)); | ||
data.timestamp = data.time; | ||
delete data.time; | ||
} | ||
if (typeof rec === 'object') { | ||
this._buffer.push(data); | ||
// loggly prefers timestamp over time | ||
if (rec.time !== undefined) { | ||
rec.timestamp = rec.time; | ||
delete rec.time; | ||
} | ||
this._checkBuffer(); | ||
}; | ||
} | ||
Bunyan2Loggly.prototype._processBuffer = function(){ | ||
clearTimeout(this._timeoutId); | ||
// write to our array buffer | ||
this._buffer.push(rec); | ||
var content = this._buffer.slice(); | ||
this._buffer = []; | ||
// check the buffer, we may or may not need to send to loggly | ||
this.checkBuffer(); | ||
for (var i = 0; i < content.length; i++) { | ||
this.logglyClient.log(content[i]); | ||
} | ||
}; | ||
Bunyan2Loggly.prototype.checkBuffer = function () { | ||
Bunyan2Loggly.prototype._checkBuffer = function(){ | ||
var bunyan2Loggly = this; | ||
if (this._buffer.length < this.buffer) { | ||
return; | ||
} | ||
if (!this._buffer.length) { | ||
return; | ||
} | ||
// duplicate the array, because it could be modified before our HTTP call succeeds | ||
var content = this._buffer.slice(); | ||
this._buffer = []; | ||
if(this._buffer.length >= this.bufferLength){ | ||
return this._processBuffer(); | ||
} | ||
// log multiple (or single) requests with loggly | ||
this.client.log(content); | ||
if(this.bufferTimeout){ | ||
clearTimeout(this._timeoutId); | ||
this._timeoutId = setTimeout( | ||
function(){ | ||
bunyan2Loggly._processBuffer(); | ||
}, | ||
this.bufferTimeout | ||
); | ||
} | ||
}; | ||
module.exports.Bunyan2Loggly = Bunyan2Loggly; | ||
module.exports = Bunyan2Loggly; |
{ | ||
"name": "bunyan-loggly", | ||
"version": "0.0.5", | ||
"version": "1.0.0", | ||
"description": "A bunyan stream to transport logs to loggly", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/smebberson/bunyan-loggly.git" | ||
}, | ||
"keywords": [ | ||
@@ -15,16 +11,26 @@ "bunyan", | ||
], | ||
"author": "Scott Mebberson", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/smebberson/bunyan-loggly/issues" | ||
}, | ||
"homepage": "https://github.com/smebberson/bunyan-loggly", | ||
"author": "Maurice Butler <maurice.butler@gmail.com>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"loggly": "~1.0.3" | ||
"clone": "^1.0.2", | ||
"loggly": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "~1.18.0", | ||
"should": "~3.1.3", | ||
"bunyan": "~0.22.1" | ||
} | ||
"proxyquire": "^1.7.4", | ||
"tape": "^4.4.0" | ||
}, | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"scripts": { | ||
"test": "node ./tests" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/MauriceButler/bunyan-loggly.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/MauriceButler/bunyan-loggly/issues" | ||
}, | ||
"homepage": "https://github.com/MauriceButler/bunyan-loggly#readme" | ||
} |
117
README.md
@@ -1,3 +0,2 @@ | ||
bunyan-loggly | ||
============= | ||
# bunyan-loggly | ||
@@ -15,10 +14,5 @@ A bunyan stream to send logs through to loggly. | ||
token: "your-really-long-input-token", | ||
subdomain: "your-subdomain", | ||
auth: { | ||
username: "your-username", | ||
password: "your-password" | ||
} | ||
subdomain: "your-subdomain" | ||
} | ||
``` | ||
> Please note: auth values are NOT required to simply send logs through to loggly. | ||
@@ -31,7 +25,11 @@ ## Usage | ||
var bunyan = require('bunyan'), | ||
Bunyan2Loggly = require('bunyan-loggly').Bunyan2Loggly, | ||
logger; | ||
Bunyan2Loggly = require('bunyan-loggly'), | ||
logglyConfig = { | ||
token: 'your-account-token', | ||
subdomain: 'your-sub-domain' | ||
}, | ||
logglyStream = new Bunyan2Loggly(logglyConfig); | ||
// create the logger | ||
logger = bunyan.createLogger({ | ||
var logger = bunyan.createLogger({ | ||
name: 'logglylog', | ||
@@ -41,6 +39,3 @@ streams: [ | ||
type: 'raw', | ||
stream: new Bunyan2Loggly({ | ||
token: 'your-account-token', | ||
subdomain: 'your-sub-domain' | ||
}) | ||
stream: logglyStream | ||
} | ||
@@ -53,26 +48,25 @@ ] | ||
> Please note: you MUST define `type: 'raw'` as bunyan-loggly expects to recieve objects so that certain values can be changed as required by loggly (i.e. time to timestamp). | ||
> Please note: you MUST define `type: 'raw'` as bunyan-loggly expects to receive objects so that certain values can be changed as required by loggly (i.e. time to timestamp). | ||
### Express logging | ||
## Buffering | ||
This is an example of using bunyan-loggly to store express.js request logs. | ||
bunyan-loggly supports basic buffering and when setup, will only send your logs through to loggly on every x logs. To setup buffering, just pass an integer as the second parameter when creating a new instance of Bunyan2Loggly: | ||
```javascript | ||
var path = require('path'), | ||
bunyan = require('bunyan'), | ||
serializerRequest = require('../lib/serializer-request'), | ||
Bunyan2Loggly = require('bunyan-loggly').Bunyan2Loggly, | ||
request; | ||
var bunyan = require('bunyan'), | ||
Bunyan2Loggly = require('bunyan-loggly'), | ||
logglyConfig = { | ||
token: 'your-account-token', | ||
subdomain: 'your-sub-domain' | ||
}, | ||
bufferLength = 5, | ||
logglyStream = new Bunyan2Loggly(logglyConfig, bufferLength); | ||
// create the logger | ||
request = bunyan.createLogger({ | ||
name: 'request', | ||
serializers: { req: bunyan.stdSerializers.req }, | ||
var logger = bunyan.createLogger({ | ||
name: 'logglylog', | ||
streams: [ | ||
{ | ||
type: 'raw', | ||
stream: new Bunyan2Loggly({ | ||
token: 'your-account-token', | ||
subdomain: 'your-sub-domain' | ||
}) | ||
stream: logglyStream | ||
} | ||
@@ -82,32 +76,27 @@ ] | ||
// export the middleware | ||
module.exports = function () { | ||
return function (req, res, next) { | ||
// move on straight away | ||
next(); | ||
// log this request | ||
request.info({ | ||
req : req, | ||
production: process.env.NODE_ENV === 'production' | ||
}); | ||
} | ||
} | ||
logger.info({}); // won't send to loggly | ||
logger.info({}); // won't send to loggly | ||
logger.info({}); // won't send to loggly | ||
logger.info({}); // won't send to loggly | ||
logger.info({}); // will send to loggly | ||
logger.info({}); // won't send to loggly | ||
``` | ||
## Buffering | ||
### Buffer Timeout | ||
bunyan-loggly supports basic buffering and when setup, will only send your logs through to loggly on every x logs. To setup buffering, just pass an integer as the second parameter when creating a new instance of Bunyan2Loggly: | ||
When buffering, a timeout can be provided to force flushing the buffer after a period of time. To setup a flush timeout, pass a timeout value (in ms) as the third parameter when creating a new instance of Bunyan2Loggly: | ||
```javascript | ||
var bunyan = require('bunyan'), | ||
Bunyan2Loggly = require('bunyan-loggly').Bunyan2Loggly, | ||
logger; | ||
Bunyan2Loggly = require('bunyan-loggly'), | ||
logglyConfig = { | ||
token: 'your-account-token', | ||
subdomain: 'your-sub-domain' | ||
}, | ||
bufferLength = 5, | ||
bufferTimeout = 500, | ||
logglyStream = new Bunyan2Loggly(logglyConfig, bufferLength, bufferTimeout); | ||
// create the logger | ||
logger = bunyan.createLogger({ | ||
var logger = bunyan.createLogger({ | ||
name: 'logglylog', | ||
@@ -117,6 +106,3 @@ streams: [ | ||
type: 'raw', | ||
stream: new Bunyan2Loggly({ | ||
token: 'your-account-token', | ||
subdomain: 'your-sub-domain' | ||
}, 5) | ||
stream: logglyStream | ||
} | ||
@@ -126,20 +112,3 @@ ] | ||
logger.info({}); // won't send to loggly | ||
logger.info({}); // won't send to loggly | ||
logger.info({}); // won't send to loggly | ||
logger.info({}); // won't send to loggly | ||
logger.info({}); // will send to loggly | ||
logger.info({}); // won't send to loggly | ||
logger.info({}); // will be sent to loggly in 500ms if buffer threshold is not reached | ||
``` | ||
Changes | ||
------- | ||
Most recent change, v0.0.4. | ||
- bunyan-loggly now requires to be setup as a [raw stream][rawstream] | ||
[You can read about all changes.][bunyanlogglyhistory] | ||
[rawstream]: https://github.com/trentm/node-bunyan#stream-type-raw "Bunyan raw stream" | ||
[bunyanlogglyhistory]: https://github.com/smebberson/bunyan-loggly/blob/master/History.md "bunyan-loggly history" |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
12965
2
198
0
0
2
8
108
2
+ Addedclone@^1.0.2
+ Addedasynckit@0.4.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbl@1.1.2(transitive)
+ Addedclone@1.0.4(transitive)
+ Addedform-data@2.0.0(transitive)
+ Addedloggly@1.1.1(transitive)
+ Addedpunycode@1.4.1(transitive)
+ Addedqs@6.2.4(transitive)
+ Addedrequest@2.75.0(transitive)
+ Addedtough-cookie@2.3.4(transitive)
- Removedasync@2.6.4(transitive)
- Removedbl@1.0.3(transitive)
- Removedform-data@1.0.1(transitive)
- Removedlodash@4.17.21(transitive)
- Removedloggly@1.0.9(transitive)
- Removedqs@5.2.1(transitive)
- Removedrequest@2.67.0(transitive)
- Removedtough-cookie@2.2.2(transitive)
Updatedloggly@^1.1.0