From 19ec10a6306c56846f2ad18c957c57d1b9f78007 Mon Sep 17 00:00:00 2001 From: mprevisic Date: Sat, 23 Mar 2019 19:04:55 +0100 Subject: [PATCH 1/7] BAEL-1970 spring logging level while testing --- .../java/com/baeldung/testlog/TestLog.java | 22 ++++++ .../testloglevel/TestLogLevelApplication.java | 15 ++++ .../testloglevel/TestLogLevelController.java | 29 ++++++++ ...ltiProfileTestLogLevelIntegrationTest.java | 70 +++++++++++++++++++ .../LogbackTestLogLevelIntegrationTest.java | 70 +++++++++++++++++++ ...estLogLevelWithProfileIntegrationTest.java | 68 ++++++++++++++++++ .../application-logback-test2.properties | 1 + ...pplication-logback-testloglevel.properties | 1 + .../application-testloglevel.properties | 2 + .../test/resources/logback-multiprofile.xml | 18 +++++ .../test/resources/logback-testloglevel.xml | 13 ++++ 11 files changed, 309 insertions(+) create mode 100644 spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java create mode 100644 spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java create mode 100644 spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java create mode 100644 spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java create mode 100644 spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java create mode 100644 spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java create mode 100644 spring-boot-testing/src/test/resources/application-logback-test2.properties create mode 100644 spring-boot-testing/src/test/resources/application-logback-testloglevel.properties create mode 100644 spring-boot-testing/src/test/resources/application-testloglevel.properties create mode 100644 spring-boot-testing/src/test/resources/logback-multiprofile.xml create mode 100644 spring-boot-testing/src/test/resources/logback-testloglevel.xml diff --git a/spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java b/spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java new file mode 100644 index 0000000000..bc5d5700e5 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java @@ -0,0 +1,22 @@ +package com.baeldung.testlog; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TestLog { + + Logger LOG = LoggerFactory.getLogger(this.getClass()); + + public void info(String msg) { + LOG.info(msg); + } + + public void error(String msg) { + LOG.error(msg); + } + + public void trace(String msg) { + LOG.trace(msg); + } + +} diff --git a/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java new file mode 100644 index 0000000000..315bd735b9 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.testloglevel; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import com.baeldung.boot.Application; + +@SpringBootApplication +public class TestLogLevelApplication { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java new file mode 100644 index 0000000000..7349c20b65 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java @@ -0,0 +1,29 @@ +package com.baeldung.testloglevel; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.testlog.TestLog; + +@RestController +public class TestLogLevelController { + + Logger LOG = LoggerFactory.getLogger(this.getClass()); + + @GetMapping("/testLogLevel") + public String testLogLevel() { + LOG.trace("This is a TRACE log"); + LOG.debug("This is a DEBUG log"); + LOG.info("This is an INFO log"); + LOG.error("This is an ERROR log"); + + new TestLog().trace("This is a TRACE log in another package"); + new TestLog().info("This is an INFO log in another package"); + new TestLog().error("This is an ERROR log in another package"); + + return "Added some log output to console..."; + } + +} \ No newline at end of file diff --git a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java new file mode 100644 index 0000000000..5192a1f00e --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java @@ -0,0 +1,70 @@ +package com.baeldung.testloglevel; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class) +@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class) +@ActiveProfiles("logback-test2") +public class LogbackMultiProfileTestLogLevelIntegrationTest { + + @Autowired + private TestRestTemplate restTemplate; + + @Rule + public OutputCapture outputCapture = new OutputCapture(); + + private String baseUrl = "/testLogLevel"; + + @Test + public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintTraceLogsForOurPackage() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("TRACE"); + } + + @Test + public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenNoTraceLogsForOtherPackages() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputDoesntContainLogForOtherPackages("TRACE"); + } + + @Test + public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintErrorLogs() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("ERROR"); + assertThatOutputContainsLogForOtherPackages("ERROR"); + } + + private void assertThatOutputContainsLogForOurPackage(String level) { + assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); + } + + private void assertThatOutputDoesntContainLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); + } + + private void assertThatOutputContainsLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); + } + +} diff --git a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java new file mode 100644 index 0000000000..c0571265a9 --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java @@ -0,0 +1,70 @@ +package com.baeldung.testloglevel; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, classes = TestLogLevelApplication.class) +@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class) +@ActiveProfiles("logback-testloglevel") +public class LogbackTestLogLevelIntegrationTest { + + @Autowired + private TestRestTemplate restTemplate; + + @Rule + public OutputCapture outputCapture = new OutputCapture(); + + private String baseUrl = "/testLogLevel"; + + @Test + public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("DEBUG"); + } + + @Test + public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputDoesntContainLogForOtherPackages("DEBUG"); + } + + @Test + public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintErrorLogs() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("ERROR"); + assertThatOutputContainsLogForOtherPackages("ERROR"); + } + + private void assertThatOutputContainsLogForOurPackage(String level) { + assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); + } + + private void assertThatOutputDoesntContainLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); + } + + private void assertThatOutputContainsLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); + } + +} diff --git a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java new file mode 100644 index 0000000000..976e7d5f76 --- /dev/null +++ b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java @@ -0,0 +1,68 @@ +package com.baeldung.testloglevel; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, classes = TestLogLevelApplication.class) +@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class) +@ActiveProfiles("testloglevel") +public class TestLogLevelWithProfileIntegrationTest { + + @Autowired + private TestRestTemplate restTemplate; + + @Rule + public OutputCapture outputCapture = new OutputCapture(); + + @Test + public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() { + ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("DEBUG"); + } + + @Test + public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() { + ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputDoesntContainLogForOtherPackages("DEBUG"); + } + + @Test + public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintInfoLogs() { + ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class); + + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("INFO"); + assertThatOutputContainsLogForOtherPackages("INFO"); + } + + private void assertThatOutputContainsLogForOurPackage(String level) { + assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); + } + + private void assertThatOutputDoesntContainLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); + } + + private void assertThatOutputContainsLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); + } + +} diff --git a/spring-boot-testing/src/test/resources/application-logback-test2.properties b/spring-boot-testing/src/test/resources/application-logback-test2.properties new file mode 100644 index 0000000000..aeed46e3ca --- /dev/null +++ b/spring-boot-testing/src/test/resources/application-logback-test2.properties @@ -0,0 +1 @@ +logging.config=classpath:logback-multiprofile.xml \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/application-logback-testloglevel.properties b/spring-boot-testing/src/test/resources/application-logback-testloglevel.properties new file mode 100644 index 0000000000..81d9b2e7c6 --- /dev/null +++ b/spring-boot-testing/src/test/resources/application-logback-testloglevel.properties @@ -0,0 +1 @@ +logging.config=classpath:logback-testloglevel.xml \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/application-testloglevel.properties b/spring-boot-testing/src/test/resources/application-testloglevel.properties new file mode 100644 index 0000000000..b5adb4cc11 --- /dev/null +++ b/spring-boot-testing/src/test/resources/application-testloglevel.properties @@ -0,0 +1,2 @@ +logging.level.com.baeldung.testloglevel=DEBUG +logging.level.root=INFO \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/logback-multiprofile.xml b/spring-boot-testing/src/test/resources/logback-multiprofile.xml new file mode 100644 index 0000000000..3711f1ec71 --- /dev/null +++ b/spring-boot-testing/src/test/resources/logback-multiprofile.xml @@ -0,0 +1,18 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/logback-testloglevel.xml b/spring-boot-testing/src/test/resources/logback-testloglevel.xml new file mode 100644 index 0000000000..b02462a155 --- /dev/null +++ b/spring-boot-testing/src/test/resources/logback-testloglevel.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file From 5957f65495b2a15a41f61465006862056817928f Mon Sep 17 00:00:00 2001 From: mprevisic Date: Sat, 6 Apr 2019 20:42:44 +0200 Subject: [PATCH 2/7] BAEL-1970 improved examples after code review --- .../baeldung/component/OtherComponent.java | 19 +++++ .../java/com/baeldung/testlog/TestLog.java | 22 ------ .../testloglevel/TestLogLevelApplication.java | 8 +-- .../testloglevel/TestLogLevelController.java | 30 ++++---- .../LogbackTestLogLevelIntegrationTest.java | 4 +- ...estLogLevelWithProfileIntegrationTest.java | 70 ++++++++++--------- .../application-logback-test.properties | 1 + ...pplication-logback-testloglevel.properties | 1 - ...es => application-logging-test.properties} | 0 ...back-testloglevel.xml => logback-test.xml} | 2 +- 10 files changed, 79 insertions(+), 78 deletions(-) create mode 100644 spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java delete mode 100644 spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java create mode 100644 spring-boot-testing/src/test/resources/application-logback-test.properties delete mode 100644 spring-boot-testing/src/test/resources/application-logback-testloglevel.properties rename spring-boot-testing/src/test/resources/{application-testloglevel.properties => application-logging-test.properties} (100%) rename spring-boot-testing/src/test/resources/{logback-testloglevel.xml => logback-test.xml} (95%) diff --git a/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java b/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java new file mode 100644 index 0000000000..61f5b440c3 --- /dev/null +++ b/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java @@ -0,0 +1,19 @@ +package com.baeldung.component; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +@Component +public class OtherComponent { + + private static final Logger LOG = LoggerFactory.getLogger(OtherComponent.class); + + public void processData() { + LOG.trace("This is a TRACE log from OtherComponent"); + LOG.debug("This is a DEBUG log from OtherComponent"); + LOG.info("This is an INFO log from OtherComponent"); + LOG.error("This is an ERROR log from OtherComponent"); + } + +} diff --git a/spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java b/spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java deleted file mode 100644 index bc5d5700e5..0000000000 --- a/spring-boot-testing/src/main/java/com/baeldung/testlog/TestLog.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.testlog; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class TestLog { - - Logger LOG = LoggerFactory.getLogger(this.getClass()); - - public void info(String msg) { - LOG.info(msg); - } - - public void error(String msg) { - LOG.error(msg); - } - - public void trace(String msg) { - LOG.trace(msg); - } - -} diff --git a/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java index 315bd735b9..ed8218c6a3 100644 --- a/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java +++ b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelApplication.java @@ -5,11 +5,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import com.baeldung.boot.Application; -@SpringBootApplication +@SpringBootApplication(scanBasePackages = {"com.baeldung.testloglevel", "com.baeldung.component"}) public class TestLogLevelApplication { - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } } diff --git a/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java index 7349c20b65..22078562b4 100644 --- a/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java +++ b/spring-boot-testing/src/main/java/com/baeldung/testloglevel/TestLogLevelController.java @@ -2,28 +2,30 @@ package com.baeldung.testloglevel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; -import com.baeldung.testlog.TestLog; +import com.baeldung.component.OtherComponent; @RestController public class TestLogLevelController { - Logger LOG = LoggerFactory.getLogger(this.getClass()); + private static final Logger LOG = LoggerFactory.getLogger(TestLogLevelController.class); - @GetMapping("/testLogLevel") - public String testLogLevel() { - LOG.trace("This is a TRACE log"); - LOG.debug("This is a DEBUG log"); - LOG.info("This is an INFO log"); - LOG.error("This is an ERROR log"); + @Autowired + private OtherComponent otherComponent; - new TestLog().trace("This is a TRACE log in another package"); - new TestLog().info("This is an INFO log in another package"); - new TestLog().error("This is an ERROR log in another package"); + @GetMapping("/testLogLevel") + public String testLogLevel() { + LOG.trace("This is a TRACE log"); + LOG.debug("This is a DEBUG log"); + LOG.info("This is an INFO log"); + LOG.error("This is an ERROR log"); - return "Added some log output to console..."; - } + otherComponent.processData(); -} \ No newline at end of file + return "Added some log output to console..."; + } + +} diff --git a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java index c0571265a9..5fb40a2f36 100644 --- a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java +++ b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java @@ -17,9 +17,9 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, classes = TestLogLevelApplication.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class) @EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class) -@ActiveProfiles("logback-testloglevel") +@ActiveProfiles("logback-test") public class LogbackTestLogLevelIntegrationTest { @Autowired diff --git a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java index 976e7d5f76..5609ce6c01 100644 --- a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java +++ b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java @@ -17,52 +17,54 @@ import org.springframework.test.context.junit4.SpringRunner; import static org.assertj.core.api.Assertions.assertThat; @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, classes = TestLogLevelApplication.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class) @EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class) -@ActiveProfiles("testloglevel") +@ActiveProfiles("logging-test") public class TestLogLevelWithProfileIntegrationTest { - @Autowired - private TestRestTemplate restTemplate; + @Autowired + private TestRestTemplate restTemplate; - @Rule - public OutputCapture outputCapture = new OutputCapture(); + @Rule + public OutputCapture outputCapture = new OutputCapture(); - @Test - public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() { - ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class); + private String baseUrl = "/testLogLevel"; - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputContainsLogForOurPackage("DEBUG"); - } + @Test + public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - @Test - public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() { - ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class); + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("DEBUG"); + } - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputDoesntContainLogForOtherPackages("DEBUG"); - } + @Test + public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - @Test - public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintInfoLogs() { - ResponseEntity response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class); + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputDoesntContainLogForOtherPackages("DEBUG"); + } - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputContainsLogForOurPackage("INFO"); - assertThatOutputContainsLogForOtherPackages("INFO"); - } + @Test + public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintInfoLogs() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - private void assertThatOutputContainsLogForOurPackage(String level) { - assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); - } + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("INFO"); + assertThatOutputContainsLogForOtherPackages("INFO"); + } - private void assertThatOutputDoesntContainLogForOtherPackages(String level) { - assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); - } + private void assertThatOutputContainsLogForOurPackage(String level) { + assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); + } - private void assertThatOutputContainsLogForOtherPackages(String level) { - assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); - } + private void assertThatOutputDoesntContainLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); + } + + private void assertThatOutputContainsLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); + } } diff --git a/spring-boot-testing/src/test/resources/application-logback-test.properties b/spring-boot-testing/src/test/resources/application-logback-test.properties new file mode 100644 index 0000000000..587302fa01 --- /dev/null +++ b/spring-boot-testing/src/test/resources/application-logback-test.properties @@ -0,0 +1 @@ +logging.config=classpath:logback-test.xml diff --git a/spring-boot-testing/src/test/resources/application-logback-testloglevel.properties b/spring-boot-testing/src/test/resources/application-logback-testloglevel.properties deleted file mode 100644 index 81d9b2e7c6..0000000000 --- a/spring-boot-testing/src/test/resources/application-logback-testloglevel.properties +++ /dev/null @@ -1 +0,0 @@ -logging.config=classpath:logback-testloglevel.xml \ No newline at end of file diff --git a/spring-boot-testing/src/test/resources/application-testloglevel.properties b/spring-boot-testing/src/test/resources/application-logging-test.properties similarity index 100% rename from spring-boot-testing/src/test/resources/application-testloglevel.properties rename to spring-boot-testing/src/test/resources/application-logging-test.properties diff --git a/spring-boot-testing/src/test/resources/logback-testloglevel.xml b/spring-boot-testing/src/test/resources/logback-test.xml similarity index 95% rename from spring-boot-testing/src/test/resources/logback-testloglevel.xml rename to spring-boot-testing/src/test/resources/logback-test.xml index b02462a155..9c6df17024 100644 --- a/spring-boot-testing/src/test/resources/logback-testloglevel.xml +++ b/spring-boot-testing/src/test/resources/logback-test.xml @@ -10,4 +10,4 @@ - \ No newline at end of file + From 2674f7abff53137b5680ac2d9ecc5c86bbc3e9cb Mon Sep 17 00:00:00 2001 From: mprevisic Date: Sat, 6 Apr 2019 20:45:08 +0200 Subject: [PATCH 3/7] BAEL-1970 minor log message correction --- .../main/java/com/baeldung/component/OtherComponent.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java b/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java index 61f5b440c3..97f001a9e5 100644 --- a/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java +++ b/spring-boot-testing/src/main/java/com/baeldung/component/OtherComponent.java @@ -10,10 +10,10 @@ public class OtherComponent { private static final Logger LOG = LoggerFactory.getLogger(OtherComponent.class); public void processData() { - LOG.trace("This is a TRACE log from OtherComponent"); - LOG.debug("This is a DEBUG log from OtherComponent"); - LOG.info("This is an INFO log from OtherComponent"); - LOG.error("This is an ERROR log from OtherComponent"); + LOG.trace("This is a TRACE log from another package"); + LOG.debug("This is a DEBUG log from another package"); + LOG.info("This is an INFO log from another package"); + LOG.error("This is an ERROR log from another package"); } } From 7186d96b12c4906243828b5875a2bc4e10e7121f Mon Sep 17 00:00:00 2001 From: mprevisic Date: Wed, 10 Apr 2019 15:28:37 +0200 Subject: [PATCH 4/7] BAEL-1970 fixed formatting (use spaces instead of tabs) --- ...ltiProfileTestLogLevelIntegrationTest.java | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java index 5192a1f00e..7a1eb4adbe 100644 --- a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java +++ b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java @@ -22,49 +22,49 @@ import org.springframework.test.context.junit4.SpringRunner; @ActiveProfiles("logback-test2") public class LogbackMultiProfileTestLogLevelIntegrationTest { - @Autowired - private TestRestTemplate restTemplate; + @Autowired + private TestRestTemplate restTemplate; - @Rule - public OutputCapture outputCapture = new OutputCapture(); + @Rule + public OutputCapture outputCapture = new OutputCapture(); - private String baseUrl = "/testLogLevel"; + private String baseUrl = "/testLogLevel"; - @Test - public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintTraceLogsForOurPackage() { - ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + @Test + public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintTraceLogsForOurPackage() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputContainsLogForOurPackage("TRACE"); - } + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("TRACE"); + } - @Test - public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenNoTraceLogsForOtherPackages() { - ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + @Test + public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenNoTraceLogsForOtherPackages() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputDoesntContainLogForOtherPackages("TRACE"); - } + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputDoesntContainLogForOtherPackages("TRACE"); + } - @Test - public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintErrorLogs() { - ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + @Test + public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintErrorLogs() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputContainsLogForOurPackage("ERROR"); - assertThatOutputContainsLogForOtherPackages("ERROR"); - } + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("ERROR"); + assertThatOutputContainsLogForOtherPackages("ERROR"); + } - private void assertThatOutputContainsLogForOurPackage(String level) { - assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); - } + private void assertThatOutputContainsLogForOurPackage(String level) { + assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); + } - private void assertThatOutputDoesntContainLogForOtherPackages(String level) { - assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); - } + private void assertThatOutputDoesntContainLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); + } - private void assertThatOutputContainsLogForOtherPackages(String level) { - assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); - } + private void assertThatOutputContainsLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); + } } From c3bd97f39a67cb38f3adf850b1f88d3642b3ff3b Mon Sep 17 00:00:00 2001 From: mprevisic Date: Wed, 10 Apr 2019 15:52:47 +0200 Subject: [PATCH 5/7] BAEL-1970 fixed formatting in xml files --- .../test/resources/logback-multiprofile.xml | 34 +++++++++---------- .../src/test/resources/logback-test.xml | 22 ++++++------ 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/spring-boot-testing/src/test/resources/logback-multiprofile.xml b/spring-boot-testing/src/test/resources/logback-multiprofile.xml index 3711f1ec71..09ca6e4cac 100644 --- a/spring-boot-testing/src/test/resources/logback-multiprofile.xml +++ b/spring-boot-testing/src/test/resources/logback-multiprofile.xml @@ -1,18 +1,18 @@ - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - - - - - - \ No newline at end of file + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + diff --git a/spring-boot-testing/src/test/resources/logback-test.xml b/spring-boot-testing/src/test/resources/logback-test.xml index 9c6df17024..312af7c2bf 100644 --- a/spring-boot-testing/src/test/resources/logback-test.xml +++ b/spring-boot-testing/src/test/resources/logback-test.xml @@ -1,13 +1,13 @@ - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + From 6ae1e85d1361b62f5762129c2664e2fbe822ea25 Mon Sep 17 00:00:00 2001 From: mprevisic Date: Fri, 12 Apr 2019 09:13:59 +0200 Subject: [PATCH 6/7] BAEL-1970 fixed formatting --- .../LogbackTestLogLevelIntegrationTest.java | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java index 5fb40a2f36..af3bafdc2e 100644 --- a/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java +++ b/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java @@ -22,49 +22,49 @@ import org.springframework.test.context.junit4.SpringRunner; @ActiveProfiles("logback-test") public class LogbackTestLogLevelIntegrationTest { - @Autowired - private TestRestTemplate restTemplate; + @Autowired + private TestRestTemplate restTemplate; - @Rule - public OutputCapture outputCapture = new OutputCapture(); + @Rule + public OutputCapture outputCapture = new OutputCapture(); - private String baseUrl = "/testLogLevel"; + private String baseUrl = "/testLogLevel"; - @Test - public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() { - ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + @Test + public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputContainsLogForOurPackage("DEBUG"); - } + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("DEBUG"); + } - @Test - public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() { - ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + @Test + public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputDoesntContainLogForOtherPackages("DEBUG"); - } + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputDoesntContainLogForOtherPackages("DEBUG"); + } - @Test - public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintErrorLogs() { - ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); + @Test + public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintErrorLogs() { + ResponseEntity response = restTemplate.getForEntity(baseUrl, String.class); - assertThat(response.getStatusCode().value()).isEqualTo(200); - assertThatOutputContainsLogForOurPackage("ERROR"); - assertThatOutputContainsLogForOtherPackages("ERROR"); - } + assertThat(response.getStatusCode().value()).isEqualTo(200); + assertThatOutputContainsLogForOurPackage("ERROR"); + assertThatOutputContainsLogForOtherPackages("ERROR"); + } - private void assertThatOutputContainsLogForOurPackage(String level) { - assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); - } + private void assertThatOutputContainsLogForOurPackage(String level) { + assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*"); + } - private void assertThatOutputDoesntContainLogForOtherPackages(String level) { - assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); - } + private void assertThatOutputDoesntContainLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level); + } - private void assertThatOutputContainsLogForOtherPackages(String level) { - assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); - } + private void assertThatOutputContainsLogForOtherPackages(String level) { + assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(level); + } } From 03a369d153874f5f89ef613691e500e8c83d4ad3 Mon Sep 17 00:00:00 2001 From: mprevisic Date: Sat, 13 Apr 2019 10:36:17 +0200 Subject: [PATCH 7/7] BAEL-1970 fixed logback base.xml path --- spring-boot-testing/src/test/resources/logback-multiprofile.xml | 2 +- spring-boot-testing/src/test/resources/logback-test.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-testing/src/test/resources/logback-multiprofile.xml b/spring-boot-testing/src/test/resources/logback-multiprofile.xml index 09ca6e4cac..be790234f2 100644 --- a/spring-boot-testing/src/test/resources/logback-multiprofile.xml +++ b/spring-boot-testing/src/test/resources/logback-multiprofile.xml @@ -1,5 +1,5 @@ - + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n diff --git a/spring-boot-testing/src/test/resources/logback-test.xml b/spring-boot-testing/src/test/resources/logback-test.xml index 312af7c2bf..0528aa88f3 100644 --- a/spring-boot-testing/src/test/resources/logback-test.xml +++ b/spring-boot-testing/src/test/resources/logback-test.xml @@ -1,5 +1,5 @@ - + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n