diff --git a/src/main/java/com/hivemq/HiveMQServer.java b/src/main/java/com/hivemq/HiveMQServer.java index 5f8873821..10f50d90e 100644 --- a/src/main/java/com/hivemq/HiveMQServer.java +++ b/src/main/java/com/hivemq/HiveMQServer.java @@ -66,18 +66,20 @@ public class HiveMQServer { private final @NotNull SystemInformation systemInformation; private final @NotNull MetricRegistry metricRegistry; private final boolean migrate; + private final boolean enableLoggingBootstrap; private @Nullable Injector injector; private @Nullable FullConfigurationService configService; public HiveMQServer() { - this(new SystemInformationImpl(true), new MetricRegistry(), null, true); + this(new SystemInformationImpl(true), new MetricRegistry(), null, true, true); } public HiveMQServer( final @NotNull SystemInformation systemInformation, final @Nullable MetricRegistry metricRegistry, final @Nullable FullConfigurationService configService, + final boolean enableLoggingBootstrap, final boolean migrate) { hivemqId = new HivemqId(); lifecycleModule = new LifecycleModule(); @@ -85,6 +87,7 @@ public HiveMQServer( this.systemInformation = systemInformation; this.metricRegistry = metricRegistry; this.configService = configService; + this.enableLoggingBootstrap = enableLoggingBootstrap; this.migrate = migrate; } @@ -105,7 +108,9 @@ public void bootstrap() throws Exception { metricRegistry.addListener(new MetricRegistryLogger()); - LoggingBootstrap.prepareLogging(); + if (enableLoggingBootstrap) { + LoggingBootstrap.prepareLogging(); + } log.info("Starting HiveMQ Community Edition Server"); @@ -116,8 +121,11 @@ public void bootstrap() throws Exception { systemInformation.init(); } - log.trace("Initializing Logging"); - LoggingBootstrap.initLogging(systemInformation.getConfigFolder()); + if (enableLoggingBootstrap) { + log.trace("Initializing Logging"); + + LoggingBootstrap.initLogging(systemInformation.getConfigFolder()); + } log.trace("Initializing Exception handlers"); HiveMQExceptionHandlerBootstrap.addUnrecoverableExceptionHandler(); @@ -216,7 +224,10 @@ public void startInstance(final @Nullable EmbeddedExtension embeddedExtension) t /* It's important that we are modifying the log levels after Guice is initialized, otherwise this somehow interferes with Singleton creation */ - LoggingBootstrap.addLoglevelModifiers(); + if (enableLoggingBootstrap) { + LoggingBootstrap.addLoglevelModifiers(); + } + instance.start(embeddedExtension); } @@ -262,7 +273,9 @@ public void stop() { if (configService.persistenceConfigurationService().getMode() == PersistenceMode.FILE) { dataLock.unlock(); } - LoggingBootstrap.resetLogging(); + if (enableLoggingBootstrap) { + LoggingBootstrap.resetLogging(); + } } public @Nullable Injector getInjector() { diff --git a/src/main/java/com/hivemq/embedded/EmbeddedHiveMQBuilder.java b/src/main/java/com/hivemq/embedded/EmbeddedHiveMQBuilder.java index 5cac4cf41..8f3f8b010 100644 --- a/src/main/java/com/hivemq/embedded/EmbeddedHiveMQBuilder.java +++ b/src/main/java/com/hivemq/embedded/EmbeddedHiveMQBuilder.java @@ -74,6 +74,14 @@ public interface EmbeddedHiveMQBuilder { */ @NotNull EmbeddedHiveMQBuilder withEmbeddedExtension(@Nullable EmbeddedExtension embeddedExtension); + /** + * Disables the internal logging bootstrap, for the case any logging is already provided by other frameworks, + * such as OSGi or Springboot. + * + * @return this builder. + */ + @NotNull EmbeddedHiveMQBuilder withoutLoggingBootstrap(); + /** * Concludes the EmbeddedHiveMQ build process. *

diff --git a/src/main/java/com/hivemq/embedded/internal/EmbeddedHiveMQBuilderImpl.java b/src/main/java/com/hivemq/embedded/internal/EmbeddedHiveMQBuilderImpl.java index b9a690d76..f9e0a270a 100644 --- a/src/main/java/com/hivemq/embedded/internal/EmbeddedHiveMQBuilderImpl.java +++ b/src/main/java/com/hivemq/embedded/internal/EmbeddedHiveMQBuilderImpl.java @@ -35,6 +35,7 @@ public class EmbeddedHiveMQBuilderImpl implements EmbeddedHiveMQBuilder { private @Nullable Path dataFolder = null; private @Nullable Path extensionsFolder = null; private @Nullable EmbeddedExtension embeddedExtension = null; + private boolean enableLoggingBootstrap = true; @Override public @NotNull EmbeddedHiveMQBuilder withConfigurationFolder(final @Nullable Path configFolder) { @@ -60,6 +61,12 @@ public class EmbeddedHiveMQBuilderImpl implements EmbeddedHiveMQBuilder { return this; } + @Override + public @NotNull EmbeddedHiveMQBuilder withoutLoggingBootstrap() { + this.enableLoggingBootstrap = false; + return this; + } + @Override public @NotNull EmbeddedHiveMQ build() { // Shim for the old API @@ -67,7 +74,7 @@ public class EmbeddedHiveMQBuilderImpl implements EmbeddedHiveMQBuilder { final File dataFile = dataFolder == null ? null : dataFolder.toFile(); final File extensionsFile = extensionsFolder == null ? null : extensionsFolder.toFile(); - return new EmbeddedHiveMQImpl(confFile, dataFile, extensionsFile, embeddedExtension); + return new EmbeddedHiveMQImpl(confFile, dataFile, extensionsFile, embeddedExtension, enableLoggingBootstrap); } } diff --git a/src/main/java/com/hivemq/embedded/internal/EmbeddedHiveMQImpl.java b/src/main/java/com/hivemq/embedded/internal/EmbeddedHiveMQImpl.java index de55b89ea..b3b508fe2 100644 --- a/src/main/java/com/hivemq/embedded/internal/EmbeddedHiveMQImpl.java +++ b/src/main/java/com/hivemq/embedded/internal/EmbeddedHiveMQImpl.java @@ -64,17 +64,21 @@ class EmbeddedHiveMQImpl implements EmbeddedHiveMQ { private @NotNull LinkedList> stopFutures = new LinkedList<>(); private @Nullable Future shutDownFuture; + private final boolean enableLoggingBootstrap; + EmbeddedHiveMQImpl( final @Nullable File conf, final @Nullable File data, final @Nullable File extensions) { - this(conf, data, extensions, null); + this(conf, data, extensions, null, true); } EmbeddedHiveMQImpl( final @Nullable File conf, final @Nullable File data, final @Nullable File extensions, - final @Nullable EmbeddedExtension embeddedExtension) { + final @Nullable EmbeddedExtension embeddedExtension, + final boolean enableLoggingBootstrap) { this.embeddedExtension = embeddedExtension; + this.enableLoggingBootstrap = enableLoggingBootstrap; log.info("Setting default authentication behavior to ALLOW ALL"); InternalConfigurations.AUTH_DENY_UNAUTHENTICATED_CONNECTIONS.set(false); @@ -146,7 +150,7 @@ private void stateChange() { systemInformation.init(); configurationService = ConfigurationBootstrap.bootstrapConfig(systemInformation); - hiveMQServer = new HiveMQServer(systemInformation, metricRegistry, configurationService, false); + hiveMQServer = new HiveMQServer(systemInformation, metricRegistry, configurationService, enableLoggingBootstrap, false); hiveMQServer.bootstrap(); hiveMQServer.startInstance(embeddedExtension); diff --git a/src/test/java/com/hivemq/embedded/internal/EmbeddedHiveMQImplTest.java b/src/test/java/com/hivemq/embedded/internal/EmbeddedHiveMQImplTest.java index 6c8fc8eed..d84d726b4 100644 --- a/src/test/java/com/hivemq/embedded/internal/EmbeddedHiveMQImplTest.java +++ b/src/test/java/com/hivemq/embedded/internal/EmbeddedHiveMQImplTest.java @@ -268,7 +268,8 @@ public void test_hivemq_uses_embedded_extension_with_normal() throws Exception { final EmbeddedExtensionImpl extension = new EmbeddedExtensionImpl("id", "name", "123", "luke_skywalker", 0, 1000, embeddedMain); - final EmbeddedHiveMQImpl embeddedHiveMQ = new EmbeddedHiveMQImpl(conf, data, extensions, extension); + final EmbeddedHiveMQImpl embeddedHiveMQ = + new EmbeddedHiveMQImpl(conf, data, extensions, extension, true); embeddedHiveMQ.start().get(); assertTrue(embeddedMain.running.get());