Keine Beschreibung

logger.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env node
  2. var logger = (function(){
  3. /**********************
  4. * Internal properties
  5. *********************/
  6. var logger, context, hasColors = true;
  7. try{
  8. require('colors');
  9. }catch(e){
  10. hasColors = false;
  11. }
  12. function prefixMsg(msg){
  13. return context.opts.plugin.id+": "+msg;
  14. }
  15. /************
  16. * Public API
  17. ************/
  18. logger = {
  19. init: function(ctx){
  20. context = ctx;
  21. },
  22. dump: function (obj){
  23. if(context.cmdLine.match("--debug") || context.cmdLine.match("--dump")) {
  24. console.log("DUMP: "+require('util').inspect(obj));
  25. }
  26. },
  27. debug: function(msg){
  28. if(context.cmdLine.match("--debug")){
  29. msg = "DEBUG: " + msg;
  30. console.log(msg);
  31. }
  32. },
  33. verbose: function(msg){
  34. if(context.opts.verbose || context.cmdLine.match("--verbose") || context.cmdLine.match("--debug")){
  35. msg = prefixMsg(msg);
  36. if(hasColors){
  37. console.log(msg.green);
  38. }else{
  39. console.log(msg);
  40. }
  41. }
  42. },
  43. log: function(msg){
  44. msg = prefixMsg(msg);
  45. if(hasColors){
  46. console.log(msg.white);
  47. }else{
  48. console.log(msg);
  49. }
  50. },
  51. info: function(msg){
  52. msg = prefixMsg(msg);
  53. if(hasColors){
  54. console.log(msg.blue);
  55. }else{
  56. console.info(msg);
  57. }
  58. },
  59. warn: function(msg){
  60. msg = prefixMsg(msg);
  61. if(hasColors){
  62. console.log(msg.yellow);
  63. }else{
  64. console.warn(msg);
  65. }
  66. },
  67. error: function(msg){
  68. msg = prefixMsg(msg);
  69. if(hasColors){
  70. console.log(msg.red);
  71. }else{
  72. console.error(msg);
  73. }
  74. }
  75. };
  76. return logger;
  77. })();
  78. module.exports = function(ctx){
  79. logger.init(ctx);
  80. return logger;
  81. };