Permalink
Browse files

Lots of changes to make it work.

  • Loading branch information...
1 parent 25a8ff8 commit d9674f0f6ac58ef077de45aa5555287711586ad8 @wolfeidau committed Oct 30, 2012
Showing with 285 additions and 71 deletions.
  1. +2 −0 .npmignore
  2. +6 −6 README.md
  3. +4 −1 httptest.js
  4. +97 −24 lib/winston-crashlog.js
  5. +7 −7 package.json
  6. +1 −0 somefile
  7. +100 −0 test/fixtures.js
  8. +68 −33 test/winston-crashlog_test.js
View
@@ -1 +1,3 @@
/node_modules/
+.idea
+*.log
View
@@ -7,14 +7,14 @@ Install the module with: `npm install winston-crashlog`
```javascript
var winston_crashlog = require('winston-crashlog');
-winston_crashlog.awesome(); // "awesome"
-```
-## Documentation
-_(Coming soon)_
+var logger = new (winston.Logger)({
+ transports: [
+ new (winston.transports.Crashlog)({ accessKeyId: '1234', accessKeySecret: '4567890' })
+ ]
+});
-## Examples
-_(Coming soon)_
+```
## Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt](https://github.com/gruntjs/grunt).
View
@@ -3,6 +3,9 @@ var http = require('http'),
winston = require('winston'),
winstonCrashLog = require('./lib/winston-crashlog');
+var config = require('/Users/markw/.crashlog/creds');
+
+console.log('accessKeyId: ' + config.creds.accessKeyId);
var logger = new (winston.Logger)({
transports: [
@@ -11,7 +14,7 @@ var logger = new (winston.Logger)({
});
//winston.handleExceptions(new winston.transports.Http({ 'host': 'localhost', 'port': 8000, 'path': '/notify' }));
-winston.handleExceptions(new winston.transports.CrashLog({ 'host': 'localhost', 'port': 8000, 'path': '/notify' }));
+winston.handleExceptions(new winston.transports.CrashLog({ host: 'localhost', port: 8000, accessKeyId: config.creds.accessKeyId, accessKeySecret: config.creds.accessKeySecret}));
//logger.log('error', 'Hello webhook log files!', { 'foo': 'bar' });
throw new Error('Hello, winston!');
View
@@ -1,25 +1,41 @@
/*
* winston-crashlog
- * https://github.com/markw/winston-crashlog
+ * https://github.com/wolfeidau/winston-crashlog
*
* Copyright (c) 2012 Mark Wolfe
* Licensed under the MIT license.
*/
var util = require('util'),
+ crypto = require('crypto'),
Ofuda = require('ofuda'),
winston = require('winston');
var CrashLog = exports.CrashLog = function (options) {
options = options || {};
options.debug = options.debug || false;
+ options.headerPrefix = options.headerPrefix || 'CrashLog';
+ options.serviceLabel = options.serviceLabel || 'CrashLog';
+
this.name = 'CrashLog';
this.client = new Ofuda(options);
this.level = options.level || 'error';
this.notifierName = options.name || 'nodejsapp';
this.notifierVersion = options.name || '0.0.1';
this.handleExceptions = options.handleExceptions || true;
+
+ this.ssl = !!options.ssl;
+ this.host = options.host || 'stdin.crashlog.io';
+ this.port = options.port;
+ this.auth = options.auth;
+ this.path = options.path || '/events';
+ this.checksum = options.checksum || false;
+
+ if (!this.port) {
+ this.port = this.ssl ? 443 : 80;
+ }
+
};
util.inherits(CrashLog, winston.Transport);
@@ -31,38 +47,95 @@ CrashLog.prototype.name = 'CrashLog';
CrashLog.prototype.log = function (level, msg, meta, callback) {
var self = this,
- notifierName = this.notifierName,
- notifierVersion = this.notifierVersion,
metac = winston.clone(meta) || {};
- var payload = {};
-
- payload.notifier = {
- name: notifierName,
- version: notifierVersion
- };
+ var transport = this.ssl ? require('https') : require('http'),
+ requestDate = new Date(meta.date), // get the date from the event record.
+ payload = this._buildMessage(msg, metac),
+ requestOptions = this._request(payload, requestDate);
- payload.event = {
- message:msg,
- type:"Error",
- timestamp:metac.date
- };
+ var req = transport.request(requestOptions, function (response) {
- payload.backtrace = [];
+ // TODO need to cater for send retries //self.emit('error', err);
+ self.emit('logged status ' + response.statusCode);
+ callback(null, true);
+ });
- metac.trace.forEach(function (frame) {
- payload.backtrace.push({ file:frame.file, number:frame.line, method: frame.method});
+ req.on('error', function (e) {
+ console.log('problem with request: ' + e.message);
});
- payload.environment = {
- platform: metac.process.version
+ // write data to request body
+ req.write(JSON.stringify(payload), encoding = 'utf8');
+ req.end();
+
+};
+
+CrashLog.prototype._buildMessage = function (msg, meta) {
+
+ var data = {
+ payload:{
+ notifier:{
+ name:this.notifierName,
+ version:this.notifierVersion
+ },
+ event:{
+ message:msg,
+ type:"Error",
+ timestamp:(new Date(meta.date)).toUTCString()
+ },
+ backtrace:[],
+ environment:{
+ platform:meta.process.version
+ }
+ }
+
};
-// console.log(msg);
-// console.log(payload);
- //console.log(metac);
+ meta.trace.forEach(function (frame) {
+ data.payload.backtrace.push({ file:frame.file, number:frame.line, method:frame.method});
+ });
+
+ return data;
+};
+
+CrashLog.prototype._request = function (body, requestDate) {
+
+ return this.client.signHttpRequest(this._checkSumRequest({
+ method:'POST',
+ host:this.host,
+ port:this.port,
+ path:this.path,
+ headers:{
+ 'Date':requestDate.toUTCString(),
+ 'Content-Type':'application/json',
+ 'Content-Length':JSON.stringify(body).length
+ }
+ }, body)
+ , this._buildCanonicalStringFromRequest);
+
+};
+CrashLog.prototype._checkSumRequest = function (request, body) {
+ if (this.checksum) {
+ request.headers['Content-MD5'] = this._checkSumBody(body);
+ }
+ return request;
+};
+
+CrashLog.prototype._checkSumBody = function (body) {
+ return crypto.createHash('md5').update(JSON.stringify(body)).digest("hex");
+};
- callback(null, true);
- self.emit('logged');
+CrashLog.prototype._buildCanonicalStringFromRequest = function (request) {
+ return [
+ request.method,
+ request.headers['Content-Type'],
+ '',
+ request.headers['Date'],
+ request.path
+ ].join('\n');
};
+
+
+
View
@@ -1,23 +1,23 @@
{
"name":"winston-crashlog",
- "description":"The best project ever.",
+ "description":"Winston transport for crashlog.io.",
"version":"0.1.0",
- "homepage":"https://github.com/markw/winston-crashlog",
+ "homepage":"https://github.com/wolfeidau/winston-crashlog",
"author":{
"name":"Mark Wolfe",
"email":"mark@wolfe.id.au"
},
"repository":{
"type":"git",
- "url":"git://github.com/markw/winston-crashlog.git"
+ "url":"git://github.com/wolfeidau/winston-crashlog.git"
},
"bugs":{
- "url":"https://github.com/markw/winston-crashlog/issues"
+ "url":"https://github.com/wolfeidau/winston-crashlog/issues"
},
"licenses":[
{
"type":"MIT",
- "url":"https://github.com/markw/winston-crashlog/blob/master/LICENSE-MIT"
+ "url":"https://github.com/wolfeidau/winston-crashlog/blob/master/LICENSE-MIT"
}
],
"main":"lib/winston-crashlog",
@@ -28,8 +28,8 @@
"test":"grunt test"
},
"dependencies":{
- "winston":"*",
- "ofuda":"*"
+ "winston":"~0.6.2",
+ "ofuda":"~0.2.2"
},
"devDependencies":{
"grunt":"~0.3.17",
View
@@ -0,0 +1 @@
+{"payload":{"notifier":{"name":"nodejsapp","version":"0.0.1"},"event":{"message":"uncaughtException","type":"Error","timestamp":"Mon, 29 Oct 2012 07:27:05 GMT"},"backtrace":[{"file":"/Users/markw/Code/Javascript/winston-crashlog/httptest.js","number":17,"method":null},{"file":"module.js","number":449,"method":"_compile"},{"file":"module.js","number":467,"method":"Module._extensions..js"},{"file":"module.js","number":356,"method":"load"},{"file":"module.js","number":312,"method":"Module._load"},{"file":"module.js","number":492,"method":"runMain"},{"file":"node.js","number":244,"method":"startup.processNextTick.process._tickCallback"}],"environment":{"platform":"v0.8.8"}}}
View
@@ -0,0 +1,100 @@
+
+
+exports.sample_exception = { date: 'Mon Oct 29 2012 18:27:05 GMT+1100 (EST)',
+ process:
+ { pid: 6899,
+ uid: 501,
+ gid: 20,
+ cwd: '/Users/markw/Code/Javascript/winston-crashlog',
+ execPath: '/usr/local/Cellar/node/0.8.8/bin/node',
+ version: 'v0.8.8',
+ argv:
+ [ 'node',
+ '/Users/markw/Code/Javascript/winston-crashlog/httptest.js' ],
+ memoryUsage: { rss: 19165184, heapTotal: 11312128, heapUsed: 6158120 } },
+ os:
+ { loadavg: [ 0.2744140625, 0.68896484375, 0.90576171875 ],
+ uptime: 42006 },
+ trace:
+ [ { column: 7,
+ file: '/Users/markw/Code/Javascript/winston-crashlog/httptest.js',
+ function: '',
+ line: 17,
+ method: null,
+ native: false },
+ { column: 26,
+ file: 'module.js',
+ function: 'Module._compile',
+ line: 449,
+ method: '_compile',
+ native: false },
+ { column: 10,
+ file: 'module.js',
+ function: 'Object.Module._extensions..js',
+ line: 467,
+ method: 'Module._extensions..js',
+ native: false },
+ { column: 32,
+ file: 'module.js',
+ function: 'Module.load',
+ line: 356,
+ method: 'load',
+ native: false },
+ { column: 12,
+ file: 'module.js',
+ function: 'Function.Module._load',
+ line: 312,
+ method: 'Module._load',
+ native: false },
+ { column: 10,
+ file: 'module.js',
+ function: 'Module.runMain',
+ line: 492,
+ method: 'runMain',
+ native: false },
+ { column: 9,
+ file: 'node.js',
+ function: 'process.startup.processNextTick.process._tickCallback',
+ line: 244,
+ method: 'startup.processNextTick.process._tickCallback',
+ native: false } ],
+ stack:
+ [ 'Error: Hello, winston!',
+ ' at Object.<anonymous> (/Users/markw/Code/Javascript/winston-crashlog/httptest.js:17:7)',
+ ' at Module._compile (module.js:449:26)',
+ ' at Object.Module._extensions..js (module.js:467:10)',
+ ' at Module.load (module.js:356:32)',
+ ' at Function.Module._load (module.js:312:12)',
+ ' at Module.runMain (module.js:492:10)',
+ ' at process.startup.processNextTick.process._tickCallback (node.js:244:9)' ] };
+
+exports.sampleMessage = { payload: { notifier: { name: 'nodejsapp', version: '0.0.1' },
+ event:
+ { message: 'uncaughtException',
+ type: 'Error',
+ timestamp: 'Mon, 29 Oct 2012 07:27:05 GMT' },
+ backtrace:
+ [ { file: '/Users/markw/Code/Javascript/winston-crashlog/httptest.js',
+ number: 17,
+ method: null },
+ { file: 'module.js', number: 449, method: '_compile' },
+ { file: 'module.js',
+ number: 467,
+ method: 'Module._extensions..js' },
+ { file: 'module.js', number: 356, method: 'load' },
+ { file: 'module.js', number: 312, method: 'Module._load' },
+ { file: 'module.js', number: 492, method: 'runMain' },
+ { file: 'node.js',
+ number: 244,
+ method: 'startup.processNextTick.process._tickCallback' } ],
+ environment: { platform: 'v0.8.8' } } };
+
+// "POST\napplication/json\n\nTue, 30 Oct 2012 19:48:27 GMT\n/events"
+exports.samplePostRequest = {
+ method:'POST',
+ path:'/events',
+ headers:{
+ 'Content-Type':'application/json',
+ 'Date':'Tue, 30 Oct 2012 19:48:27 GMT'
+ }
+};
Oops, something went wrong.

0 comments on commit d9674f0

Please sign in to comment.