Skip to content

Commit e46fc9b

Browse files
artembilangaryrussell
authored andcommitted
INT-4407: Add merge option to Log4j2LevelAdjuster
JIRA: https://jira.spring.io/browse/INT-4407 To make a `Log4j2LevelAdjuster` more builder friendly, add a `merge` option to the overloaded `classes()` and `categories()` methods. This way the existing (or previously configured) `categories` and `classes` are merged with the provided values and default `org.springframework.integration` category can be reuse. Note: this behavior can be reconsidered as default one in the `5.1`
1 parent 4bb4a01 commit e46fc9b

File tree

6 files changed

+40
-12
lines changed

6 files changed

+40
-12
lines changed

spring-integration-amqp/src/test/java/org/springframework/integration/amqp/outbound/AsyncAmqpGatewayTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ public class AsyncAmqpGatewayTests {
7979
@Rule
8080
public Log4j2LevelAdjuster adjuster =
8181
Log4j2LevelAdjuster.trace()
82-
.categories("org.springframework.integration",
83-
"org.springframework.amqp");
82+
.categories(true, "org.springframework.amqp");
8483

8584
@AfterClass
8685
public static void tearDown() {

spring-integration-ftp/src/test/java/org/springframework/integration/ftp/inbound/FtpStreamingMessageSourceTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2017 the original author or authors.
2+
* Copyright 2016-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -57,6 +57,7 @@
5757
/**
5858
* @author Gary Russell
5959
* @author Artem Bilan
60+
*
6061
* @since 4.3
6162
*
6263
*/
@@ -74,8 +75,9 @@ public class FtpStreamingMessageSourceTests extends FtpTestSupport {
7475
private SourcePollingChannelAdapter adapter;
7576

7677
@Rule
77-
public Log4j2LevelAdjuster adjuster = Log4j2LevelAdjuster.debug()
78-
.categories("org.springframework.integration", "org.apache.commons");
78+
public Log4j2LevelAdjuster adjuster =
79+
Log4j2LevelAdjuster.debug()
80+
.categories(true, "org.apache.commons");
7981

8082
@SuppressWarnings("unchecked")
8183
@Test

spring-integration-jms/src/test/java/org/springframework/integration/jms/JmsOutboundGatewayTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public class JmsOutboundGatewayTests extends ActiveMQMultiContextTests {
7575
@Rule
7676
public Log4j2LevelAdjuster adjuster =
7777
Log4j2LevelAdjuster.trace()
78-
.categories("org.springframework.integration", "org.springframework.jms", "org.apache");
78+
.categories(true, "org.springframework.jms", "org.apache");
7979

8080
@Test
8181
public void testContainerBeanNameWhenNoGatewayBeanName() {

spring-integration-redis/src/test/java/org/springframework/integration/redis/store/DelayerHandlerRescheduleIntegrationTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public class DelayerHandlerRescheduleIntegrationTests extends RedisAvailableTest
6060
@Rule
6161
public Log4j2LevelAdjuster adjuster =
6262
Log4j2LevelAdjuster.trace()
63-
.categories("org.springframework.integration",
64-
"org.springframework.data.redis");
63+
.categories(true, "org.springframework.data.redis");
6564

6665
@Test
6766
@RedisAvailable

spring-integration-stomp/src/test/java/org/springframework/integration/stomp/client/StompServerIntegrationTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ public class StompServerIntegrationTests {
8181
@Rule
8282
public Log4j2LevelAdjuster adjuster =
8383
Log4j2LevelAdjuster.trace()
84-
.categories("org.springframework",
85-
"org.springframework.integration",
84+
.categories(true, "org.springframework",
8685
"org.apache.activemq.broker",
8786
"reactor.ipc",
8887
"io.netty");

spring-integration-test-support/src/main/java/org/springframework/integration/test/rule/Log4j2LevelAdjuster.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,22 @@ public void evaluate() throws Throwable {
163163
* @return a Log4j2LevelAdjuster copy with the provided classes
164164
*/
165165
public Log4j2LevelAdjuster classes(Class<?>... classes) {
166-
return new Log4j2LevelAdjuster(this.level, classes, this.categories);
166+
return classes(false, classes);
167+
}
168+
169+
/**
170+
* Specify the classes for logging level adjusting configured before.
171+
* A new copy Log4j2LevelAdjuster instance is produced by this method.
172+
* The provided classes parameter can be merged with existing value in the {@link #classes}.
173+
* @param merge to merge or not with previously configured {@link #classes}
174+
* @param classes the classes to use for logging level adjusting
175+
* @return a Log4j2LevelAdjuster copy with the provided classes
176+
* @since 5.0.2
177+
*/
178+
public Log4j2LevelAdjuster classes(boolean merge, Class<?>... classes) {
179+
return new Log4j2LevelAdjuster(this.level,
180+
merge ? Stream.of(this.classes, classes).flatMap(Stream::of).toArray(Class<?>[]::new) : classes,
181+
this.categories);
167182
}
168183

169184
/**
@@ -174,7 +189,21 @@ public Log4j2LevelAdjuster classes(Class<?>... classes) {
174189
* @return a Log4j2LevelAdjuster copy with the provided categories
175190
*/
176191
public Log4j2LevelAdjuster categories(String... categories) {
177-
return new Log4j2LevelAdjuster(this.level, this.classes, categories);
192+
return categories(false, categories);
193+
}
194+
195+
/**
196+
* Specify the categories for logging level adjusting configured before.
197+
* A new copy Log4j2LevelAdjuster instance is produced by this method.
198+
* The provided categories parameter can be merged with existing value in the {@link #categories}.
199+
* @param merge to merge or not with previously configured {@link #categories}
200+
* @param categories the categories to use for logging level adjusting
201+
* @return a Log4j2LevelAdjuster copy with the provided categories
202+
* @since 5.0.2
203+
*/
204+
public Log4j2LevelAdjuster categories(boolean merge, String... categories) {
205+
return new Log4j2LevelAdjuster(this.level, this.classes,
206+
merge ? Stream.of(this.categories, categories).flatMap(Stream::of).toArray(String[]::new) : categories);
178207
}
179208

180209
/**

0 commit comments

Comments
 (0)