Skip to content

Commit 59137dd

Browse files
committed
Logging added
1 parent 9183995 commit 59137dd

File tree

7 files changed

+54
-41
lines changed

7 files changed

+54
-41
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ To minimise the number of return trips between HyperGraphQL server and RDF store
4141
```
4242

4343
### HyperGraphQL response:
44-
```json
44+
```js
4545
{
4646
"extensions": {},
4747
"data": {
@@ -98,7 +98,7 @@ Clone the Git repository into a local directory. Then in the root of the project
9898

9999
Basic settings are defined in the *properties.json* file. The defaults are:
100100

101-
```json
101+
```js
102102
{
103103
"schemaFile": "schema.graphql",
104104
"contextFile": "context.json",
@@ -173,7 +173,7 @@ The context specification consists of three components:
173173

174174
The following example presents a possible context associated with the schema above, where all predicates are associated with the *http://dbpedia.org* graph.
175175

176-
```json
176+
```js
177177
{
178178
"@predicates": {
179179
"person": {

log4j.properties

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Root logger option
2+
log4j.rootLogger=DEBUG, file
3+
4+
# Direct log messages to a log file
5+
log4j.appender.file=org.apache.log4j.RollingFileAppender
6+
7+
#Redirect to Tomcat logs folder
8+
#log4j.appender.file.File=${catalina.home}/logs/logging.log
9+
10+
log4j.appender.file.File=logging.log
11+
log4j.appender.file.MaxFileSize=10MB
12+
log4j.appender.file.MaxBackupIndex=10
13+
log4j.appender.file.layout=org.apache.log4j.PatternLayout
14+
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

src/main/java/uk/co/semanticintegration/hypergraphql/Converter.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.databind.JsonNode;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import graphql.language.TypeDefinition;
6+
import org.apache.log4j.Logger;
67

78
import java.io.IOException;
89
import java.util.ArrayList;
@@ -19,7 +20,7 @@
1920
* This class contains jsonRewrite methods between different query/response formats
2021
*/
2122
public class Converter {
22-
23+
static Logger logger = Logger.getLogger(Converter.class);
2324

2425
private JsonNode globalContext;
2526
private Map<String, String> JSONLD_VOC = new HashMap<String, String>() {{
@@ -115,7 +116,7 @@ public List<String> graphql2sparql(String query) {
115116
String constructQuery = getConstructQuery(nextQuery);
116117
output.add(constructQuery);
117118
} catch (Exception e) {
118-
System.out.println(e.fillInStackTrace());
119+
logger.error(e);
119120
}
120121
}
121122

@@ -249,7 +250,6 @@ private String[] getSubquery(JsonNode node, String parentNode, String parentGrap
249250

250251
childConstruct.add(nodeMark);
251252

252-
// childOptionalPattern = String.format(servicePattern, endpointId, childOptionalPattern);
253253
} else {
254254

255255
String[] grandChildPatterns = getSubquery(field, childNode, graphId, endpointId);
@@ -353,7 +353,7 @@ public JsonNode query2json(String query) {
353353
try {
354354
JsonNode object = mapper.readTree(query);
355355

356-
// System.out.println(object.toString()); //debug message
356+
logger.debug("Generated query JSON: " + object.toString()); //debug message
357357

358358
return object;
359359
} catch (IOException e) {
@@ -389,7 +389,7 @@ public JsonNode definitionToJson(TypeDefinition type) {
389389

390390
return object;
391391
} catch (IOException e) {
392-
e.printStackTrace();
392+
logger.error(e);
393393
}
394394

395395
return null;

src/main/java/uk/co/semanticintegration/hypergraphql/GraphqlService.java

+5-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import graphql.*;
44
import graphql.language.SourceLocation;
5+
import org.apache.log4j.Logger;
56

67
import java.io.IOException;
78
import java.util.ArrayList;
@@ -15,6 +16,9 @@
1516
public class GraphqlService {
1617
private GraphQL graphQL;
1718
private Config config;
19+
static Logger logger = Logger.getLogger(GraphqlService.class);
20+
21+
1822

1923
public GraphqlService(Config config, GraphQL graphQL) {
2024
this.graphQL = graphQL;
@@ -51,26 +55,7 @@ public Map<String, Object> results(String query) {
5155
try {
5256
data = converter.jsonLDdata(query, qlResult.getData());
5357
} catch (IOException e) {
54-
55-
GraphQLError error = new GraphQLError() {
56-
@Override
57-
public String getMessage() {
58-
return e.getMessage();
59-
}
60-
61-
@Override
62-
public List<SourceLocation> getLocations() {
63-
return null;
64-
}
65-
66-
@Override
67-
public ErrorType getErrorType() {
68-
return null ;
69-
}
70-
};
71-
72-
errors.add(error);
73-
58+
logger.error(e);
7459
}
7560

7661
} else {

src/main/java/uk/co/semanticintegration/hypergraphql/GraphqlWiring.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.*;
2727

2828
import org.apache.jena.rdf.model.RDFNode;
29+
import org.apache.log4j.Logger;
2930

3031
/**
3132
* Created by szymon on 24/08/2017.
@@ -39,6 +40,10 @@ public class GraphqlWiring {
3940
private GraphQLSchema schema;
4041
private Converter converter;
4142

43+
static Logger logger = Logger.getLogger(GraphqlWiring.class);
44+
45+
46+
4247
private Map<String, GraphQLArgument> defaultArguments = new HashMap<String, GraphQLArgument>() {{
4348
put("limit", new GraphQLArgument("limit", GraphQLInt));
4449
put("offset", new GraphQLArgument("offset", GraphQLInt));
@@ -227,12 +232,12 @@ public GraphQLFieldDefinition registerGraphQLField(Boolean isQueryType, JsonNode
227232
if (refType.isList) {
228233
return getBuiltField(isQueryType, fieldDef, refType, instancesOfTypeFetcher);
229234
} else {
230-
System.out.println("All queries must return array types.");
235+
logger.error("Wrong schema: all fields in the Query type must return array types. This is not the case for: " + fieldDef.get("name"));
231236
System.exit(1);
232237
}
233238
} else {
234239

235-
String fieldName = refType.dataType;
240+
String targetDataTypeName = refType.dataType;
236241

237242
final Set<String> allowedTypes = new HashSet<String>() {{
238243
add("String");
@@ -241,8 +246,8 @@ public GraphQLFieldDefinition registerGraphQLField(Boolean isQueryType, JsonNode
241246
add("Boolean");
242247
}};
243248

244-
// if (fieldName.equals("String") || fieldName.equals("Int") || fieldName.equals("ID") || fieldName.equals("Boolean")) {
245-
if (allowedTypes.contains(fieldName)) {
249+
// if (targetDataTypeName.equals("String") || targetDataTypeName.equals("Int") || targetDataTypeName.equals("ID") || targetDataTypeName.equals("Boolean")) {
250+
if (allowedTypes.contains(targetDataTypeName)) {
246251
return getBuiltField(isQueryType, fieldDef, refType, literalFetchers.get(refType.isList));
247252
} else {
248253
return getBuiltField(isQueryType, fieldDef, refType, objectFetchers.get(refType.isList));

src/main/java/uk/co/semanticintegration/hypergraphql/Main.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
package uk.co.semanticintegration.hypergraphql;
22

33
import graphql.GraphQL;
4-
import org.apache.log4j.BasicConfigurator;
5-
import org.apache.log4j.Level;
64
import org.apache.log4j.Logger;
5+
import org.apache.log4j.PropertyConfigurator;
76

87
public class Main {
98

9+
static Logger logger = Logger.getLogger(Main.class);
10+
11+
1012
public static void main(String[] args) {
1113

12-
BasicConfigurator.configure();
13-
Logger.getRootLogger().setLevel(Level.OFF);
14+
PropertyConfigurator.configure("log4j.properties");
1415

1516
Config config = new Config("properties.json");
1617

@@ -21,6 +22,8 @@ public static void main(String[] args) {
2122
System.out.println("GraphQL server started at: http://localhost:" + config.graphql().port() + config.graphql().path());
2223
System.out.println("GraphiQL UI available at: http://localhost:" + config.graphql().port() + config.graphql().graphiql());
2324

25+
logger.info("Server started at http://localhost:" + config.graphql().port() + config.graphql().path());
26+
2427
Controller.start(config, graphQL);
2528

2629
}

src/main/java/uk/co/semanticintegration/hypergraphql/SparqlClient.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.jena.sparql.ARQConstants;
1313
import org.apache.jena.sparql.util.Context;
1414
import org.apache.jena.sparql.util.Symbol;
15+
import org.apache.log4j.Logger;
1516

1617
/**
1718
* Created by szymon on 22/08/2017.
@@ -25,12 +26,18 @@ public class SparqlClient {
2526

2627
private Model model;
2728

29+
private static Logger logger = Logger.getLogger(SparqlClient.class);
30+
31+
32+
2833
public SparqlClient(List<String> queries, Map<String, Context> sparqlEndpointsContext) {
2934

3035
model = ModelFactory.createDefaultModel();
3136

3237
for (String constructQuery : queries) {
3338

39+
logger.debug("Executing construct query: " + constructQuery);
40+
3441
QueryExecution qexec = QueryExecutionFactory.create(constructQuery, model);
3542

3643
Context mycxt = qexec.getContext();
@@ -39,9 +46,8 @@ public SparqlClient(List<String> queries, Map<String, Context> sparqlEndpointsCo
3946

4047
try {
4148
qexec.execConstruct(model);
42-
4349
} catch (Exception e) {
44-
System.out.println(e.fillInStackTrace());
50+
logger.error(e);
4551
}
4652
}
4753

@@ -50,14 +56,14 @@ public SparqlClient(List<String> queries, Map<String, Context> sparqlEndpointsCo
5056

5157
public ResultSet sparqlSelect(String queryString) {
5258

53-
// System.out.println(queryString);
59+
logger.debug("Executing SPARQL fetchquery: " + queryString);
5460
QueryExecution qexec = QueryExecutionFactory.create(queryString, model);
5561
try {
5662
ResultSet results = qexec.execSelect();
5763
return results;
5864
} catch (Exception e) {
59-
System.out.println("Failed to return the response on query: " + queryString);
60-
System.out.println(e);
65+
logger.error("Failed to return the response on query: " + queryString);
66+
logger.error(e);
6167
return null;
6268
}
6369
}

0 commit comments

Comments
 (0)