Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit d9674f0

Browse files
committed
Lots of changes to make it work.
1 parent 25a8ff8 commit d9674f0

File tree

8 files changed

+285
-71
lines changed

8 files changed

+285
-71
lines changed

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
/node_modules/
2+
.idea
3+
*.log

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ Install the module with: `npm install winston-crashlog`
77

88
```javascript
99
var winston_crashlog = require('winston-crashlog');
10-
winston_crashlog.awesome(); // "awesome"
11-
```
1210

13-
## Documentation
14-
_(Coming soon)_
11+
var logger = new (winston.Logger)({
12+
transports: [
13+
new (winston.transports.Crashlog)({ accessKeyId: '1234', accessKeySecret: '4567890' })
14+
]
15+
});
1516

16-
## Examples
17-
_(Coming soon)_
17+
```
1818

1919
## Contributing
2020
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).

httptest.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ var http = require('http'),
33
winston = require('winston'),
44
winstonCrashLog = require('./lib/winston-crashlog');
55

6+
var config = require('/Users/markw/.crashlog/creds');
7+
8+
console.log('accessKeyId: ' + config.creds.accessKeyId);
69

710
var logger = new (winston.Logger)({
811
transports: [
@@ -11,7 +14,7 @@ var logger = new (winston.Logger)({
1114
});
1215

1316
//winston.handleExceptions(new winston.transports.Http({ 'host': 'localhost', 'port': 8000, 'path': '/notify' }));
14-
winston.handleExceptions(new winston.transports.CrashLog({ 'host': 'localhost', 'port': 8000, 'path': '/notify' }));
17+
winston.handleExceptions(new winston.transports.CrashLog({ host: 'localhost', port: 8000, accessKeyId: config.creds.accessKeyId, accessKeySecret: config.creds.accessKeySecret}));
1518

1619
//logger.log('error', 'Hello webhook log files!', { 'foo': 'bar' });
1720
throw new Error('Hello, winston!');

lib/winston-crashlog.js

Lines changed: 97 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,41 @@
11
/*
22
* winston-crashlog
3-
* https://github.com/markw/winston-crashlog
3+
* https://github.com/wolfeidau/winston-crashlog
44
*
55
* Copyright (c) 2012 Mark Wolfe
66
* Licensed under the MIT license.
77
*/
88
var util = require('util'),
9+
crypto = require('crypto'),
910
Ofuda = require('ofuda'),
1011
winston = require('winston');
1112

1213
var CrashLog = exports.CrashLog = function (options) {
1314
options = options || {};
1415
options.debug = options.debug || false;
1516

17+
options.headerPrefix = options.headerPrefix || 'CrashLog';
18+
options.serviceLabel = options.serviceLabel || 'CrashLog';
19+
1620
this.name = 'CrashLog';
1721
this.client = new Ofuda(options);
1822

1923
this.level = options.level || 'error';
2024
this.notifierName = options.name || 'nodejsapp';
2125
this.notifierVersion = options.name || '0.0.1';
2226
this.handleExceptions = options.handleExceptions || true;
27+
28+
this.ssl = !!options.ssl;
29+
this.host = options.host || 'stdin.crashlog.io';
30+
this.port = options.port;
31+
this.auth = options.auth;
32+
this.path = options.path || '/events';
33+
this.checksum = options.checksum || false;
34+
35+
if (!this.port) {
36+
this.port = this.ssl ? 443 : 80;
37+
}
38+
2339
};
2440

2541
util.inherits(CrashLog, winston.Transport);
@@ -31,38 +47,95 @@ CrashLog.prototype.name = 'CrashLog';
3147

3248
CrashLog.prototype.log = function (level, msg, meta, callback) {
3349
var self = this,
34-
notifierName = this.notifierName,
35-
notifierVersion = this.notifierVersion,
3650
metac = winston.clone(meta) || {};
3751

38-
var payload = {};
39-
40-
payload.notifier = {
41-
name: notifierName,
42-
version: notifierVersion
43-
};
52+
var transport = this.ssl ? require('https') : require('http'),
53+
requestDate = new Date(meta.date), // get the date from the event record.
54+
payload = this._buildMessage(msg, metac),
55+
requestOptions = this._request(payload, requestDate);
4456

45-
payload.event = {
46-
message:msg,
47-
type:"Error",
48-
timestamp:metac.date
49-
};
57+
var req = transport.request(requestOptions, function (response) {
5058

51-
payload.backtrace = [];
59+
// TODO need to cater for send retries //self.emit('error', err);
60+
self.emit('logged status ' + response.statusCode);
61+
callback(null, true);
62+
});
5263

53-
metac.trace.forEach(function (frame) {
54-
payload.backtrace.push({ file:frame.file, number:frame.line, method: frame.method});
64+
req.on('error', function (e) {
65+
console.log('problem with request: ' + e.message);
5566
});
5667

57-
payload.environment = {
58-
platform: metac.process.version
68+
// write data to request body
69+
req.write(JSON.stringify(payload), encoding = 'utf8');
70+
req.end();
71+
72+
};
73+
74+
CrashLog.prototype._buildMessage = function (msg, meta) {
75+
76+
var data = {
77+
payload:{
78+
notifier:{
79+
name:this.notifierName,
80+
version:this.notifierVersion
81+
},
82+
event:{
83+
message:msg,
84+
type:"Error",
85+
timestamp:(new Date(meta.date)).toUTCString()
86+
},
87+
backtrace:[],
88+
environment:{
89+
platform:meta.process.version
90+
}
91+
}
92+
5993
};
6094

61-
// console.log(msg);
62-
// console.log(payload);
63-
//console.log(metac);
95+
meta.trace.forEach(function (frame) {
96+
data.payload.backtrace.push({ file:frame.file, number:frame.line, method:frame.method});
97+
});
98+
99+
return data;
100+
};
101+
102+
CrashLog.prototype._request = function (body, requestDate) {
103+
104+
return this.client.signHttpRequest(this._checkSumRequest({
105+
method:'POST',
106+
host:this.host,
107+
port:this.port,
108+
path:this.path,
109+
headers:{
110+
'Date':requestDate.toUTCString(),
111+
'Content-Type':'application/json',
112+
'Content-Length':JSON.stringify(body).length
113+
}
114+
}, body)
115+
, this._buildCanonicalStringFromRequest);
116+
117+
};
64118

119+
CrashLog.prototype._checkSumRequest = function (request, body) {
120+
if (this.checksum) {
121+
request.headers['Content-MD5'] = this._checkSumBody(body);
122+
}
123+
return request;
124+
};
125+
126+
CrashLog.prototype._checkSumBody = function (body) {
127+
return crypto.createHash('md5').update(JSON.stringify(body)).digest("hex");
128+
};
65129

66-
callback(null, true);
67-
self.emit('logged');
130+
CrashLog.prototype._buildCanonicalStringFromRequest = function (request) {
131+
return [
132+
request.method,
133+
request.headers['Content-Type'],
134+
'',
135+
request.headers['Date'],
136+
request.path
137+
].join('\n');
68138
};
139+
140+
141+

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
{
22
"name":"winston-crashlog",
3-
"description":"The best project ever.",
3+
"description":"Winston transport for crashlog.io.",
44
"version":"0.1.0",
5-
"homepage":"https://github.com/markw/winston-crashlog",
5+
"homepage":"https://github.com/wolfeidau/winston-crashlog",
66
"author":{
77
"name":"Mark Wolfe",
88
"email":"mark@wolfe.id.au"
99
},
1010
"repository":{
1111
"type":"git",
12-
"url":"git://github.com/markw/winston-crashlog.git"
12+
"url":"git://github.com/wolfeidau/winston-crashlog.git"
1313
},
1414
"bugs":{
15-
"url":"https://github.com/markw/winston-crashlog/issues"
15+
"url":"https://github.com/wolfeidau/winston-crashlog/issues"
1616
},
1717
"licenses":[
1818
{
1919
"type":"MIT",
20-
"url":"https://github.com/markw/winston-crashlog/blob/master/LICENSE-MIT"
20+
"url":"https://github.com/wolfeidau/winston-crashlog/blob/master/LICENSE-MIT"
2121
}
2222
],
2323
"main":"lib/winston-crashlog",
@@ -28,8 +28,8 @@
2828
"test":"grunt test"
2929
},
3030
"dependencies":{
31-
"winston":"*",
32-
"ofuda":"*"
31+
"winston":"~0.6.2",
32+
"ofuda":"~0.2.2"
3333
},
3434
"devDependencies":{
3535
"grunt":"~0.3.17",

somefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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"}}}

test/fixtures.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
2+
3+
exports.sample_exception = { date: 'Mon Oct 29 2012 18:27:05 GMT+1100 (EST)',
4+
process:
5+
{ pid: 6899,
6+
uid: 501,
7+
gid: 20,
8+
cwd: '/Users/markw/Code/Javascript/winston-crashlog',
9+
execPath: '/usr/local/Cellar/node/0.8.8/bin/node',
10+
version: 'v0.8.8',
11+
argv:
12+
[ 'node',
13+
'/Users/markw/Code/Javascript/winston-crashlog/httptest.js' ],
14+
memoryUsage: { rss: 19165184, heapTotal: 11312128, heapUsed: 6158120 } },
15+
os:
16+
{ loadavg: [ 0.2744140625, 0.68896484375, 0.90576171875 ],
17+
uptime: 42006 },
18+
trace:
19+
[ { column: 7,
20+
file: '/Users/markw/Code/Javascript/winston-crashlog/httptest.js',
21+
function: '',
22+
line: 17,
23+
method: null,
24+
native: false },
25+
{ column: 26,
26+
file: 'module.js',
27+
function: 'Module._compile',
28+
line: 449,
29+
method: '_compile',
30+
native: false },
31+
{ column: 10,
32+
file: 'module.js',
33+
function: 'Object.Module._extensions..js',
34+
line: 467,
35+
method: 'Module._extensions..js',
36+
native: false },
37+
{ column: 32,
38+
file: 'module.js',
39+
function: 'Module.load',
40+
line: 356,
41+
method: 'load',
42+
native: false },
43+
{ column: 12,
44+
file: 'module.js',
45+
function: 'Function.Module._load',
46+
line: 312,
47+
method: 'Module._load',
48+
native: false },
49+
{ column: 10,
50+
file: 'module.js',
51+
function: 'Module.runMain',
52+
line: 492,
53+
method: 'runMain',
54+
native: false },
55+
{ column: 9,
56+
file: 'node.js',
57+
function: 'process.startup.processNextTick.process._tickCallback',
58+
line: 244,
59+
method: 'startup.processNextTick.process._tickCallback',
60+
native: false } ],
61+
stack:
62+
[ 'Error: Hello, winston!',
63+
' at Object.<anonymous> (/Users/markw/Code/Javascript/winston-crashlog/httptest.js:17:7)',
64+
' at Module._compile (module.js:449:26)',
65+
' at Object.Module._extensions..js (module.js:467:10)',
66+
' at Module.load (module.js:356:32)',
67+
' at Function.Module._load (module.js:312:12)',
68+
' at Module.runMain (module.js:492:10)',
69+
' at process.startup.processNextTick.process._tickCallback (node.js:244:9)' ] };
70+
71+
exports.sampleMessage = { payload: { notifier: { name: 'nodejsapp', version: '0.0.1' },
72+
event:
73+
{ message: 'uncaughtException',
74+
type: 'Error',
75+
timestamp: 'Mon, 29 Oct 2012 07:27:05 GMT' },
76+
backtrace:
77+
[ { file: '/Users/markw/Code/Javascript/winston-crashlog/httptest.js',
78+
number: 17,
79+
method: null },
80+
{ file: 'module.js', number: 449, method: '_compile' },
81+
{ file: 'module.js',
82+
number: 467,
83+
method: 'Module._extensions..js' },
84+
{ file: 'module.js', number: 356, method: 'load' },
85+
{ file: 'module.js', number: 312, method: 'Module._load' },
86+
{ file: 'module.js', number: 492, method: 'runMain' },
87+
{ file: 'node.js',
88+
number: 244,
89+
method: 'startup.processNextTick.process._tickCallback' } ],
90+
environment: { platform: 'v0.8.8' } } };
91+
92+
// "POST\napplication/json\n\nTue, 30 Oct 2012 19:48:27 GMT\n/events"
93+
exports.samplePostRequest = {
94+
method:'POST',
95+
path:'/events',
96+
headers:{
97+
'Content-Type':'application/json',
98+
'Date':'Tue, 30 Oct 2012 19:48:27 GMT'
99+
}
100+
};

0 commit comments

Comments
 (0)