logs.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. 'use strict';
  2. const C = require('../config');
  3. const F = require('../common/function');
  4. const fs=require('fs');
  5. const path = require('path');
  6. const _ = require('underscore');
  7. _.str = require('underscore.string');
  8. _.v = require('validator');
  9. var logs_obj = require('../libs/logs.js');
  10. var logs = new logs_obj();
  11. module.exports = function (app, commonManager) {
  12. let that = this;
  13. // 日志文件夹路径
  14. this.log_path = path.resolve(__dirname, '../logs');
  15. this.qn_curl_log_dir_path = this.log_path + '/qn_curl';
  16. // qn curl log路径
  17. this.qn_curl_log_path = this.qn_curl_log_dir_path + '/curl_qn_%s.log';
  18. // qn curl error log路径
  19. this.qn_curl_error_log_path = this.qn_curl_log_dir_path + '/curl_qn_error_%s.log';
  20. // qn curl 临时扣费失败路径
  21. this.qn_curl_interim_consume_fail_log = this.log_path + '/qn_curl_interim_consume_fail.log';
  22. // 表数据转移失败路径
  23. this.collect_move_table_fail_log = this.log_path + '/collect_move_table_fail.log';
  24. this.level = {debug: 'debug', info: 'info', warning: 'warning', error: 'error'};
  25. /**
  26. * 格式化内容
  27. * @param data object 例如{'name':'xxx'}
  28. * @returns {string}
  29. */
  30. this.formatData = function (data) {
  31. let data_str = F.fDatetime('yyyy-MM-dd HH:mm:ss') + ' ';
  32. for(let key in data) {
  33. let value = typeof data[key] == 'object' ? JSON.stringify(data[key]) : data[key];
  34. data_str += key + ':' + value + ' ';
  35. }
  36. data_str += '\n';
  37. return data_str;
  38. };
  39. /**
  40. * 追加方式写入文件
  41. * @param file_path 文件路径
  42. * @param data object 例如{'name':'xxx'}
  43. * @returns {}
  44. */
  45. this.writeFile = function (file_path, data) {
  46. let data_str = that.formatData(data);
  47. fs.appendFileSync(file_path, data_str);
  48. };
  49. /**
  50. * 添加qn curl记录
  51. * @param url string
  52. * @param params 参数object
  53. * @param result curl返回结果object
  54. * @returns {}
  55. */
  56. this.addQnCurlLogs = function (url, params, result) {
  57. if(C.is_log_qn_curl != true) return ;
  58. if(!fs.existsSync(this.qn_curl_log_dir_path)) {
  59. fs.mkdirSync(this.qn_curl_log_dir_path);
  60. }
  61. let log_path = _.str.vsprintf(this.qn_curl_log_path, [F.fDatetime('yyyyMMdd')]);
  62. if(!fs.existsSync(log_path)) {
  63. fs.writeFile(log_path, '');
  64. }
  65. that.writeFile(log_path, {
  66. 'url':url,
  67. 'params':params,
  68. 'result':result
  69. });
  70. };
  71. /**
  72. * 添加qn curl失败记录
  73. * @param url string
  74. * @param params 参数object
  75. * @param result curl返回结果object
  76. * @returns {}
  77. */
  78. this.addQnCurlErrorLogs = function (url, params, result) {
  79. let log_path = _.str.vsprintf(this.qn_curl_error_log_path, [F.fDatetime('yyyyMMdd')]);
  80. if(!fs.existsSync(this.qn_curl_log_dir_path)) {
  81. fs.mkdirSync(this.qn_curl_log_dir_path);
  82. }
  83. if(!fs.existsSync(log_path)) {
  84. fs.writeFile(log_path, '');
  85. }
  86. that.writeFile(log_path, {
  87. 'url':url,
  88. 'params':params,
  89. 'result':result
  90. });
  91. };
  92. this.addLogs = function (file_prefix,data,level=that.level.info) {
  93. logs.addLogs(file_prefix,data,level);
  94. };
  95. /**
  96. * 添加qn 临时扣费失败记录
  97. * @param params 参数object
  98. * @param result curl返回结果object
  99. * @returns {}
  100. */
  101. this.addQnInterimConsumeFailLogs = function (params, result) {
  102. let log_path = this.qn_curl_interim_consume_fail_log;
  103. if(!fs.existsSync(log_path)) {
  104. fs.writeFile(log_path, '');
  105. }
  106. that.writeFile(log_path, {
  107. 'params':params,
  108. 'result':result
  109. });
  110. };
  111. /**
  112. * 添加男士进入房间流程log
  113. * @param title 说明
  114. * @param data
  115. * @returns {}
  116. */
  117. this.roomManInLogs = function (title, data) {
  118. let content = {'title':title};
  119. if(!F.isNull(data)) {
  120. for (let key in data) {
  121. content[key] = data[key];
  122. }
  123. }
  124. that.addLogs('room_man_in/process', content, that.level.debug);
  125. that.addLogs('sys/sys', content, that.level.debug);
  126. };
  127. /**
  128. * 扣费定时器报错日志
  129. * @param title 说明
  130. * @param data
  131. * @returns {}
  132. */
  133. this.timerErrLogs = function (title, data) {
  134. let content = {'title':title};
  135. if(!F.isNull(data)) {
  136. for (let key in data) {
  137. content[key] = data[key];
  138. }
  139. }
  140. that.addLogs('room_man_in/timer_err', content, that.level.debug);
  141. that.addLogs('sys/sys', content, that.level.debug);
  142. };
  143. /**
  144. * 扣费定时器日志
  145. * @param title 说明
  146. * @param data
  147. * @returns {}
  148. */
  149. this.timerConsumeLogs = function (title, data) {
  150. let content = {'title':title};
  151. if(!F.isNull(data)) {
  152. for (let key in data) {
  153. content[key] = data[key];
  154. }
  155. }
  156. that.addLogs('room_man_in/timer_consume', content, that.level.debug);
  157. that.addLogs('sys/sys', content, that.level.debug);
  158. };
  159. /**
  160. * 添加男士异常离开直播间流程log
  161. * @param title 说明
  162. * @param data
  163. * @returns {}
  164. */
  165. this.leaveRoomExceptionLogs = function (title, data) {
  166. let content = {'title':title};
  167. if(!F.isNull(data)) {
  168. for (let key in data) {
  169. content[key] = data[key];
  170. }
  171. }
  172. console.log(title);console.log(content);
  173. that.addLogs('room_man_leave_exception/process', content, that.level.debug);
  174. };
  175. /**
  176. * 获取logs日志列表
  177. * @param title 指定文件夹
  178. * @returns {*}
  179. */
  180. this.getLogsList = function (title = '') {
  181. // let logs_path = path.resolve(__dirname, '../logs/');
  182. let logs_path = 'logs/';
  183. let folders = fs.readdirSync(logs_path);
  184. let down_files = new Array();
  185. let file_path;
  186. folders.forEach(function(file, index) {
  187. file_path = logs_path + '/' + file;
  188. if(fs.statSync(file_path).isDirectory()) {
  189. let files = fs.readdirSync(file_path);
  190. down_files.push({'prefix':encodeURI(file_path), 'name':file, 'is_folder':true, 'sub_file':files});
  191. }else {
  192. down_files.push({'prefix':encodeURI(logs_path), 'name':file, 'is_folder':false, 'sub_file':[]});
  193. }
  194. });
  195. return down_files;
  196. };
  197. /**
  198. * 读取文件内容
  199. * @param filename
  200. * @returns {*}
  201. */
  202. this.readFile = function (filename) {
  203. let res;
  204. return new Promise(function(resolve, reject) {
  205. fs.readFile(filename, function(err, data) {
  206. if(err) {
  207. res = {'result':false};
  208. }else {
  209. res = {'result':true, 'data':data};
  210. }
  211. resolve(res);
  212. });
  213. });
  214. };
  215. /**
  216. * 添加表数据转移失败日志
  217. * @param params
  218. * @param errno
  219. * @param err_stack
  220. * @returns {}
  221. */
  222. this.addCollectMoveTableErrorLogs = function (params, errno, err_stack) {
  223. let log_path = this.collect_move_table_fail_log;
  224. if(!fs.existsSync(log_path)) {
  225. fs.writeFile(log_path, '');
  226. }
  227. that.writeFile(log_path, {
  228. 'params':params,
  229. 'errno':errno,
  230. 'err_stack':err_stack
  231. });
  232. };
  233. /**
  234. * 获取文件列表
  235. * @param title 指定文件夹
  236. * @returns {*}
  237. */
  238. this.getFileList = function (title = '') {
  239. let folder_map = [
  240. 'common',
  241. 'config',
  242. 'libs',
  243. 'manager',
  244. 'model',
  245. 'routes'
  246. ];
  247. let down_files = new Array();
  248. for (var i = 0; i < folder_map.length; i++) {
  249. let folder_path = folder_map[i];
  250. let files = fs.readdirSync(folder_path);
  251. down_files.push({'prefix':encodeURI(folder_path), 'name':folder_map[i], 'is_folder':true, 'sub_file':files});
  252. }
  253. return down_files;
  254. };
  255. /**
  256. * 获取日志内容一行长度
  257. * @param content
  258. * @param find_str
  259. * @returns {int}
  260. */
  261. this.getLineLengthFromBuffer = function (content, find_str = F.fDatetime('yyyy-MM-dd')) {
  262. let index = -1; // 目标索引
  263. let pre_index_char; // 上一个字符
  264. let offset = 1; // 搜索位置偏移量 - 避免搜索第一个
  265. let count = 0; // 计数器 - 避免死锁
  266. while(true && count < 10) {
  267. index = content.indexOf(find_str, offset);
  268. if(index == -1) break;
  269. pre_index_char = content.substr(index - 1, 1);
  270. if(pre_index_char == '\n') break;
  271. offset = index + 1;
  272. count++;
  273. }
  274. return index;
  275. };
  276. /**
  277. * 读取文件内容
  278. * @param filename
  279. * @returns {*}
  280. */
  281. this.readFileContent = function (fs, fd, buffer, start, length, offset = null) {
  282. let res;
  283. return new Promise(function(resolve, reject) {
  284. fs.read(fd, buffer, start, length, offset, function (err, byte, read_buffer) {
  285. if(err) {
  286. console.log('errrrr');
  287. console.log(err);
  288. res = {'result':false};
  289. }else {
  290. res = {'result':true, 'buffer':read_buffer, 'byte':byte};
  291. }
  292. resolve(res);
  293. });
  294. });
  295. };
  296. /**
  297. * 获取fd
  298. * @param filename
  299. * @returns {*}
  300. */
  301. this.getFdFromFs = function (fs_param, path) {
  302. let res;
  303. return new Promise(function(resolve, reject) {
  304. fs_param.open(path, 'r', function (err, fd) {
  305. if(err) {
  306. res = {'result':false};
  307. }else {
  308. res = {'result':true, 'fd':fd};
  309. }
  310. resolve(res);
  311. });
  312. });
  313. };
  314. /**
  315. * 搜索日志内容
  316. * @param path
  317. * @param find_arr 查询的字符串数组
  318. * @param line_str 行开始的标识
  319. * @returns {*}
  320. */
  321. this.searchLogsContent = function* (path, find_arr, line_str) {
  322. // 重新排序搜素数组
  323. find_arr.sort(function(v1, v2) {
  324. return v1 < v2;
  325. });
  326. let fd_res = yield that.getFdFromFs(fs, path);
  327. let buffer_size = 1000;
  328. let buffer = new Buffer(buffer_size);
  329. let content_buffer;
  330. let content;
  331. let length;
  332. let pre_content = ''; // 上一次剩余字符串
  333. let content_array;
  334. let search_result = new Array();
  335. while(true) {
  336. content_array = new Array();
  337. content_buffer = yield that.readFileContent(fs, fd_res.fd, buffer, 0, buffer_size);
  338. if(content_buffer.result == false) break;
  339. if(content_buffer.byte == 0) break;
  340. content = content_buffer.buffer.slice(0, content_buffer.byte).toString();
  341. content = pre_content + content;
  342. pre_content = '';
  343. while(true) {
  344. length = that.getLineLengthFromBuffer(content, line_str);
  345. if(length == -1) {
  346. pre_content = content.substr(0);
  347. break;
  348. }
  349. content_array.push(content.substr(0, length));
  350. content = content.substr(length);
  351. }
  352. if(!F.isNull(content_array)) {
  353. // 搜索
  354. for (let i = 0; i < content_array.length; i++) {
  355. let is_in = true;
  356. for (let j = 0; j < find_arr.length; j++) {
  357. if(content_array[i].indexOf(find_arr[j]) == -1) {
  358. is_in = false;
  359. break;
  360. }
  361. }
  362. if(is_in == true) search_result.push(content_array[i]);
  363. }
  364. }
  365. }
  366. fs.close(fd_res.fd);
  367. return {search_result:search_result};
  368. };
  369. };