Skip to content

Commit

Permalink
Reduce access log spam (v4) (#2020)
Browse files Browse the repository at this point in the history
Backport #1968 into v4.

Co-authored-by: Tobias Kuhn <tobias.kuhn@vivid-planet.com>
Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 30, 2024
1 parent 3d99ad5 commit 541ca12
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-phones-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/cms-api": minor
---

Enhanced the access log functionality to now skip logging for field resolvers in GraphQL context. This change improves the readability and relevance of our logs by reducing unnecessary entries.
10 changes: 9 additions & 1 deletion demo/api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,15 @@ export class AppModule {
PredefinedPageModule,
CronJobsModule,
ProductsModule,
AccessLogModule,
AccessLogModule.forRoot({
shouldLogRequest: ({ user }) => {
// Ignore system user
if (user === true) {
return false;
}
return true;
},
}),
],
};
}
Expand Down
11 changes: 10 additions & 1 deletion packages/api/cms-api/src/access-log/access-log.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export class AccessLogInterceptor implements NestInterceptor {
if (requestType === "graphql") {
const graphqlExecutionContext = GqlExecutionContext.create(context);
const graphqlContext = graphqlExecutionContext.getContext();
const gqlInfo = graphqlExecutionContext.getInfo<GraphQLResolveInfo>();

if (this.isResolvingGraphQLField(gqlInfo)) {
return next.handle();
}

if (
this.shouldLogRequest &&
Expand All @@ -38,7 +43,6 @@ export class AccessLogInterceptor implements NestInterceptor {
this.pushUserToRequestData(graphqlContext.req.user, requestData);

const gqlArgs = { ...graphqlExecutionContext.getArgs() };
const gqlInfo = graphqlExecutionContext.getInfo<GraphQLResolveInfo>();

if (gqlInfo.operation.operation === "mutation") {
delete gqlArgs["input"];
Expand Down Expand Up @@ -84,4 +88,9 @@ export class AccessLogInterceptor implements NestInterceptor {
requestData.push(`user: ${user.id} (${user.name})`);
}
}

private isResolvingGraphQLField(gqlInfo: GraphQLResolveInfo): boolean {
const parentType = gqlInfo.parentType.name;
return parentType !== "Query" && parentType !== "Mutation";
}
}

0 comments on commit 541ca12

Please sign in to comment.