auto_logger.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. if (this.session.sessionUser) {
  21. const bLogger = getBussinessLogger(this);
  22. const logData = {
  23. method: this.method,
  24. user: this.session.sessionUser,
  25. project: this.session.sessionProject,
  26. data: this.body,
  27. };
  28. bLogger.info(JSON.stringify(logData));
  29. }
  30. // 自动记录log的action
  31. // const autoLogAction = ['save', 'delete'];
  32. // if (this.actionName !== undefined && autoLogAction.indexOf(this.actionName) >= 0) {
  33. // // 操作数据的id
  34. // const idReg = /\/(\d+)/;
  35. // const paramInfo = this.request.originalUrl.match(idReg);
  36. // let targetId = paramInfo[1] !== undefined ? paramInfo[1] : -1;
  37. // targetId = parseInt(targetId);
  38. //
  39. // const logData = {
  40. // controller: this.controllerName,
  41. // action: this.actionName,
  42. // operation: this.currentName === undefined ? '保存数据' : this.currentName,
  43. // target_id: targetId,
  44. // };
  45. // yield this.service.log.addLog(logData);
  46. // }
  47. yield next;
  48. };
  49. };