Skip to content

Commit d32c01c

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 c52d7a8 commit d32c01c

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.
@@ -104,7 +105,7 @@ public void onApplicationEvent(MongoMappingEvent<?> event) {
104105
public void onBeforeConvert(BeforeConvertEvent<E> event) {
105106

106107
if (LOG.isDebugEnabled()) {
107-
LOG.debug(String.format("onBeforeConvert(%s)", event.getSource()));
108+
LOG.debug(String.format("onBeforeConvert(%s)", SerializationUtils.serializeToJsonSafely(event.getSource())));
108109
}
109110
}
110111

@@ -117,7 +118,7 @@ public void onBeforeConvert(BeforeConvertEvent<E> event) {
117118
public void onBeforeSave(BeforeSaveEvent<E> event) {
118119

119120
if (LOG.isDebugEnabled()) {
120-
LOG.debug(String.format("onBeforeSave(%s, %s)", event.getSource(), event.getDocument()));
121+
LOG.debug(String.format("onBeforeSave(%s, %s)", SerializationUtils.serializeToJsonSafely(event.getSource()), SerializationUtils.serializeToJsonSafely(event.getDocument())));
121122
}
122123
}
123124

@@ -130,7 +131,7 @@ public void onBeforeSave(BeforeSaveEvent<E> event) {
130131
public void onAfterSave(AfterSaveEvent<E> event) {
131132

132133
if (LOG.isDebugEnabled()) {
133-
LOG.debug(String.format("onAfterSave(%s, %s)", event.getSource(), event.getDocument()));
134+
LOG.debug(String.format("onAfterSave(%s, %s)", SerializationUtils.serializeToJsonSafely(event.getSource()), SerializationUtils.serializeToJsonSafely(event.getDocument())));
134135
}
135136
}
136137

@@ -143,7 +144,7 @@ public void onAfterSave(AfterSaveEvent<E> event) {
143144
public void onAfterLoad(AfterLoadEvent<E> event) {
144145

145146
if (LOG.isDebugEnabled()) {
146-
LOG.debug(String.format("onAfterLoad(%s)", event.getDocument()));
147+
LOG.debug(String.format("onAfterLoad(%s)", SerializationUtils.serializeToJsonSafely(event.getDocument())));
147148
}
148149
}
149150

@@ -156,7 +157,7 @@ public void onAfterLoad(AfterLoadEvent<E> event) {
156157
public void onAfterConvert(AfterConvertEvent<E> event) {
157158

158159
if (LOG.isDebugEnabled()) {
159-
LOG.debug(String.format("onAfterConvert(%s, %s)", event.getDocument(), event.getSource()));
160+
LOG.debug(String.format("onAfterConvert(%s, %s)", SerializationUtils.serializeToJsonSafely(event.getDocument()), SerializationUtils.serializeToJsonSafely(event.getSource())));
160161
}
161162
}
162163

@@ -169,7 +170,7 @@ public void onAfterConvert(AfterConvertEvent<E> event) {
169170
public void onAfterDelete(AfterDeleteEvent<E> event) {
170171

171172
if (LOG.isDebugEnabled()) {
172-
LOG.debug(String.format("onAfterDelete(%s)", event.getDocument()));
173+
LOG.debug(String.format("onAfterDelete(%s)", SerializationUtils.serializeToJsonSafely(event.getDocument())));
173174
}
174175
}
175176

@@ -182,7 +183,7 @@ public void onAfterDelete(AfterDeleteEvent<E> event) {
182183
public void onBeforeDelete(BeforeDeleteEvent<E> event) {
183184

184185
if (LOG.isDebugEnabled()) {
185-
LOG.debug(String.format("onBeforeDelete(%s)", event.getDocument()));
186+
LOG.debug(String.format("onBeforeDelete(%s)", SerializationUtils.serializeToJsonSafely(event.getDocument())));
186187
}
187188
}
188189
}

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)