logs.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. const C = require('../config');
  3. const fs=require('fs');
  4. const path = require('path');
  5. const _ = require('underscore');
  6. _.str = require('underscore.string');
  7. _.v = require('validator');
  8. var util = require("util");
  9. var co = require('co');
  10. module.exports = function () {
  11. let that = this;
  12. // 日志文件夹路径
  13. this.log_path = path.resolve(__dirname, '../logs');
  14. // qn curl log路径
  15. this.qn_curl_log_path = this.log_path + '/qn_curl/curl_qn_%s.log';
  16. // qn curl 临时扣费失败路径
  17. this.qn_curl_interim_consume_fail_log = this.log_path + '/qn_curl_interim_consume_fail.log';
  18. this.level = {debug: 'debug', info: 'info', warning: 'warning', error: 'error'};
  19. /**
  20. * 格式化内容
  21. * @param data object 例如{'name':'xxx'}
  22. * @returns {string}
  23. */
  24. this.formatData = function (data) {
  25. let cur_time = new Date();
  26. let data_str = cur_time.format('yyyy-MM-dd HH:mm:ss') + ' ';
  27. for(let key in data) {
  28. let value = typeof data[key] == 'object' ? JSON.stringify(data[key]) : data[key];
  29. data_str += key + ':' + value + '';
  30. }
  31. data_str += '\n';
  32. return data_str;
  33. };
  34. /**
  35. * 追加方式写入文件
  36. * @param file_path 文件路径
  37. * @param data object 例如{'name':'xxx'}
  38. * @returns {}
  39. */
  40. this.writeFile = function (file_path, data) {
  41. let data_str = that.formatData(data);
  42. fs.appendFileSync(file_path, data_str);
  43. };
  44. this.isNull = function(obj) {
  45. if (obj == null || typeof(obj) == "undefined" || obj.length == 0) {
  46. return true;
  47. }
  48. if(typeof(obj) == 'object' && !(obj instanceof Date) && Object.keys(obj).length == 0) {
  49. return true;
  50. }
  51. return false;
  52. };
  53. this.syncAddLogs = function (file_prefix,data,level=that.level.info) {
  54. // if (file_prefix == "debug/debug") console.log(data);
  55. if (this.isNull(file_prefix)) file_prefix = "sys_log";
  56. if (!this.isNull(C.NO_LOG_DEBUG) && file_prefix.indexOf("debug/debug") >= 0) return;
  57. let cur_time = new Date();
  58. //let content = typeof data == 'object' ? util.inspect(data,{depth:null}) : data;
  59. let content = typeof data == 'object' ? JSON.stringify(data) : data;
  60. let value = _.str.vsprintf("%s %s\n", [cur_time.format('yyyy-MM-dd HH:mm:ss'),content]);
  61. let relative_file_path = _.str.vsprintf("%s_%s_%s.log", [file_prefix,cur_time.format('yyyy-MM-dd'),level]);
  62. if(!fs.existsSync(this.log_path+"/"+relative_file_path)) {
  63. let dirs_list = file_prefix.split("/");
  64. dirs_list.pop();
  65. let dirs = "";
  66. for (let i = 0;i < dirs_list.length;++i) {
  67. if (this.isNull(dirs_list[i])) continue;
  68. dirs += "/" +dirs_list[i];
  69. if (!fs.existsSync(this.log_path+dirs)) {
  70. console.log(this.log_path+dirs);
  71. fs.mkdirSync(this.log_path+dirs);
  72. }
  73. }
  74. fs.writeFile(this.log_path+"/"+relative_file_path, '');
  75. }
  76. fs.appendFileSync(this.log_path+"/"+relative_file_path, value);
  77. };
  78. this.addLogs = function (file_prefix,data,level=that.level.info) {
  79. that.syncAddLogs(file_prefix,data,level);
  80. };
  81. };