From 3ed5a723bbed34c63dd2c8d26c653f24d2a282c1 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 30 Dec 2016 15:50:45 +0100 Subject: [PATCH] Polish log4j2 sample --- .../pom.xml | 20 +++++--- .../actuator/log4j2/HelloWorldService.java | 32 ------------ .../actuator/log4j2/SampleController.java | 47 ----------------- .../actuator/log4j2/ServiceProperties.java | 36 ------------- .../src/main/resources/application.properties | 19 +------ .../src/main/resources/log4j2.xml | 3 +- .../SampleActuatorLog4J2ApplicationTests.java | 51 ++++++++++++------- 7 files changed, 47 insertions(+), 161 deletions(-) delete mode 100644 spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/HelloWorldService.java delete mode 100644 spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleController.java delete mode 100644 spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/ServiceProperties.java diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j2/pom.xml b/spring-boot-samples/spring-boot-sample-actuator-log4j2/pom.xml index ba9da3c5ae..b99d08f234 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j2/pom.xml +++ b/spring-boot-samples/spring-boot-sample-actuator-log4j2/pom.xml @@ -21,7 +21,7 @@ org.springframework.boot - spring-boot-starter-actuator + spring-boot-starter org.springframework.boot @@ -29,24 +29,28 @@ + + org.springframework.boot + spring-boot-starter-log4j2 + + org.springframework.boot spring-boot-starter-web org.springframework.boot - spring-boot-starter-log4j2 + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-security + org.springframework.boot spring-boot-starter-test test - - - org.springframework.boot - spring-boot-starter-logging - - diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/HelloWorldService.java b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/HelloWorldService.java deleted file mode 100644 index a31b5a4fae..0000000000 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/HelloWorldService.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2012-2014 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package sample.actuator.log4j2; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class HelloWorldService { - - @Autowired - private ServiceProperties configuration; - - public String getHelloMessage() { - return "Hello " + this.configuration.getName(); - } - -} diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleController.java b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleController.java deleted file mode 100644 index 53a87ac45f..0000000000 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/SampleController.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2012-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package sample.actuator.log4j2; - -import java.util.Collections; -import java.util.Map; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; - -@Controller -public class SampleController { - - @Autowired - private HelloWorldService helloWorldService; - - @GetMapping("/") - @ResponseBody - public Map helloWorld() { - return Collections.singletonMap("message", - this.helloWorldService.getHelloMessage()); - } - - @RequestMapping("/foo") - @ResponseBody - public String foo() { - throw new IllegalArgumentException("Server error"); - } - -} diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/ServiceProperties.java b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/ServiceProperties.java deleted file mode 100644 index 94945534a2..0000000000 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/java/sample/actuator/log4j2/ServiceProperties.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 2012-2014 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package sample.actuator.log4j2; - -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; - -@ConfigurationProperties(prefix = "service", ignoreUnknownFields = false) -@Component -public class ServiceProperties { - - private String name = "World"; - - public String getName() { - return this.name; - } - - public void setName(String name) { - this.name = name; - } - -} diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/resources/application.properties b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/resources/application.properties index 23b700c536..efe0d6cd97 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/resources/application.properties +++ b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/resources/application.properties @@ -1,18 +1,3 @@ -#logging.file=/tmp/logs/app.log -#server.port=8080 -#management.port=8080 -management.address=127.0.0.1 -management.shell.ssh.enabled=true -management.shell.ssh.port=2222 -#management.shell.telnet.enabled=false -#management.shell.telnet.port=1111 -management.shell.auth.type=spring -#management.shell.auth.type=key -#management.shell.auth.key.path=${user.home}/test/id_rsa.pub.pem -#management.shell.auth.type=simple - endpoints.shutdown.enabled=true -server.tomcat.basedir=target/tomcat -server.tomcat.access_log_pattern=%h %t "%r" %s %b -security.require_ssl=false -service.name=Daniel + +management.security.enabled=false \ No newline at end of file diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/resources/log4j2.xml b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/resources/log4j2.xml index 519e37c7c1..2f385bde1a 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/resources/log4j2.xml +++ b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/main/resources/log4j2.xml @@ -2,7 +2,7 @@ ???? - %clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%wEx + %clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx @@ -13,7 +13,6 @@ - diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java index 72c73c3482..d31440ba83 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java @@ -16,43 +16,56 @@ package sample.actuator.log4j2; -import java.util.Map; - +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.test.annotation.DirtiesContext; +import org.springframework.boot.test.rule.OutputCapture; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; -import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /** - * Basic integration tests for service demo application. + * Tests for {@link SampleActuatorLog4J2Application}. * * @author Dave Syer + * @@author Stephane Nicoll */ @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) -@DirtiesContext +@SpringBootTest +@AutoConfigureMockMvc public class SampleActuatorLog4J2ApplicationTests { + private static final Logger logger = LogManager.getLogger(SampleActuatorLog4J2ApplicationTests.class); + + @Rule + public OutputCapture output = new OutputCapture(); + @Autowired - private TestRestTemplate restTemplate; + private MockMvc mvc; + + @Test + public void testLogger() { + logger.info("Hello World"); + this.output.expect(containsString("Hello World")); + } @Test - public void testHome() throws Exception { - @SuppressWarnings("rawtypes") - ResponseEntity entity = this.restTemplate.getForEntity("/", Map.class); - assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK); - @SuppressWarnings("unchecked") - Map body = entity.getBody(); - assertThat(body.get("message")).isEqualTo("Hello Daniel"); + public void validateLoggersEndpoint() throws Exception { + this.mvc.perform(get("/loggers/org.apache.coyote.http11.Http11NioProtocol")) + .andExpect(status().isOk()) + .andExpect(content().string(equalTo( + "{\"configuredLevel\":\"WARN\"," + "\"effectiveLevel\":\"WARN\"}"))); } }