Node-Red configuration
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

logger.js 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const loggerStatusActive={fill: 'yellow',shape:'ring',text:'Logging'};
  2. const loggerStatusOff={fill: 'green',shape:'ring',text:''};
  3. const { inspect } = require('util');
  4. function Logger(label="***",active=true,count=111,msg) {
  5. this.consoleFunction=console.log;
  6. this.sendFunction=this.sendConsole;
  7. this.type="debug";
  8. if(label instanceof Object) Object.assign(this,label);
  9. else this.label=label;
  10. if(this.active==undefined) this.active=active;
  11. if(this.count==undefined) this.count=count;
  12. this.set(this.active,this.count);
  13. return msg?this.sendInfo(msg):this;
  14. };
  15. Logger.prototype.objectDump=function(o) {
  16. return o?this.send(inspect(o, {showHidden: true, depth: null})):this;
  17. };
  18. Logger.prototype.send=function(message,type,node,sendFunction=this.sendFunction) {
  19. if(--this.count) {
  20. sendFunction.apply(this,[(message instanceof Object ? JSON.stringify(message) : message),type,node]);
  21. } else {
  22. this.setOff();
  23. }
  24. return this;
  25. };
  26. Logger.prototype.sendConsole=function(message,type=this.type,consoleFunction=this.consoleFunction) {
  27. const ts = (new Date().toString()).split(' ');
  28. consoleFunction.apply(this,[([parseInt(ts[2], 10), ts[1], ts[4]].join(" ") + " - ["+type+"] "+this.label+" "+message)]);
  29. return this;
  30. };
  31. Logger.prototype.sendDebug=function(message,values) {
  32. if(values) return this.send(Object.assign({},{label:message},values),"debug");
  33. return this.send(message,"debug");
  34. }
  35. Logger.prototype.debug=Logger.prototype.sendDebug;
  36. Logger.prototype.sendError=function(message) {
  37. return this.send(message,"error");
  38. }
  39. Logger.prototype.error=Logger.prototype.sendError;
  40. Logger.prototype.sendErrorAndDump=function(message,o,ex) {
  41. return this.sendError(message).objectDump(o).stackDump(ex);
  42. }
  43. Logger.prototype.sendErrorAndStackDump=function(message,ex) {
  44. return this.sendError(message).stackDump(ex);
  45. }
  46. Logger.prototype.sendInfo=function(message) {
  47. return this.send(message,"info");
  48. }
  49. Logger.prototype.info=Logger.prototype.sendInfo;
  50. Logger.prototype.sendNode=function(message,node=this.node,type=this.noderedLogType) {
  51. return node[type](message);
  52. }
  53. Logger.prototype.sendWarn=function(message) {
  54. return this.send(message,"warn");
  55. }
  56. Logger.prototype.sendWarning=Logger.prototype.sendWarn;
  57. Logger.prototype.warn=Logger.prototype.sendWarn;
  58. Logger.prototype.warning=Logger.prototype.sendWarn;
  59. Logger.prototype.setNodeStatus=function(node) {
  60. if(node) this.node=node;
  61. if(this.node==null) throw Error("No node set");
  62. this.showNodeStatus();
  63. return this;
  64. };
  65. Logger.prototype.set=function(active,count) {
  66. if(count!==undefined) {
  67. this.count=count;
  68. this.countDefault=count
  69. }
  70. if(active!==undefined) this.active=active;
  71. this.showNodeStatus();
  72. this.sendConsole('logging turning '+(this.active?"on logging next " + this.count + " log points":"off"));
  73. return this;
  74. };
  75. Logger.prototype.setOff=function() {
  76. return this.set(false);
  77. };
  78. Logger.prototype.setOn=function(count=this.countDefault) {
  79. return this.set(true,count);
  80. };
  81. Logger.prototype.showNodeStatus=function() {
  82. if(this.node) this.node.status(this.active?loggerStatusActive:loggerStatusOff);
  83. };
  84. Logger.prototype.stackDump=function(ex) {
  85. if(ex) console.log(ex.stack)
  86. else console.trace();
  87. return this;
  88. };
  89. module.exports = Logger;