Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added master.log(type, data)

  • Loading branch information...
commit f2d640082cfacd667b54054ee813cc54694465e3 1 parent 81e59c5
@tj tj authored
Showing with 31 additions and 2 deletions.
  1. +12 −1 example.js
  2. +19 −1 lib/cluster-log.js
View
13 example.js
@@ -7,8 +7,19 @@ var cluster = require('cluster')
, log = require('./')
, http = require('http');
+var msgs = ['oh noes', 'boom', 'broken stuff'];
+
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');
});
View
20 lib/cluster-log.js
@@ -51,9 +51,25 @@ function log(options, port, host) {
? 9999
: 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();
+ // worker logs
+ master.logEvent = function(worker, type, data){
+ push(type, data);
+ };
+
// push a log item
function push(type, msg) {
@@ -99,4 +115,6 @@ function log(options, port, host) {
if (options.title) app.set('title', options.title);
app.listen(port, host);
}
+
+ return log;
};
Please sign in to comment.
Something went wrong with that request. Please try again.