BAEL-1970 spring logging level while testing
This commit is contained in:
parent
b9ffaea139
commit
19ec10a630
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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...";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||||
|
|
||||||
|
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||||
|
assertThatOutputContainsLogForOurPackage("TRACE");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenNoTraceLogsForOtherPackages() {
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||||
|
|
||||||
|
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||||
|
assertThatOutputDoesntContainLogForOtherPackages("TRACE");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenErrorRootLevelAndTraceLevelForOurPackage_whenCall_thenPrintErrorLogs() {
|
||||||
|
ResponseEntity<String> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||||
|
|
||||||
|
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||||
|
assertThatOutputContainsLogForOurPackage("DEBUG");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() {
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||||
|
|
||||||
|
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||||
|
assertThatOutputDoesntContainLogForOtherPackages("DEBUG");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenErrorRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintErrorLogs() {
|
||||||
|
ResponseEntity<String> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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<String> response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class);
|
||||||
|
|
||||||
|
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||||
|
assertThatOutputContainsLogForOurPackage("DEBUG");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() {
|
||||||
|
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class);
|
||||||
|
|
||||||
|
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||||
|
assertThatOutputDoesntContainLogForOtherPackages("DEBUG");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintInfoLogs() {
|
||||||
|
ResponseEntity<String> 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
logging.config=classpath:logback-multiprofile.xml
|
@ -0,0 +1 @@
|
|||||||
|
logging.config=classpath:logback-testloglevel.xml
|
@ -0,0 +1,2 @@
|
|||||||
|
logging.level.com.baeldung.testloglevel=DEBUG
|
||||||
|
logging.level.root=INFO
|
@ -0,0 +1,18 @@
|
|||||||
|
<configuration>
|
||||||
|
<include resource="/org/springframework/boot/logging/logback/basse.xml" />
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
<root level="error">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
<springProfile name="logback-test1">
|
||||||
|
<logger name="com.baeldung.testloglevel" level="info" />
|
||||||
|
</springProfile>
|
||||||
|
<springProfile name="logback-test2">
|
||||||
|
<logger name="com.baeldung.testloglevel" level="trace" />
|
||||||
|
</springProfile>
|
||||||
|
</configuration>
|
@ -0,0 +1,13 @@
|
|||||||
|
<configuration>
|
||||||
|
<include resource="/org/springframework/boot/logging/logback/basse.xml" />
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
<root level="error">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
<logger name="com.baeldung.testloglevel" level="debug" />
|
||||||
|
</configuration>
|
Loading…
x
Reference in New Issue
Block a user