Skip to content

Commit

Permalink
Added master.log(type, data)
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Apr 20, 2011
1 parent 81e59c5 commit f2d6400
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
13 changes: 12 additions & 1 deletion example.js
Expand Up @@ -7,8 +7,19 @@ var cluster = require('cluster')
, log = require('./') , log = require('./')
, http = require('http'); , http = require('http');


var msgs = ['oh noes', 'boom', 'broken stuff'];

var server = http.createServer(function(req, res){ var server = http.createServer(function(req, res){
if (Math.random() > 0.9) throw new Error('omgz!'); // cluster will report uncaught exceptions
if (Math.random() > 0.9) throw new Error(msgs[Math.random() * msgs.length | 0]);
// we can also log arbitrary data
if (req.url == '/login') cluster.log('login', 'tj logged in');
// in many cases we would normally next(err)
// so perhaps within an error handler we would
// notify cluster of an exception, even if it
// did not crash the worker as an uncaughtException,
// but is still useful for us.
if (req.url == '/error') cluster.log(new Error('some error occurred'));
res.end('Hello World'); res.end('Hello World');
}); });


Expand Down
20 changes: 19 additions & 1 deletion lib/cluster-log.js
Expand Up @@ -51,9 +51,25 @@ function log(options, port, host) {
? 9999 ? 9999
: port; : port;


return function(master){ log.enableInWorker = true;

function log(master){
if (master.isWorker) {
master.log = function(type, data){
if (type instanceof Error) data = type, type = 'exception';
if (data instanceof Error) data = data.stack || data.message;
master.call('logEvent', type, data);
};
return;
}

var db = redis.createClient(); var db = redis.createClient();


// worker logs
master.logEvent = function(worker, type, data){
push(type, data);
};

// push a log item // push a log item


function push(type, msg) { function push(type, msg) {
Expand Down Expand Up @@ -99,4 +115,6 @@ function log(options, port, host) {
if (options.title) app.set('title', options.title); if (options.title) app.set('title', options.title);
app.listen(port, host); app.listen(port, host);
} }

return log;
}; };

0 comments on commit f2d6400

Please sign in to comment.