diff --git a/engine/src/main/java/org/hibernate/search/engine/logging/impl/Log.java b/engine/src/main/java/org/hibernate/search/engine/logging/impl/Log.java index f15679fae03..6e2f90d2acd 100644 --- a/engine/src/main/java/org/hibernate/search/engine/logging/impl/Log.java +++ b/engine/src/main/java/org/hibernate/search/engine/logging/impl/Log.java @@ -29,10 +29,12 @@ import org.hibernate.search.util.common.SearchTimeoutException; import org.hibernate.search.util.common.logging.impl.ClassFormatter; import org.hibernate.search.util.common.logging.impl.DurationInSecondsAndFractionsFormatter; +import org.hibernate.search.util.common.logging.impl.EventContextFormatter; import org.hibernate.search.util.common.logging.impl.EventContextNoPrefixFormatter; import org.hibernate.search.util.common.logging.impl.MessageConstants; import org.hibernate.search.util.common.logging.impl.SimpleNameClassFormatter; import org.hibernate.search.util.common.reporting.EventContext; +import org.hibernate.search.util.common.reporting.spi.EventContextProvider; import org.jboss.logging.BasicLogger; import org.jboss.logging.Logger; @@ -141,10 +143,12 @@ SearchException collectedFailures(String process, String renderedFailures, value = "Hibernate Search encountered a failure during %1$s;" + " continuing for now to list all problems," + " but the process will ultimately be aborted.\n" - + "Context: %2$s\n" + + "%2$s\n" // Context + "Failure:" // The stack trace follows ) - void newCollectedFailure(String process, String context, @Cause Throwable failure); + void newCollectedFailure(String process, + @FormatWith(EventContextFormatter.class) EventContextProvider contextProvider, + @Cause Throwable failure); @LogMessage(level = Logger.Level.WARN) @Message(id = ID_OFFSET + 22, value = "Exception while collecting a failure" diff --git a/engine/src/main/java/org/hibernate/search/engine/reporting/spi/ContextualFailureCollector.java b/engine/src/main/java/org/hibernate/search/engine/reporting/spi/ContextualFailureCollector.java index 5944a7d2c9a..79684602b92 100644 --- a/engine/src/main/java/org/hibernate/search/engine/reporting/spi/ContextualFailureCollector.java +++ b/engine/src/main/java/org/hibernate/search/engine/reporting/spi/ContextualFailureCollector.java @@ -6,6 +6,8 @@ */ package org.hibernate.search.engine.reporting.spi; +import org.hibernate.search.util.common.reporting.spi.EventContextProvider; + /** * A failure collector with an implicit context. *

@@ -13,7 +15,7 @@ * * @see FailureCollector */ -public interface ContextualFailureCollector extends FailureCollector { +public interface ContextualFailureCollector extends FailureCollector, EventContextProvider { boolean hasFailure(); diff --git a/engine/src/main/java/org/hibernate/search/engine/reporting/spi/RootFailureCollector.java b/engine/src/main/java/org/hibernate/search/engine/reporting/spi/RootFailureCollector.java index f9c1db8f569..230571c5d99 100644 --- a/engine/src/main/java/org/hibernate/search/engine/reporting/spi/RootFailureCollector.java +++ b/engine/src/main/java/org/hibernate/search/engine/reporting/spi/RootFailureCollector.java @@ -11,7 +11,6 @@ import java.util.Collection; import java.util.List; import java.util.Map; -import java.util.StringJoiner; import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.atomic.AtomicInteger; @@ -25,7 +24,6 @@ import org.hibernate.search.util.common.logging.impl.LoggerFactory; import org.hibernate.search.util.common.reporting.EventContext; import org.hibernate.search.util.common.reporting.EventContextElement; -import org.hibernate.search.util.common.reporting.impl.CommonEventContextMessages; public final class RootFailureCollector implements FailureCollector { @@ -135,8 +133,8 @@ ContextualFailureCollectorImpl withDefaultContext() { return withContext( EventContexts.defaultContext() ); } - void appendContextTo(StringJoiner joiner) { - // Nothing to do + EventContext createEventContext(EventContextElement contextElement) { + return EventContext.create( contextElement ); } final void appendChildrenFailuresTo(List failures, ToStringTreeBuilder builder) { @@ -152,20 +150,21 @@ final void appendChildrenFailuresTo(List failures, ToStringTreeBuilde final Collection children() { return children.values(); } + } private static class ContextualFailureCollectorImpl extends NonRootFailureCollector implements ContextualFailureCollector { private final NonRootFailureCollector parent; - private final EventContextElement context; + private final EventContextElement contextElement; // Avoiding blocking implementations because we access this from reactive event loops private final Collection failures = new ConcurrentLinkedDeque<>(); private final Collection failureMessages = new ConcurrentLinkedDeque<>(); - private ContextualFailureCollectorImpl(NonRootFailureCollector parent, EventContextElement context) { + private ContextualFailureCollectorImpl(NonRootFailureCollector parent, EventContextElement contextElement) { super( parent ); this.parent = parent; - this.context = context; + this.contextElement = contextElement; } @Override @@ -209,13 +208,17 @@ ContextualFailureCollectorImpl withDefaultContext() { } @Override - void appendContextTo(StringJoiner joiner) { - parent.appendContextTo( joiner ); - joiner.add( context.render() ); + public EventContext eventContext() { + return parent.createEventContext( contextElement ); + } + + @Override + EventContext createEventContext(EventContextElement contextElement) { + return eventContext().append( contextElement ); } void appendFailuresTo(List failures, ToStringTreeBuilder builder) { - builder.startObject( context.render() ); + builder.startObject( contextElement.render() ); failures.addAll( this.failures ); if ( !failureMessages.isEmpty() ) { builder.attribute( EngineEventContextMessages.INSTANCE.failureReportFailures(), failureMessages ); @@ -225,9 +228,7 @@ void appendFailuresTo(List failures, ToStringTreeBuilder builder) { } private void doAdd(Throwable failure, String failureMessage) { - StringJoiner contextJoiner = new StringJoiner( CommonEventContextMessages.INSTANCE.contextSeparator() ); - appendContextTo( contextJoiner ); - log.newCollectedFailure( root.process, contextJoiner.toString(), failure ); + log.newCollectedFailure( root.process, this, failure ); if ( root.shouldAddFailure() ) { failureMessages.add( failureMessage ); diff --git a/util/common/src/main/java/org/hibernate/search/util/common/logging/impl/EventContextFormatter.java b/util/common/src/main/java/org/hibernate/search/util/common/logging/impl/EventContextFormatter.java index 70aba526619..f88b2395f4c 100644 --- a/util/common/src/main/java/org/hibernate/search/util/common/logging/impl/EventContextFormatter.java +++ b/util/common/src/main/java/org/hibernate/search/util/common/logging/impl/EventContextFormatter.java @@ -7,11 +7,16 @@ package org.hibernate.search.util.common.logging.impl; import org.hibernate.search.util.common.reporting.EventContext; +import org.hibernate.search.util.common.reporting.spi.EventContextProvider; public final class EventContextFormatter { private final EventContext eventContext; + public EventContextFormatter(EventContextProvider eventContextProvider) { + this( eventContextProvider.eventContext() ); + } + public EventContextFormatter(EventContext eventContext) { this.eventContext = eventContext; } diff --git a/util/common/src/main/java/org/hibernate/search/util/common/reporting/EventContext.java b/util/common/src/main/java/org/hibernate/search/util/common/reporting/EventContext.java index ff385e08fbf..4d98b32cfe4 100644 --- a/util/common/src/main/java/org/hibernate/search/util/common/reporting/EventContext.java +++ b/util/common/src/main/java/org/hibernate/search/util/common/reporting/EventContext.java @@ -102,6 +102,10 @@ public String renderWithPrefix() { return MESSAGES.contextPrefix() + render(); } + public EventContext append(EventContextElement other) { + return new EventContext( this, other ); + } + public EventContext append(EventContext other) { return other.appendTo( this ); } diff --git a/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/stub/StubUnusedContextualFailureCollector.java b/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/stub/StubUnusedContextualFailureCollector.java index e8313810a59..21b40625847 100644 --- a/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/stub/StubUnusedContextualFailureCollector.java +++ b/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/stub/StubUnusedContextualFailureCollector.java @@ -37,4 +37,9 @@ public ContextualFailureCollector withContext(EventContext context) { public ContextualFailureCollector withContext(EventContextElement contextElement) { return fail( "Unexpected call to withContext(" + contextElement + ")" ); } + + @Override + public EventContext eventContext() { + return fail( "Unexpected call to eventContext()" ); + } }