Skip to content

Commit b882a21

Browse files
committed
Generate generic logger message
1 parent c0faf27 commit b882a21

3 files changed

Lines changed: 101 additions & 6 deletions

File tree

backend/src/main/java/club/devcord/devmarkt/graphql/CustomInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ public class CustomInstrumentation extends SimpleInstrumentation {
2727
@Override
2828
public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher,
2929
InstrumentationFieldFetchParameters parameters) {
30-
return new ErrorHandlingDataFetcher<>(dataFetcher);
30+
return new ProxyDataFetcher<>(dataFetcher);
3131
}
3232
}

backend/src/main/java/club/devcord/devmarkt/graphql/ErrorHandlingDataFetcher.java renamed to backend/src/main/java/club/devcord/devmarkt/graphql/ProxyDataFetcher.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,42 @@
1616

1717
package club.devcord.devmarkt.graphql;
1818

19+
import club.devcord.devmarkt.logging.LogMsgGenerator;
1920
import club.devcord.devmarkt.responses.Failure;
2021
import club.devcord.devmarkt.responses.FailureException;
2122
import graphql.execution.DataFetcherResult;
2223
import graphql.schema.DataFetcher;
2324
import graphql.schema.DataFetchingEnvironment;
25+
import graphql.schema.GraphQLObjectType;
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
2428

25-
public class ErrorHandlingDataFetcher<T> implements DataFetcher<Object> {
29+
public class ProxyDataFetcher<T> implements DataFetcher<Object> {
2630

27-
private final DataFetcher<T> originalDataFetcher;
31+
private static final Logger LOGGER = LoggerFactory.getLogger(ProxyDataFetcher.class);
2832

29-
public ErrorHandlingDataFetcher(DataFetcher<T> originalDataFetcher) {
33+
private final DataFetcher<T> originalDataFetcher;
34+
public ProxyDataFetcher(DataFetcher<T> originalDataFetcher) {
3035
this.originalDataFetcher = originalDataFetcher;
3136
}
3237

3338
@Override
3439
public Object get(DataFetchingEnvironment environment) throws Exception {
40+
var schema = environment.getGraphQLSchema();
41+
Object result;
3542
try {
36-
return originalDataFetcher.get(environment);
43+
result = originalDataFetcher.get(environment);
3744
} catch (FailureException exception) {
38-
return DataFetcherResult.newResult()
45+
result = DataFetcherResult.newResult()
3946
.data(new Failure(exception.errors()))
4047
.build();
4148
}
49+
if (environment.getParentType() instanceof GraphQLObjectType type
50+
&& (type == schema.getSubscriptionType()
51+
|| (type == schema.getMutationType())
52+
|| (type == schema.getQueryType()))) {
53+
LOGGER.info(LogMsgGenerator.generateMsg(environment, result));
54+
}
55+
return result;
4256
}
4357
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2022 Contributors to the Devmarkt-Backend project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package club.devcord.devmarkt.logging;
18+
19+
import club.devcord.devmarkt.entities.auth.User;
20+
import club.devcord.devmarkt.entities.auth.UserId;
21+
import club.devcord.devmarkt.responses.Failure;
22+
import graphql.execution.DataFetcherResult;
23+
import graphql.schema.DataFetchingEnvironment;
24+
import java.util.Optional;
25+
26+
public class LogMsgGenerator {
27+
28+
private LogMsgGenerator() {
29+
30+
}
31+
32+
public static String generateMsg(DataFetchingEnvironment environment, Object result) {
33+
StringBuilder msg = new StringBuilder(
34+
"Request!");
35+
msg.append(" User: ")
36+
.append(Optional.ofNullable((User) environment.
37+
getGraphQlContext().
38+
get("user"))
39+
.map(User::id)
40+
.map(UserId::toString)
41+
.orElse("none")
42+
)
43+
.append("; Operation: ")
44+
.append(environment.getOperationDefinition().getOperation())
45+
.append(" -> ")
46+
.append(environment.getFieldDefinition().getName());
47+
if (!environment.getArguments().isEmpty()) {
48+
msg.append("; Arg: ");
49+
for (var arg : environment.getArguments().entrySet()) {
50+
msg.append(arg.getKey())
51+
.append(" -> ")
52+
.append(arg.getValue());
53+
}
54+
}
55+
msg.append("; Result: ");
56+
if (result instanceof DataFetcherResult<?> dataFetcherResult) {
57+
if (dataFetcherResult.getData() instanceof Failure failure) {
58+
msg.append("Failure: ");
59+
for (var error : failure.errors()) {
60+
msg.append(error.code())
61+
.append(" -> ")
62+
.append(error.data())
63+
.append(", ");
64+
}
65+
}
66+
if (!dataFetcherResult.getErrors().isEmpty()) {
67+
msg.append("; Errors: ");
68+
for (var error : dataFetcherResult.getErrors()) {
69+
msg.append(error.getErrorType())
70+
.append(" -> ")
71+
.append(error.getMessage())
72+
.append(", ");
73+
}
74+
}
75+
} else {
76+
msg.append(result);
77+
}
78+
return msg.toString();
79+
}
80+
81+
}

0 commit comments

Comments
 (0)