Skip to content

Commit 0f436c7

Browse files
committed
Configure log explicitly with custom configuration file
Jetty (used in Prometheus support) is verbose when starting, so the default Logback configuration isn't used anymore. As PerfTest cannot embed a logback.xml file at the root of the classpath, a custom file is provided and used explicitly. This can be overriden by using the logback.configurationFile system property. [#158209647] References #42
1 parent 3aff825 commit 0f436c7

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

pom.xml

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@
121121
<artifactId>micrometer-registry-prometheus</artifactId>
122122
<version>${micrometer.version}</version>
123123
</dependency>
124+
<dependency>
125+
<groupId>ch.qos.logback</groupId>
126+
<artifactId>logback-classic</artifactId>
127+
<version>${logback.version}</version>
128+
<optional>true</optional>
129+
</dependency>
130+
<dependency>
131+
<groupId>ch.qos.logback</groupId>
132+
<artifactId>logback-core</artifactId>
133+
<version>${logback.version}</version>
134+
<optional>true</optional>
135+
</dependency>
136+
124137

125138
<dependency>
126139
<groupId>org.junit.jupiter</groupId>
@@ -179,13 +192,6 @@
179192
<scope>test</scope>
180193
</dependency>
181194

182-
<dependency>
183-
<groupId>ch.qos.logback</groupId>
184-
<artifactId>logback-classic</artifactId>
185-
<version>${logback.version}</version>
186-
<scope>test</scope>
187-
</dependency>
188-
189195
<dependency>
190196
<groupId>org.openjdk.jmh</groupId>
191197
<artifactId>jmh-core</artifactId>
@@ -464,6 +470,12 @@
464470
<version>${logback.version}</version>
465471
</dependency>
466472

473+
<dependency>
474+
<groupId>ch.qos.logback</groupId>
475+
<artifactId>logback-core</artifactId>
476+
<version>${logback.version}</version>
477+
</dependency>
478+
467479
</dependencies>
468480
<build>
469481
<plugins>

src/main/java/com/rabbitmq/perf/PerfTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
import java.util.concurrent.TimeUnit;
3131
import java.util.function.Function;
3232

33+
import ch.qos.logback.classic.LoggerContext;
34+
import ch.qos.logback.classic.joran.JoranConfigurator;
35+
import ch.qos.logback.core.joran.spi.JoranException;
36+
import ch.qos.logback.core.util.StatusPrinter;
3337
import com.rabbitmq.client.impl.ClientVersion;
3438
import com.rabbitmq.client.impl.nio.NioParams;
3539
import io.micrometer.core.instrument.MeterRegistry;
@@ -340,10 +344,29 @@ static MulticastSet.CompletionHandler getCompletionHandler(MulticastParams p) {
340344
return completionHandler;
341345
}
342346

343-
public static void main(String[] args) {
347+
public static void main(String[] args) throws IOException {
348+
configureLogbackIfNecessary();
344349
main(args, new PerfTestOptions().setSystemExiter(new JvmSystemExiter()).setSkipSslContextConfiguration(false));
345350
}
346351

352+
private static void configureLogbackIfNecessary() throws IOException {
353+
if (System.getProperty("logback.configurationFile") == null) {
354+
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
355+
InputStream configurationFile = PerfTest.class.getResourceAsStream("/logback-perf-test.xml");
356+
try {
357+
JoranConfigurator configurator = new JoranConfigurator();
358+
configurator.setContext(context);
359+
context.reset();
360+
configurator.doConfigure(configurationFile);
361+
} catch (JoranException je) {
362+
// StatusPrinter will handle this
363+
} finally {
364+
configurationFile.close();
365+
}
366+
StatusPrinter.printInCaseOfErrorsOrWarnings(context);
367+
}
368+
}
369+
347370
private static SSLContext getSslContextIfNecessary(CommandLineProxy cmd, Properties systemProperties) throws NoSuchAlgorithmException {
348371
SSLContext sslContext = null;
349372
if (hasOption(cmd, "udsc") || hasOption(cmd,"useDefaultSslContext")) {
File renamed without changes.

0 commit comments

Comments
 (0)