日志:短链接记录
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
import { genCode } from "@pubUtils/util";
|
||||
|
||||
module.exports = () => {
|
||||
return async function log(ctx, next) {
|
||||
ctx.logcode = genCode(10);
|
||||
|
||||
ctx.service.utils.log('INFO', `[${ctx.request.url}] [${ctx.logcode}] request: ${JSON.stringify(ctx.request.body)}`)
|
||||
|
||||
|
||||
try{
|
||||
await next();
|
||||
} catch(e) {
|
||||
ctx.service.utils.log('ERROR', `[${ctx.request.url}] [${ctx.logcode}] err: ${e.stack}`);
|
||||
throw e;
|
||||
}
|
||||
const resBody = ctx.body;
|
||||
ctx.service.utils.log('INFO', `[${ctx.request.url}] [${ctx.logcode}] res: ${JSON.stringify(resBody)}`)
|
||||
};
|
||||
};
|
||||
|
||||
@@ -36,4 +36,18 @@ export default class Utils extends Service {
|
||||
public checkActivityData(data: any, type: number) {
|
||||
return { data, type }
|
||||
}
|
||||
|
||||
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}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user