BAEL-1970 improved examples after code review
This commit is contained in:
parent
19ec10a630
commit
5957f65495
@ -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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -5,11 +5,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|||||||
|
|
||||||
import com.baeldung.boot.Application;
|
import com.baeldung.boot.Application;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication(scanBasePackages = {"com.baeldung.testloglevel", "com.baeldung.component"})
|
||||||
public class TestLogLevelApplication {
|
public class TestLogLevelApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(Application.class, args);
|
SpringApplication.run(Application.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,28 +2,30 @@ package com.baeldung.testloglevel;
|
|||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import com.baeldung.testlog.TestLog;
|
import com.baeldung.component.OtherComponent;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class TestLogLevelController {
|
public class TestLogLevelController {
|
||||||
|
|
||||||
Logger LOG = LoggerFactory.getLogger(this.getClass());
|
private static final Logger LOG = LoggerFactory.getLogger(TestLogLevelController.class);
|
||||||
|
|
||||||
@GetMapping("/testLogLevel")
|
@Autowired
|
||||||
public String testLogLevel() {
|
private OtherComponent otherComponent;
|
||||||
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");
|
@GetMapping("/testLogLevel")
|
||||||
new TestLog().info("This is an INFO log in another package");
|
public String testLogLevel() {
|
||||||
new TestLog().error("This is an ERROR log in another package");
|
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();
|
||||||
}
|
|
||||||
|
|
||||||
}
|
return "Added some log output to console...";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -17,9 +17,9 @@ import org.springframework.test.context.ActiveProfiles;
|
|||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, classes = TestLogLevelApplication.class)
|
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class)
|
||||||
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
|
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
|
||||||
@ActiveProfiles("logback-testloglevel")
|
@ActiveProfiles("logback-test")
|
||||||
public class LogbackTestLogLevelIntegrationTest {
|
public class LogbackTestLogLevelIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@ -17,52 +17,54 @@ import org.springframework.test.context.junit4.SpringRunner;
|
|||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT, classes = TestLogLevelApplication.class)
|
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestLogLevelApplication.class)
|
||||||
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
|
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
|
||||||
@ActiveProfiles("testloglevel")
|
@ActiveProfiles("logging-test")
|
||||||
public class TestLogLevelWithProfileIntegrationTest {
|
public class TestLogLevelWithProfileIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private TestRestTemplate restTemplate;
|
private TestRestTemplate restTemplate;
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public OutputCapture outputCapture = new OutputCapture();
|
public OutputCapture outputCapture = new OutputCapture();
|
||||||
|
|
||||||
@Test
|
private String baseUrl = "/testLogLevel";
|
||||||
public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() {
|
|
||||||
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class);
|
|
||||||
|
|
||||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
@Test
|
||||||
assertThatOutputContainsLogForOurPackage("DEBUG");
|
public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() {
|
||||||
}
|
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||||
|
|
||||||
@Test
|
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||||
public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() {
|
assertThatOutputContainsLogForOurPackage("DEBUG");
|
||||||
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class);
|
}
|
||||||
|
|
||||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
@Test
|
||||||
assertThatOutputDoesntContainLogForOtherPackages("DEBUG");
|
public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() {
|
||||||
}
|
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||||
|
|
||||||
@Test
|
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||||
public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintInfoLogs() {
|
assertThatOutputDoesntContainLogForOtherPackages("DEBUG");
|
||||||
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class);
|
}
|
||||||
|
|
||||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
@Test
|
||||||
assertThatOutputContainsLogForOurPackage("INFO");
|
public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintInfoLogs() {
|
||||||
assertThatOutputContainsLogForOtherPackages("INFO");
|
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
|
||||||
}
|
|
||||||
|
|
||||||
private void assertThatOutputContainsLogForOurPackage(String level) {
|
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||||
assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*");
|
assertThatOutputContainsLogForOurPackage("INFO");
|
||||||
}
|
assertThatOutputContainsLogForOtherPackages("INFO");
|
||||||
|
}
|
||||||
|
|
||||||
private void assertThatOutputDoesntContainLogForOtherPackages(String level) {
|
private void assertThatOutputContainsLogForOurPackage(String level) {
|
||||||
assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).doesNotContain(level);
|
assertThat(outputCapture.toString()).containsPattern("TestLogLevelController.*" + level + ".*");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertThatOutputContainsLogForOtherPackages(String level) {
|
private void assertThatOutputDoesntContainLogForOtherPackages(String level) {
|
||||||
assertThat(outputCapture.toString().replaceAll("(?m)^.*TestLogLevelController.*$", "")).contains(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-test.xml
|
@ -1 +0,0 @@
|
|||||||
logging.config=classpath:logback-testloglevel.xml
|
|
@ -10,4 +10,4 @@
|
|||||||
<appender-ref ref="STDOUT" />
|
<appender-ref ref="STDOUT" />
|
||||||
</root>
|
</root>
|
||||||
<logger name="com.baeldung.testloglevel" level="debug" />
|
<logger name="com.baeldung.testloglevel" level="debug" />
|
||||||
</configuration>
|
</configuration>
|
Loading…
x
Reference in New Issue
Block a user