Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lmorchard committed Mar 9, 2012
0 parents commit 16ad6c5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.swp
*.swo
55 changes: 55 additions & 0 deletions firelogger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// See also: <https://github.com/darwin/firelogger/wiki>
module.exports = function (options) {

options = options || {};
var levels = options.levels ||
['debug', 'info', 'warning', 'error', 'critical'];

return function (req, res, mw_next) {
var messages = [],
orig_writeHead = res.writeHead;

res.log = function (data) {
var now = new Date();
data = data || {};
data.time = now.toTimeString();
data.timestamp = now.getTime() * 1000;
messages.push(data);
};

levels.forEach(function (level) {
res.log[level] = function (msg, data) {
data = data || {};
data.level = level;
data.message = msg;
res.log(data);
};
});

res.writeHead = function (status, headers) {

function wh_next() {
res.writeHead = orig_writeHead;
res.writeHead(status, headers);
}

var fl_ver = req.header('X-FireLogger');
if (!fl_ver) { return wh_next(); }

var d_logs = { logs: messages },
d_json = JSON.stringify(d_logs),
d_b64 = (new Buffer(d_json, 'utf-8')).toString('base64'),
d_lines = d_b64.match(/(.{1,75})/g),
uid = parseInt(Math.random() * 1000000, 16);

for (var i=0; i<d_lines.length; i++) {
res.header('FireLogger-' + uid + '-' + i, d_lines[i]);
}

wh_next();
};

mw_next();
};

};
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"author": "Les Orchard <me@lmorchard.com> (http://lmorchard.com/)",
"name": "firelogger",
"description": "Middleware for express/connect that supports the FireLogger protocol",
"version": "0.0.1",
"homepage": "http://github.com/lmorchard/node-firelogger/",
"repository": {
"type": "git",
"url": "git://github.com/lmorchard/node-firelogger.git"
},
"main": "firelogger.js",
"engines": {
"node": "~0.6.10"
},
"dependencies": {},
"devDependencies": {}
}

0 comments on commit 16ad6c5

Please sign in to comment.