refactor(audit-manager): optimize the logic of getting metadata (#5814)

* chore: optmize log output

* chore: optmize log output
This commit is contained in:
Zhi Chen 2024-12-13 10:00:41 +08:00 committed by GitHub
parent 2cb4304a38
commit 21bd55422f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 44 deletions

View File

@ -284,6 +284,8 @@ export class AuditManager {
const auditLog: AuditLog = this.formatAuditData(ctx);
auditLog.uuid = reqId;
auditLog.status = ctx.status;
const defaultMetaData = await this.getDefaultMetaData(ctx);
auditLog.metadata = { ...metadata, ...defaultMetaData };
if (typeof action !== 'string') {
if (action.getUserInfo) {
const userInfo = await action.getUserInfo(ctx);
@ -298,10 +300,15 @@ export class AuditManager {
}
if (action.getMetaData) {
const extra = await action.getMetaData(ctx);
auditLog.metadata = { ...metadata, ...extra };
} else {
const defaultMetaData = await this.getDefaultMetaData(ctx);
auditLog.metadata = { ...metadata, ...defaultMetaData };
if (extra) {
if (extra.request) {
auditLog.metadata.request = { ...auditLog.metadata.request, ...extra.request };
}
if (extra.response) {
auditLog.metadata.response = { ...auditLog.metadata.response, ...extra.response };
}
auditLog.metadata = { ...extra, ...auditLog.metadata };
}
}
if (action.getSourceAndTarget) {
const sourceAndTarget = await action.getSourceAndTarget(ctx);
@ -312,9 +319,6 @@ export class AuditManager {
auditLog.targetRecordUK = sourceAndTarget.targetRecordUK;
}
}
} else {
const defaultMetaData = await this.getDefaultMetaData(ctx);
auditLog.metadata = { ...metadata, ...defaultMetaData };
}
this.logger.log(auditLog);
} catch (err) {

View File

@ -129,20 +129,10 @@ export class PluginAuthServer extends Plugin {
}
return {
request: {
params: ctx.request?.params,
body: {
...ctx.request?.body,
password: undefined,
},
path: ctx.request?.path,
headers: {
'x-authenticator': ctx.request?.headers['x-authenticator'],
'x-locale': ctx.request?.headers['x-locale'],
'x-timezone': ctx.request?.headers['x-timezone'],
},
},
response: {
body,
},
};
},
@ -180,24 +170,11 @@ export class PluginAuthServer extends Plugin {
getMetaData: async (ctx: any) => {
return {
request: {
params: ctx.request?.params,
body: {
...ctx.request?.body,
password: undefined,
confirm_password: undefined,
},
path: ctx.request?.path,
headers: {
'x-authenticator': ctx.request?.headers['x-authenticator'],
'x-locale': ctx.request?.headers['x-locale'],
'x-timezone': ctx.request?.headers['x-timezone'],
},
},
response: {
body: {
...ctx.response?.body,
token: undefined,
},
},
};
},
@ -207,15 +184,7 @@ export class PluginAuthServer extends Plugin {
getMetaData: async (ctx: any) => {
return {
request: {
params: ctx.request.params,
query: ctx.request.query,
body: {},
path: ctx.request.path,
headers: {
'x-authenticator': ctx.request?.headers['x-authenticator'],
'x-locale': ctx.request?.headers['x-locale'],
'x-timezone': ctx.request?.headers['x-timezone'],
},
},
response: {
body: {},

View File

@ -154,16 +154,10 @@ export default class PluginUsersServer extends Plugin {
const getMetaDataForUpdateProfileAction = async (ctx: any) => {
return {
request: {
params: ctx.request.params,
query: ctx.request.query,
body: {
...ctx.request.body,
password: '******',
},
path: ctx.request.path,
},
response: {
body: ctx.body,
},
};
};