BAEL-1970 improved examples after code review

This commit is contained in:
mprevisic 2019-04-06 20:42:44 +02:00
parent 19ec10a630
commit 5957f65495
10 changed files with 79 additions and 78 deletions

View File

@ -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");
}
}

View File

@ -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);
}
}

View File

@ -5,7 +5,7 @@ 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) {

View File

@ -2,15 +2,19 @@ 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);
@Autowired
private OtherComponent otherComponent;
@GetMapping("/testLogLevel")
public String testLogLevel() {
@ -19,9 +23,7 @@ public class TestLogLevelController {
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");
otherComponent.processData();
return "Added some log output to console...";
}

View File

@ -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

View File

@ -17,9 +17,9 @@ 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
@ -28,9 +28,11 @@ public class TestLogLevelWithProfileIntegrationTest {
@Rule
public OutputCapture outputCapture = new OutputCapture();
private String baseUrl = "/testLogLevel";
@Test
public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintDebugLogsForOurPackage() {
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class);
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
assertThat(response.getStatusCode().value()).isEqualTo(200);
assertThatOutputContainsLogForOurPackage("DEBUG");
@ -38,7 +40,7 @@ public class TestLogLevelWithProfileIntegrationTest {
@Test
public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenNoDebugLogsForOtherPackages() {
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class);
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
assertThat(response.getStatusCode().value()).isEqualTo(200);
assertThatOutputDoesntContainLogForOtherPackages("DEBUG");
@ -46,7 +48,7 @@ public class TestLogLevelWithProfileIntegrationTest {
@Test
public void givenInfoRootLevelAndDebugLevelForOurPackage_whenCall_thenPrintInfoLogs() {
ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8080/testLogLevel", String.class);
ResponseEntity<String> response = restTemplate.getForEntity(baseUrl, String.class);
assertThat(response.getStatusCode().value()).isEqualTo(200);
assertThatOutputContainsLogForOurPackage("INFO");

View File

@ -0,0 +1 @@
logging.config=classpath:logback-test.xml

View File

@ -1 +0,0 @@
logging.config=classpath:logback-testloglevel.xml