Yeah, but don't log to disk if you care about performance,
which I'm guessing you do since you're using cluster :)

Here's why -- https://medium.com/node-js-javascript/37a93d4e0013.
You may also want to look at this -- https://github.com/hij1nx/net-log

--
https://twitter.com/hij1nx
https://github.com/hij1nx

On Tuesday, April 29, 2014 7:31:38 AM UTC-4, Alexey Petrushin wrote:
>
> Adam Wiggins, one of creator of Heroky wrote interested post about 
> logging, you may find it interesting
> http://adam.heroku.com/past/2011/4/1/logs_are_streams_not_files/
>
> On Monday, 28 April 2014 14:48:46 UTC+4, Jose Luis Rivas wrote:
>>
>> I would suggest you to not write directly but redirect output to a file. 
>>
>> node app.js > file.log 
>>
>> keep it simple. 
>>
>> On 4/28/14, 5:45 AM, gen chen wrote: 
>> > I am now working on a node.js project based on cluster.  I got stuck on 
>> > the logging.  After doing some research, I worked out a solution. here 
>> > is it. i don't know if it is a good idea.  The idea is like this.  only 
>> > master process can wirte to the log file, if the current process is a 
>> > worker, then it send a log message to the master and then write to the 
>> > log file while the master can directly write to the log file. this can 
>> > avoid multiple process open and write to a same file. 
>> > 
>> > var util = require('util'); 
>> > var fs = require('fs'); 
>> > var cluster = require('cluster'); 
>> > 
>> > var logger = module.exports; 
>> > 
>> > var levels  = ['debug', 'info', 'warn', 'error', 'fatal']; 
>> > var logLevel = 'debug'; 
>> > 
>> > var logfile = null; 
>> > var errorLogfile  = null; 
>> > 
>> > 
>> > if(cluster.isMaster){ 
>> > 
>> >     logfile = fs.createWriteStream('debug.log', {flags:'a'}); 
>> >     errorLogfile = fs.createWriteStream('error.log', {flags:'a'}); 
>> > 
>> >     cluster.on('online', function(worker){ 
>> >         //collect log message from child and write to logfile. 
>> >         worker.on('message', function(msg){ 
>> >             if(msg.type == 'logging') { 
>> >                 var level = msg.data.level; 
>> >                 var logStr = msg.data.msg; 
>> >                 if(levels.indexOf(level) >= levels.indexOf('error')){ 
>> >                     errorLogfile.write(logStr + '\n'); 
>> >                 }else{ 
>> >                     logfile.write(logStr + '\n'); 
>> >                 } 
>> >             } 
>> >         }); 
>> >     }); 
>> > } 
>> > 
>> > 
>> > function log(level, args){ 
>> > 
>> >     if(levels.indexOf(level) < levels.indexOf(logLevel)) return; 
>> > 
>> >     var args = Array.prototype.slice.call(args); 
>> > 
>> >     args = args.map(function(a){ 
>> >         if(typeof a !== 'string') 
>> >             return JSON.stringify(a); 
>> >         else return a; 
>> >     }); 
>> >     var msg = util.format.apply(null, args); 
>> > 
>> >     var out = []; 
>> >     out.push(new Date()); 
>> >     out.push('[' + level.toUpperCase() + ']'); 
>> >     out.push(msg); 
>> > 
>> >         
>> >     if(cluster.isMaster){ 
>> > 
>> >         //write directly to the log file 
>> >         if(levels.indexOf(level) >= levels.indexOf('error')){ 
>> >             errorLogfile.write(out.join(' ') + '\n'); 
>> >         }else{ 
>> >             logfile.write(out.join(' ') + '\n'); 
>> >         } 
>> > 
>> >     }else{ 
>> > 
>> >         //send to master 
>> >         cluster.worker.process.send({ 
>> >             type : 'logging', 
>> >             data : { 
>> >                 level : level, 
>> >                 msg : out.join(' ') 
>> >             } 
>> >         }); 
>> >     } 
>> > 
>> > } 
>> > 
>> > 
>> > logger.debug = function(){log('debug', arguments);} 
>> > logger.info = function(){log('info', arguments);} 
>> > logger.warn = function(){log('warn', arguments);} 
>> > logger.error = function(){log('error', arguments);} 
>> > logger.fatal = function(){log('fatal', arguments);} 
>> > 
>> > 
>> > 
>> > -- 
>> > -- 
>> > Job Board: http://jobs.nodejs.org/ 
>> > Posting guidelines: 
>> > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines 
>> > You received this message because you are subscribed to the Google 
>> > Groups "nodejs" group. 
>> > To post to this group, send email to nod...@googlegroups.com 
>> > To unsubscribe from this group, send email to 
>> > nodejs+un...@googlegroups.com 
>> > For more options, visit this group at 
>> > http://groups.google.com/group/nodejs?hl=en?hl=en 
>> > 
>> > --- 
>> > You received this message because you are subscribed to the Google 
>> > Groups "nodejs" group. 
>> > To unsubscribe from this group and stop receiving emails from it, send 
>> > an email to nodejs+un...@googlegroups.com 
>> > <mailto:nodejs+un...@googlegroups.com>. 
>> > For more options, visit https://groups.google.com/d/optout. 
>>
>> -- 
>> Jose Luis Rivas - http://joseluisrivas.net 
>> Venezuela - GPG: 0xB9AC8C43 
>>
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to nodejs+unsubscr...@googlegroups.com.
To post to this group, send email to nodejs@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/01359a4a-ef2d-46b4-ae0d-f3505ac32f98%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to