auto_logger.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. /**
  3. * 自动记录日志
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/30
  7. * @version
  8. */
  9. module.exports = options => {
  10. return function* autoLogger(next) {
  11. // 记录业务日志
  12. function getBussinessLogger(ctx) {
  13. if (ctx.url.match(/ledger/)) {
  14. return ctx.getLogger('ledger');
  15. } else if (ctx.url.match(/stage/)) {
  16. return ctx.getLogger('stage');
  17. }
  18. return ctx.getLogger('mixed');
  19. }
  20. const bLogger = getBussinessLogger(this);
  21. if (this.session.sessionUser) {
  22. const logData = {
  23. method: this.method,
  24. user: this.session.sessionUser,
  25. project: this.session.sessionProject,
  26. data: this.request.body,
  27. };
  28. bLogger.info(JSON.stringify(logData));
  29. } else {
  30. const logData = {
  31. method: this.method,
  32. data: this.body,
  33. };
  34. bLogger.info(JSON.stringify(logData));
  35. }
  36. this.logTime = new Date();
  37. // 自动记录log的action
  38. // const autoLogAction = ['save', 'delete'];
  39. // if (this.actionName !== undefined && autoLogAction.indexOf(this.actionName) >= 0) {
  40. // // 操作数据的id
  41. // const idReg = /\/(\d+)/;
  42. // const paramInfo = this.request.originalUrl.match(idReg);
  43. // let targetId = paramInfo[1] !== undefined ? paramInfo[1] : -1;
  44. // targetId = parseInt(targetId);
  45. //
  46. // const logData = {
  47. // controller: this.controllerName,
  48. // action: this.actionName,
  49. // operation: this.currentName === undefined ? '保存数据' : this.currentName,
  50. // target_id: targetId,
  51. // };
  52. // yield this.service.log.addLog(logData);
  53. // }
  54. yield next;
  55. };
  56. };