123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- 'use strict';
- /**
- * 自动记录日志
- *
- * @author CaiAoLin
- * @date 2017/10/30
- * @version
- */
- const os = require('os');
- module.exports = options => {
- return function* autoLogger(next) {
- this.logId = this.app.uuid.v1();
- // 记录业务日志
- function getBussinessLogger(ctx) {
- if (ctx.url.match(/ledger/)) {
- return ctx.getLogger('ledger');
- } else if (ctx.url.match(/stage/)) {
- return ctx.getLogger('stage');
- }
- return ctx.getLogger('mixed');
- }
- if (this.url.indexOf('/public/') !== 0) {
- const bLogger = getBussinessLogger(this);
- if (this.session.sessionUser) {
- const logData = {
- method: this.method,
- user: this.session.sessionUser,
- project: this.session.sessionProject,
- data: this.request.body,
- };
- bLogger.info(JSON.stringify(logData));
- } else {
- const logData = {
- method: this.method,
- data: this.body,
- };
- bLogger.info(JSON.stringify(logData));
- }
- this.logInfo = { id: this.logId, time: new Date() };
- const cpus = os.cpus();
- this.logInfo.cpus = cpus.map(x => {
- return `${((1-x.times.idle/(x.times.idle+x.times.user+x.times.nice+x.times.sys+x.times.irq))*100).toFixed(2)}%`
- });
- yield this.service.globalLog.requestLog(this.request.body.data);
- }
- yield next;
- };
- };
|