debug.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*!
  2. * Cluster - debug
  3. * Copyright (c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Enable verbose debugging output.
  8. *
  9. * @return {Function}
  10. * @api public
  11. */
  12. module.exports = function(options){
  13. options = options || {};
  14. // strip colors
  15. function color(text) {
  16. if (options.colors === false) return text.replace(/\033\[\d+m/g, '');
  17. return text
  18. }
  19. // logger
  20. var log = {
  21. debug: function(str){
  22. console.error(color(' \033[90mdebug - %s\033[0m'), str);
  23. },
  24. info: function(str){
  25. console.error(color(' info \033[90m- %s\033[0m'), str);
  26. },
  27. warning: function(str){
  28. console.error(color(' \033[33mwarning\033[0m \033[90m- %s\033[0m'), str);
  29. },
  30. error: function(str){
  31. console.error(color(' \033[31merror\033[0m \033[90m- %s\033[0m'), str);
  32. }
  33. };
  34. return function(master){
  35. // start
  36. master.on('start', function(){
  37. log.info('master started');
  38. });
  39. // closing
  40. master.on('closing', function(){
  41. log.info('shutting down');
  42. });
  43. // close
  44. master.on('close', function(){
  45. log.info('shutdown complete');
  46. });
  47. // killing workers
  48. master.on('kill', function(sig){
  49. log.warning('kill(' + (sig || 'SIGTERM') + ')');
  50. });
  51. // worker died
  52. master.on('worker killed', function(worker){
  53. if ('restarting' == master.state) return;
  54. log.warning('worker ' + worker.id + ' died');
  55. });
  56. // worker exception
  57. master.on('worker exception', function(worker, err){
  58. log.error('worker ' + worker.id + ' uncaught exception ' + err.message);
  59. });
  60. // worker is waiting on connections to be closed
  61. master.on('worker waiting', function(worker, connections){
  62. log.warning('worker ' + worker.id + ' waiting on ' + connections + ' connections');
  63. });
  64. // worker has timed out
  65. master.on('worker timeout', function(worker, timeout){
  66. log.warning('worker ' + worker.id + ' timed out after ' + timeout + 'ms');
  67. });
  68. // connection
  69. master.on('worker connected', function(worker){
  70. log.info('worker ' + worker.id + ' connected');
  71. });
  72. // removed
  73. master.on('worker removed', function(worker){
  74. log.info('worker ' + worker.id + ' removed');
  75. });
  76. // worker
  77. master.on('worker', function(worker){
  78. log.info('worker ' + worker.id + ' spawned');
  79. });
  80. // listening
  81. master.on('listening', function(){
  82. log.info('listening for connections');
  83. });
  84. // cyclic or immediate restart
  85. master.on('cyclic restart', function(){
  86. log.warning('cyclic restart detected, restarting in ' + master.options['restart timeout'] + 'ms');
  87. });
  88. // restart requested
  89. master.on('restarting', function(){
  90. log.info('restart requested');
  91. });
  92. // restart complete
  93. master.on('restart', function(){
  94. log.info('restart complete');
  95. });
  96. // exit
  97. process.on('exit', function(){
  98. log.debug('exit');
  99. });
  100. }
  101. };