logger.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*!
  2. * Cluster - logger
  3. * Copyright (c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var fs = require('fs')
  10. , Log = require('log')
  11. , mkdir = require('mkdirp').mkdirp;
  12. /**
  13. * Enable stdout / stderr logs for both the master
  14. * process, as well as workers.
  15. *
  16. * These output to the given `dir`, or `./logs`
  17. * relative to the server's file.
  18. *
  19. * Examples:
  20. *
  21. * // log to ./logs
  22. * engine(server)
  23. * .use(engine.logger())
  24. * .listen(3000);
  25. *
  26. * // log to ./app/logs
  27. * engine(server)
  28. * .use(engine.logger('./app/logs'))
  29. * .listen(3000);
  30. *
  31. * // log to /var/log/node
  32. * engine(server)
  33. * .use(engine.logger('/var/log/node'))
  34. * .listen(3000);
  35. *
  36. * @param {String} dir
  37. * @param {Number} level
  38. * @return {Function}
  39. * @api public
  40. */
  41. module.exports = function(dir, level){
  42. return function(master){
  43. dir = master.resolve(dir || 'logs');
  44. mkdir(dir, 0755, function(err){
  45. if (err) throw err;
  46. // master log
  47. var stream = fs.createWriteStream(dir + '/master.log', { flags: 'a' });
  48. var log = master.log = new Log(level || Log.INFO, stream);
  49. // master events
  50. master.on('start', function(){
  51. log.info('master started');
  52. });
  53. // master is shutting down
  54. master.on('closing', function(){
  55. log.warning('shutting down master');
  56. });
  57. // master has closed and performed cleanup
  58. master.on('close', function(){
  59. log.info('shutdown complete');
  60. });
  61. // sending signal to all workers
  62. master.on('kill', function(sig){
  63. log.warning('sent kill(%s) to all workers', sig);
  64. });
  65. // worker was killed
  66. master.on('worker killed', function(worker){
  67. if ('restarting' == master.state) return;
  68. log.error('worker %s died', worker.id);
  69. });
  70. // worker exception
  71. master.on('worker exception', function(worker, err){
  72. log.error('worker %s uncaught exception %s', worker.id, err.message);
  73. });
  74. // worker is waiting on connections to be closed
  75. master.on('worker waiting', function(worker, connections){
  76. log.info('worker %s waiting on %s connections', worker.id, connections);
  77. });
  78. // worker has timed out
  79. master.on('worker timeout', function(worker, timeout){
  80. log.warning('worker %s timed out after %sms', worker.id, timeout);
  81. });
  82. // worker connected to master
  83. master.on('worker connected', function(worker){
  84. log.debug('worker %s connected', worker.id);
  85. });
  86. // cyclic or immediate restart
  87. master.on('cyclic restart', function(){
  88. log.warning('cyclic restart detected, restarting in %sms'
  89. , master.options['restart timeout']);
  90. });
  91. // restart requested
  92. master.on('restarting', function(){
  93. log.info('restart requested');
  94. });
  95. // restart complete
  96. master.on('restart', function(){
  97. log.info('restart complete');
  98. });
  99. // repl socket connection established
  100. master.on('repl socket', function(sock){
  101. var from = sock.remoteAddress
  102. ? 'from ' + sock.remoteAddress
  103. : '';
  104. sock.on('connect', function(){
  105. log.info('repl connection %s', from);
  106. });
  107. sock.on('close', function(){
  108. log.info('repl disconnect %s', from);
  109. });
  110. });
  111. // override fds
  112. master.customFds = [-1, -1];
  113. // children
  114. master.on('worker', function(worker){
  115. var proc = worker.proc;
  116. log.info('spawned worker ' + worker.id);
  117. // worker log streams
  118. var access = fs.createWriteStream(dir + '/workers.access.log', { flags: 'a' })
  119. , error = fs.createWriteStream(dir + '/workers.error.log', { flags: 'a' });
  120. // redirect stdout / stderr
  121. proc.stdout.pipe(access);
  122. proc.stderr.pipe(error);
  123. });
  124. });
  125. }
  126. };