Skip to content

Commit 611ece0

Browse files
Serialize values for debug output safely in AbstractMongoEventListener.
We now make sure that codec configuration will not cause an exception when debug logging is turned on. Resolves: #3968 Original Pull Request: #3970
1 parent be2286e commit 611ece0

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/event/AbstractMongoEventListener.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.context.ApplicationListener;
2222
import org.springframework.core.GenericTypeResolver;
23+
import org.springframework.data.mongodb.core.query.SerializationUtils;
2324

2425
/**
2526
* Base class to implement domain class specific {@link ApplicationListener}s.
@@ -100,7 +101,7 @@ public void onApplicationEvent(MongoMappingEvent<?> event) {
100101
public void onBeforeConvert(BeforeConvertEvent<E> event) {
101102

102103
if (LOG.isDebugEnabled()) {
103-
LOG.debug(String.format("onBeforeConvert(%s)", event.getSource()));
104+
LOG.debug(String.format("onBeforeConvert(%s)", SerializationUtils.serializeToJsonSafely(event.getSource())));
104105
}
105106
}
106107

@@ -113,7 +114,7 @@ public void onBeforeConvert(BeforeConvertEvent<E> event) {
113114
public void onBeforeSave(BeforeSaveEvent<E> event) {
114115

115116
if (LOG.isDebugEnabled()) {
116-
LOG.debug(String.format("onBeforeSave(%s, %s)", event.getSource(), event.getDocument()));
117+
LOG.debug(String.format("onBeforeSave(%s, %s)", SerializationUtils.serializeToJsonSafely(event.getSource()), SerializationUtils.serializeToJsonSafely(event.getDocument())));
117118
}
118119
}
119120

@@ -126,7 +127,7 @@ public void onBeforeSave(BeforeSaveEvent<E> event) {
126127
public void onAfterSave(AfterSaveEvent<E> event) {
127128

128129
if (LOG.isDebugEnabled()) {
129-
LOG.debug(String.format("onAfterSave(%s, %s)", event.getSource(), event.getDocument()));
130+
LOG.debug(String.format("onAfterSave(%s, %s)", SerializationUtils.serializeToJsonSafely(event.getSource()), SerializationUtils.serializeToJsonSafely(event.getDocument())));
130131
}
131132
}
132133

@@ -139,7 +140,7 @@ public void onAfterSave(AfterSaveEvent<E> event) {
139140
public void onAfterLoad(AfterLoadEvent<E> event) {
140141

141142
if (LOG.isDebugEnabled()) {
142-
LOG.debug(String.format("onAfterLoad(%s)", event.getDocument()));
143+
LOG.debug(String.format("onAfterLoad(%s)", SerializationUtils.serializeToJsonSafely(event.getDocument())));
143144
}
144145
}
145146

@@ -152,7 +153,7 @@ public void onAfterLoad(AfterLoadEvent<E> event) {
152153
public void onAfterConvert(AfterConvertEvent<E> event) {
153154

154155
if (LOG.isDebugEnabled()) {
155-
LOG.debug(String.format("onAfterConvert(%s, %s)", event.getDocument(), event.getSource()));
156+
LOG.debug(String.format("onAfterConvert(%s, %s)", SerializationUtils.serializeToJsonSafely(event.getDocument()), SerializationUtils.serializeToJsonSafely(event.getSource())));
156157
}
157158
}
158159

@@ -165,7 +166,7 @@ public void onAfterConvert(AfterConvertEvent<E> event) {
165166
public void onAfterDelete(AfterDeleteEvent<E> event) {
166167

167168
if (LOG.isDebugEnabled()) {
168-
LOG.debug(String.format("onAfterDelete(%s)", event.getDocument()));
169+
LOG.debug(String.format("onAfterDelete(%s)", SerializationUtils.serializeToJsonSafely(event.getDocument())));
169170
}
170171
}
171172

@@ -178,7 +179,7 @@ public void onAfterDelete(AfterDeleteEvent<E> event) {
178179
public void onBeforeDelete(BeforeDeleteEvent<E> event) {
179180

180181
if (LOG.isDebugEnabled()) {
181-
LOG.debug(String.format("onBeforeDelete(%s)", event.getDocument()));
182+
LOG.debug(String.format("onBeforeDelete(%s)", SerializationUtils.serializeToJsonSafely(event.getDocument())));
182183
}
183184
}
184185
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AbstractMongoEventListenerUnitTests.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import java.time.Instant;
21+
2022
import org.bson.Document;
2123
import org.junit.jupiter.api.Test;
22-
2324
import org.springframework.context.support.AbstractApplicationContext;
2425
import org.springframework.context.support.ClassPathXmlApplicationContext;
2526
import org.springframework.data.mongodb.core.mapping.Account;
2627
import org.springframework.data.mongodb.repository.Contact;
2728
import org.springframework.data.mongodb.repository.Person;
2829

30+
import com.mongodb.BasicDBObject;
31+
2932
/**
3033
* Unit tests for {@link AbstractMongoEventListener}.
3134
*
@@ -154,6 +157,14 @@ public void donInvokePersonCallbackForUntypedEvent() {
154157
assertThat(listener.invokedOnBeforeDelete).isFalse();
155158
}
156159

160+
@Test // GH-3968
161+
public void debugLogShouldNotFailMongoDBCodecError() {
162+
163+
MongoMappingEvent<BasicDBObject> event = new BeforeConvertEvent<>(new BasicDBObject("date", Instant.now()), "collection-1");
164+
UntypedEventListener listener = new UntypedEventListener();
165+
listener.onApplicationEvent(event);
166+
}
167+
157168
class SamplePersonEventListener extends AbstractMongoEventListener<Person> {
158169

159170
boolean invokedOnBeforeConvert;

spring-data-mongodb/src/test/resources/logback.xml

+5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
</encoder>
88
</appender>
99

10+
<appender name="no-op" class="ch.qos.logback.core.helpers.NOPAppender" />
11+
1012
<!--
1113
<logger name="org.springframework" level="debug" />
1214
-->
1315

1416
<logger name="org.springframework.data.mongodb.core" level="error"/>
17+
<logger name="org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener" level="debug" additivity="false">
18+
<appender-ref ref="no-op" />
19+
</logger>
1520
<logger name="org.springframework.data.mongodb.test.util" level="info"/>
1621

1722
<root level="error">

0 commit comments

Comments
 (0)