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
This commit is contained in:
Christoph Strobl
2022-02-18 08:50:58 +01:00
parent 5cd74e6f96
commit edd67c087c
3 changed files with 25 additions and 8 deletions

View File

@@ -19,6 +19,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.core.GenericTypeResolver;
import org.springframework.data.mongodb.core.query.SerializationUtils;
/**
* Base class to implement domain class specific {@link ApplicationListener}s.
@@ -103,7 +104,7 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
public void onBeforeConvert(BeforeConvertEvent<E> event) {
if (LOG.isDebugEnabled()) {
LOG.debug("onBeforeConvert({})", event.getSource());
LOG.debug("onBeforeConvert({})", SerializationUtils.serializeToJsonSafely(event.getSource()));
}
}
@@ -116,7 +117,7 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
public void onBeforeSave(BeforeSaveEvent<E> event) {
if (LOG.isDebugEnabled()) {
LOG.debug("onBeforeSave({}, {})", event.getSource(), event.getDocument());
LOG.debug("onBeforeSave({}, {})", SerializationUtils.serializeToJsonSafely(event.getSource()), SerializationUtils.serializeToJsonSafely(event.getDocument()));
}
}
@@ -129,7 +130,7 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
public void onAfterSave(AfterSaveEvent<E> event) {
if (LOG.isDebugEnabled()) {
LOG.debug("onAfterSave({}, {})", event.getSource(), event.getDocument());
LOG.debug("onAfterSave({}, {})", SerializationUtils.serializeToJsonSafely(event.getSource()), SerializationUtils.serializeToJsonSafely(event.getDocument()));
}
}
@@ -142,7 +143,7 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
public void onAfterLoad(AfterLoadEvent<E> event) {
if (LOG.isDebugEnabled()) {
LOG.debug("onAfterLoad({})", event.getDocument());
LOG.debug("onAfterLoad({})", SerializationUtils.serializeToJsonSafely(event.getDocument()));
}
}
@@ -155,7 +156,7 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
public void onAfterConvert(AfterConvertEvent<E> event) {
if (LOG.isDebugEnabled()) {
LOG.debug("onAfterConvert({}, {})", event.getDocument(), event.getSource());
LOG.debug("onAfterConvert({}, {})", SerializationUtils.serializeToJsonSafely(event.getDocument()), SerializationUtils.serializeToJsonSafely(event.getSource()));
}
}
@@ -168,7 +169,7 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
public void onAfterDelete(AfterDeleteEvent<E> event) {
if (LOG.isDebugEnabled()) {
LOG.debug("onAfterDelete({})", event.getDocument());
LOG.debug("onAfterDelete({})", SerializationUtils.serializeToJsonSafely(event.getDocument()));
}
}
@@ -181,7 +182,7 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
public void onBeforeDelete(BeforeDeleteEvent<E> event) {
if (LOG.isDebugEnabled()) {
LOG.debug("onBeforeDelete({})", event.getDocument());
LOG.debug("onBeforeDelete({})", SerializationUtils.serializeToJsonSafely(event.getDocument()));
}
}
}

View File

@@ -17,15 +17,18 @@ package org.springframework.data.mongodb.core.mapping.event;
import static org.assertj.core.api.Assertions.*;
import java.time.Instant;
import org.bson.Document;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.mongodb.core.mapping.Account;
import org.springframework.data.mongodb.repository.Contact;
import org.springframework.data.mongodb.repository.Person;
import com.mongodb.BasicDBObject;
/**
* Unit tests for {@link AbstractMongoEventListener}.
*
@@ -154,6 +157,14 @@ public class AbstractMongoEventListenerUnitTests {
assertThat(listener.invokedOnBeforeDelete).isFalse();
}
@Test // GH-3968
public void debugLogShouldNotFailMongoDBCodecError() {
MongoMappingEvent<BasicDBObject> event = new BeforeConvertEvent<>(new BasicDBObject("date", Instant.now()), "collection-1");
UntypedEventListener listener = new UntypedEventListener();
listener.onApplicationEvent(event);
}
class SamplePersonEventListener extends AbstractMongoEventListener<Person> {
boolean invokedOnBeforeConvert;

View File

@@ -7,11 +7,16 @@
</encoder>
</appender>
<appender name="no-op" class="ch.qos.logback.core.helpers.NOPAppender" />
<!--
<logger name="org.springframework" level="debug" />
-->
<logger name="org.springframework.data.mongodb.core" level="error"/>
<logger name="org.springframework.data.mongodb.core.mapping.event.AbstractMongoEventListener" level="debug" additivity="false">
<appender-ref ref="no-op" />
</logger>
<logger name="org.springframework.data.mongodb.test.util" level="info"/>
<root level="error">