auto_logger.js 1.7 KB

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