Skip to content

Commit 9a6490b

Browse files
committed
GH-2800 - Introduce additional loggers for cypher notifications.
Closes #2800
1 parent eb89011 commit 9a6490b

File tree

5 files changed

+63
-56
lines changed

5 files changed

+63
-56
lines changed

src/main/antora/modules/ROOT/pages/appendix/index.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ include::custom-queries.adoc[]
1212

1313
include::spatial-types.adoc[]
1414

15+
include::logging.adoc[]
16+
1517
include::migrating.adoc[]
1618

1719
include::build.adoc[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[[logging]]
2+
= Logging
3+
4+
Spring Data Neo4j provides multiple loggers for https://neo4j.com/docs/status-codes/current/notifications/all-notifications/[Cypher notifications], starting with version 7.1.5.
5+
The logger `org.springframework.data.neo4j.cypher` includes all statements that were invoked by Spring Data Neo4j and all notifications sent from the server.
6+
To exclude or elevate some categories, the following loggers are in place:
7+
8+
* `org.springframework.data.neo4j.cypher.performance`
9+
* `org.springframework.data.neo4j.cypher.hint`
10+
* `org.springframework.data.neo4j.cypher.unrecognized`
11+
* `org.springframework.data.neo4j.cypher.unsupported`
12+
* `org.springframework.data.neo4j.cypher.deprecation`
13+
* `org.springframework.data.neo4j.cypher.generic`

src/main/java/org/springframework/data/neo4j/core/ResultSummaries.java

+41-6
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
import java.util.stream.Collectors;
2020
import java.util.stream.Stream;
2121

22+
import org.apache.commons.logging.LogFactory;
23+
import org.neo4j.driver.NotificationCategory;
2224
import org.neo4j.driver.summary.InputPosition;
2325
import org.neo4j.driver.summary.Notification;
2426
import org.neo4j.driver.summary.Plan;
2527
import org.neo4j.driver.summary.ResultSummary;
28+
import org.springframework.core.log.LogAccessor;
2629

2730
/**
2831
* Utility class for dealing with result summaries.
@@ -34,6 +37,12 @@
3437
final class ResultSummaries {
3538

3639
private static final String LINE_SEPARATOR = System.lineSeparator();
40+
private static final LogAccessor cypherPerformanceNotificationLog = new LogAccessor(LogFactory.getLog("org.springframework.data.neo4j.cypher.performance"));
41+
private static final LogAccessor cypherHintNotificationLog = new LogAccessor(LogFactory.getLog("org.springframework.data.neo4j.cypher.hint"));
42+
private static final LogAccessor cypherUnrecognizedNotificationLog = new LogAccessor(LogFactory.getLog("org.springframework.data.neo4j.cypher.unrecognized"));
43+
private static final LogAccessor cypherUnsupportedNotificationLog = new LogAccessor(LogFactory.getLog("org.springframework.data.neo4j.cypher.unsupported"));
44+
private static final LogAccessor cypherDeprecationNotificationLog = new LogAccessor(LogFactory.getLog("org.springframework.data.neo4j.cypher.deprecation"));
45+
private static final LogAccessor cypherGenericNotificationLog = new LogAccessor(LogFactory.getLog("org.springframework.data.neo4j.cypher.generic"));
3746

3847
/**
3948
* Does some post-processing on the giving result summary, especially logging all notifications
@@ -57,15 +66,41 @@ private static void logNotifications(ResultSummary resultSummary) {
5766
String query = resultSummary.query().text();
5867
resultSummary.notifications()
5968
.forEach(notification -> {
60-
Consumer<String> log = switch (notification.severity()) {
61-
case "WARNING" -> Neo4jClient.cypherLog::warn;
62-
case "INFORMATION" -> Neo4jClient.cypherLog::info;
63-
default -> Neo4jClient.cypherLog::debug;
64-
};
65-
log.accept(ResultSummaries.format(notification, query));
69+
LogAccessor log = notification.category()
70+
.map(ResultSummaries::getLogAccessor)
71+
.orElse(Neo4jClient.cypherLog);
72+
Consumer<String> logFunction =
73+
switch (notification.severity()) {
74+
case "WARNING" -> log::warn;
75+
case "INFORMATION" -> log::info;
76+
default -> log::debug;
77+
};
78+
logFunction.accept(ResultSummaries.format(notification, query));
6679
});
6780
}
6881

82+
private static LogAccessor getLogAccessor(NotificationCategory category) {
83+
if (category == NotificationCategory.HINT) {
84+
return cypherHintNotificationLog;
85+
}
86+
if (category == NotificationCategory.DEPRECATION) {
87+
return cypherDeprecationNotificationLog;
88+
}
89+
if (category == NotificationCategory.PERFORMANCE) {
90+
return cypherPerformanceNotificationLog;
91+
}
92+
if (category == NotificationCategory.GENERIC) {
93+
return cypherGenericNotificationLog;
94+
}
95+
if (category == NotificationCategory.UNSUPPORTED) {
96+
return cypherUnsupportedNotificationLog;
97+
}
98+
if (category == NotificationCategory.UNRECOGNIZED) {
99+
return cypherUnrecognizedNotificationLog;
100+
}
101+
return Neo4jClient.cypherLog;
102+
}
103+
69104
/**
70105
* Creates a formatted string for a notification issued for a given query.
71106
*

src/test/java/org/springframework/data/neo4j/test/TestLogFilter.java

-49
This file was deleted.

src/test/resources/logback-test.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
<configuration>
1919

2020
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
21-
<filter class="org.springframework.data.neo4j.test.TestLogFilter" />
2221
<encoder>
2322
<pattern>[%t] %d %5p %40.40c:%4L - %m%n</pattern>
2423
</encoder>
@@ -31,6 +30,13 @@
3130
<logger name="InboundMessageDispatcher" level="info"/>
3231

3332
<logger name="org.springframework.data.neo4j.cypher" level="info"/>
33+
<logger name="org.springframework.data.neo4j.cypher.performance" level="info"/>
34+
<logger name="org.springframework.data.neo4j.cypher.hint" level="info"/>
35+
<logger name="org.springframework.data.neo4j.cypher.unrecognized" level="error"/>
36+
<logger name="org.springframework.data.neo4j.cypher.unsupported" level="error"/>
37+
<!-- if deprecation gets set to "warn" or finer, all id() deprecation will get logged -->
38+
<logger name="org.springframework.data.neo4j.cypher.deprecation" level="error"/>
39+
<logger name="org.springframework.data.neo4j.cypher.generic" level="error"/>
3440
<logger name="org.springframework.data.neo4j" level="info"/>
3541
<logger name="org.springframework.data.neo4j.test" level="info"/>
3642
<logger name="org.springframework.data.neo4j.repository.query.Neo4jQuerySupport" level="debug" />

0 commit comments

Comments
 (0)