auto_logger.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. // 自动记录log的action
  37. // const autoLogAction = ['save', 'delete'];
  38. // if (this.actionName !== undefined && autoLogAction.indexOf(this.actionName) >= 0) {
  39. // // 操作数据的id
  40. // const idReg = /\/(\d+)/;
  41. // const paramInfo = this.request.originalUrl.match(idReg);
  42. // let targetId = paramInfo[1] !== undefined ? paramInfo[1] : -1;
  43. // targetId = parseInt(targetId);
  44. //
  45. // const logData = {
  46. // controller: this.controllerName,
  47. // action: this.actionName,
  48. // operation: this.currentName === undefined ? '保存数据' : this.currentName,
  49. // target_id: targetId,
  50. // };
  51. // yield this.service.log.addLog(logData);
  52. // }
  53. yield next;
  54. };
  55. };