日志:短链接记录

This commit is contained in:
luying
2021-07-27 16:56:57 +08:00
parent ff36c4b361
commit 7bc4ff4577
20 changed files with 244 additions and 9 deletions
+13 -1
View File
@@ -1,4 +1,5 @@
import { ENCRYPT_KEY, ENCRYPT_IV } from '@consts';
import { genCode } from 'app/pubUtils/util';
import { Context } from 'egg';
const crypto = require('crypto');
const isJSON = require('koa-is-json');
@@ -21,6 +22,7 @@ module.exports = options => {
return async function parmsDecode(ctx: Context, next) {
let m = ctx.request.url.indexOf("/dev");
let n = ctx.request.url.indexOf("/web");
ctx.logcode = genCode(10);
if(m == 0 || n == 0) {
await next();
return;
@@ -47,17 +49,27 @@ module.exports = options => {
try {
ctx.request.body = JSON.parse(decodeStr);
console.log('req body', ctx.request.body);
ctx.service.utils.log('INFO', `[${ctx.request.url}] [${ctx.logcode}] request: ${JSON.stringify(ctx.request.body)}`)
} catch (e) {
console.error('parms parse err');
ctx.service.utils.log('ERROR', `[${ctx.request.url}] [${ctx.logcode}] request: parms parse err`)
}
await next();
try{
await next();
} catch(e) {
ctx.service.utils.log('ERROR', `[${ctx.request.url}] [${ctx.logcode}] err: ${e.stack}`);
throw e;
}
const resBody = ctx.body;
console.log('return value:', JSON.stringify(resBody));
if (isJSON(resBody)) {
ctx.body = { result: aesEncrypt(JSON.stringify(resBody), ENCRYPT_KEY, ENCRYPT_IV) };
ctx.service.utils.log('INFO', `[${ctx.request.url}] [${ctx.logcode}] res: ${JSON.stringify(resBody)}`)
} else {
ctx.body = { result: aesEncrypt(JSON.stringify({ status: 3, data: 'internal err' }), ENCRYPT_KEY, ENCRYPT_IV) };
ctx.service.utils.log('ERROR', `[${ctx.request.url}] [${ctx.logcode}] res: ${resBody}`)
}
};
};
+2
View File
@@ -20,6 +20,8 @@ module.exports = () => {
ctx.userCode = user.userCode;
ctx.pkgName = user.pkgName;
ctx.tel = user.tel;
ctx.service.utils.log('INFO', `[${ctx.request.url}] [${ctx.logcode}] user: ${user.uid}`);
await next();
};
};
+14
View File
@@ -54,4 +54,18 @@ export default class Utils extends Service {
public unlockFigure(roleId: string, conditions: {type: number, paramHid?: number, paramFavourLv?: number, paramSkinId?: number}[], role?: RoleType) {
return unlockFigure(roleId, conditions, role);
}
public log(level: 'DEBUG'|'INFO'|'WARN'| 'ERROR', message: string) {
const log = this.app.loggers.get('linkLogger');
if(level == 'DEBUG') {
log.error(`${message}`);
} else if (level == 'INFO') {
log.info(`${message}`);
} else if (level == 'WARN') {
log.warn(`${message}`);
} else if (level == "ERROR") {
log.error(`${message}`);
}
}
}