Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. const LOG_PATH = "D:/nodejs/Logs/";
  2. const fs = require('fs');
  3. const qu = require('./log-queue');
  4.  
  5. class FileLogger {
  6. constructor(_logpath, _interval) {
  7. this.logPath = _logpath || LOG_PATH;
  8. this.interval = _interval || 5000;
  9. this.logs = new qu();
  10. console.log("isEmpty %d", this.logs.isEmpty());
  11.  
  12. this.handler = setInterval(this.logPeriodically.bind(this), this.interval);
  13. }
  14.  
  15. stopPeriodicLogging () {
  16. clearInterval(this.handler);
  17. }
  18.  
  19. logPeriodically() {
  20. if (this.logs.isEmpty()) {
  21. console.log('Nothing to log now');
  22. return;
  23. }
  24. const dt = new Date();
  25. const logName = dt.toLocaleString().slice(0, -9);
  26. const logFile = this.logPath + logName + ".log";
  27. console.log(dt.toISOString, " ", this.logs);
  28. let ws;
  29. try {
  30. ws = fs.createWriteStream(logFile, {
  31. flags: "a",
  32. encoding:'utf8',
  33. mode: 0o644,
  34. autoClose: true
  35. });
  36. ws.on('open',()=> {
  37. while (!this.logs.isEmpty()) {
  38. try{let msg = this.logs.dequeue();
  39. // console.log(`msg to ${logFile} : ${msg}`);
  40.  
  41. ws.write(msg, 'utf8', (err) => {
  42. if (err) { console.log(err.message);
  43.  
  44. }
  45. if (this.logs.isEmpty()) {
  46. // console.log(`need to end stream here`);
  47. ws.close();
  48. }
  49. }) ;
  50. } catch (error){
  51. console.log("fs error", error.message);
  52. }
  53. }
  54. });
  55. ws.on('close', () => {
  56. console.log('ws closed');
  57. })
  58. } catch (error) {
  59. console.log(error.message);
  60. } finally {
  61. ;
  62. }
  63. // old version.....
  64. // while (!this.logs.isEmpty()) {
  65. // try{
  66. // fs.appendFile(logFile, this.logs.dequeue(), (err) => {
  67. // if (err) { console.log(err.message);
  68. // }
  69. // }) ;
  70. // } catch (error){
  71. // console.log("fs error", error.message);
  72. // }
  73.  
  74. // }
  75. // return;
  76. };
  77.  
  78. logIt(data) {
  79. const dt = new Date();
  80. const message = "\r\n-" + dt.toISOString() + "-" + data;
  81. console.log(dt.toISOString() + "-" + data);
  82. try{
  83. this.logs.enqueue(message);
  84. } catch (error) {
  85. console.log("logQueue error : ", error.message);
  86. }
  87.  
  88. }
  89. }
  90.  
  91. module.exports = FileLogger;
  92. /*==================================================================*/
  93. /*====================log-queue.js==================================*/
  94. /*==================================================================*/
  95.  
  96. class LogQueue {
  97.  
  98. constructor(){
  99. // if (!LogQueue.instance){
  100. // LogQueue.instance = this;
  101. // this.collection =[];
  102. // }
  103. // return LogQueue.instance;
  104. this.collection = [];
  105. }
  106.  
  107. enqueue(log) {
  108. this.collection.push(log);
  109. }
  110.  
  111. dequeue() {
  112. return this.collection.shift();
  113. }
  114.  
  115. isEmpty() {
  116. return (this.collection.length === 0);
  117. }
  118.  
  119. length() {
  120. return this.collection.length;
  121. }
  122. front() {
  123. return this.collection[0];
  124. }
  125. }
  126.  
  127. // const log = new LogQueue();
  128. // Object.freeze(log);
  129.  
  130. module.exports = LogQueue;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement