logger.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const fs = require("fs-extra");
  2. const path = require('path');
  3. const dateformat = require('dateformat');
  4. const localpaths = require('./localpaths.js')
  5. const os = require('os');
  6. fs.ensureDirSync(path.join(localpaths.user, 'logs'))
  7. const logsLocation = path.join(localpaths.user, 'logs', `${dateformat(new Date(), 'yyyy-mm-dd-HH.MM.ss')}.txt`)
  8. fs.appendFileSync(logsLocation, `${os.platform()} - ${os.type()} ${os.release()} ${os.arch()}\r\n\r\n`);
  9. fs.readdir(path.join(localpaths.user, 'logs'), function (err, files) {
  10. if (err) throw err;
  11. else {
  12. var logs = [];
  13. files.forEach(function (file) {
  14. logs.push(file.substring(0, file.length-4));
  15. });
  16. logs.sort()
  17. if (logs.length>5){
  18. for (let i=0; i<logs.length-5; i++){
  19. fs.unlinkSync(path.join(localpaths.user, 'logs', logs[i]+".txt"));
  20. }
  21. }
  22. }
  23. });
  24. function removeColors(string){
  25. return string.replace(/\x1b\[\d+m/g,"");
  26. }
  27. function debug(message){
  28. var str = "[\x1b[32mDebug\x1b[0m] "+message;
  29. console.log(str);
  30. //fs.appendFileSync(logsLocation, removeColors(str)+"\r\n");
  31. return;
  32. }
  33. function info(message){
  34. var str = "[\x1b[35mInfo\x1b[0m] "+message;
  35. console.log(str);
  36. fs.appendFileSync(logsLocation, removeColors(str)+"\r\n");
  37. return;
  38. }
  39. function warn(message){
  40. var str = "[\x1b[33mWarning\x1b[0m] "+message;
  41. console.log(str);
  42. fs.appendFileSync(logsLocation, removeColors(str)+"\r\n");
  43. return;
  44. }
  45. function error(message){
  46. var str = "[\x1b[31mError\x1b[0m] "+message;
  47. console.log(str);
  48. fs.appendFileSync(logsLocation, removeColors(str)+"\r\n");
  49. return;
  50. }
  51. module.exports.debug = debug;
  52. module.exports.info = info;
  53. module.exports.warn = warn;
  54. module.exports.error = error;
  55. module.exports.logPath = logsLocation;