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 c52d7a8c4f
commit d32c01c92f
3 changed files with 25 additions and 8 deletions

View File

@@ -20,6 +20,7 @@ import org.apache.commons.logging.LogFactory;
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.
@@ -104,7 +105,7 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
public void onBeforeConvert(BeforeConvertEvent<E> event) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("onBeforeConvert(%s)", event.getSource()));
LOG.debug(String.format("onBeforeConvert(%s)", SerializationUtils.serializeToJsonSafely(event.getSource())));
}
}
@@ -117,7 +118,7 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
public void onBeforeSave(BeforeSaveEvent<E> event) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("onBeforeSave(%s, %s)", event.getSource(), event.getDocument()));
LOG.debug(String.format("onBeforeSave(%s, %s)", SerializationUtils.serializeToJsonSafely(event.getSource()), SerializationUtils.serializeToJsonSafely(event.getDocument())));
}
}
@@ -130,7 +131,7 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
public void onAfterSave(AfterSaveEvent<E> event) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("onAfterSave(%s, %s)", event.getSource(), event.getDocument()));
LOG.debug(String.format("onAfterSave(%s, %s)", SerializationUtils.serializeToJsonSafely(event.getSource()), SerializationUtils.serializeToJsonSafely(event.getDocument())));
}
}
@@ -143,7 +144,7 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
public void onAfterLoad(AfterLoadEvent<E> event) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("onAfterLoad(%s)", event.getDocument()));
LOG.debug(String.format("onAfterLoad(%s)", SerializationUtils.serializeToJsonSafely(event.getDocument())));
}
}
@@ -156,7 +157,7 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
public void onAfterConvert(AfterConvertEvent<E> event) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("onAfterConvert(%s, %s)", event.getDocument(), event.getSource()));
LOG.debug(String.format("onAfterConvert(%s, %s)", SerializationUtils.serializeToJsonSafely(event.getDocument()), SerializationUtils.serializeToJsonSafely(event.getSource())));
}
}
@@ -169,7 +170,7 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
public void onAfterDelete(AfterDeleteEvent<E> event) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("onAfterDelete(%s)", event.getDocument()));
LOG.debug(String.format("onAfterDelete(%s)", SerializationUtils.serializeToJsonSafely(event.getDocument())));
}
}
@@ -182,7 +183,7 @@ public abstract class AbstractMongoEventListener<E> implements ApplicationListen
public void onBeforeDelete(BeforeDeleteEvent<E> event) {
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("onBeforeDelete(%s)", event.getDocument()));
LOG.debug(String.format("onBeforeDelete(%s)", 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">