spring boot 默认是使用logback来进行日志记录的,但是好多时候,还是喜欢使用log4j,好在,spring boot 支持的~~
首先pom依赖
03 | < modelVersion >4.0.0</ modelVersion > |
04 | < groupId >com.dutycode.api.weixin</ groupId > |
05 | < artifactId >com.dutycode.api.weixin</ artifactId > |
06 | < version >0.0.1-SNAPSHOT</ version > |
08 | < groupId >org.springframework.boot</ groupId > |
09 | < artifactId >spring-boot-starter-parent</ artifactId > |
10 | < version >1.1.10.RELEASE</ version > |
15 | < groupId >dom4j</ groupId > |
16 | < artifactId >dom4j</ artifactId > |
17 | < version >1.6.1</ version > |
20 | < groupId >org.springframework.boot</ groupId > |
21 | < artifactId >spring-boot-starter-web</ artifactId > |
24 | < artifactId >log4j-over-slf4j</ artifactId > |
25 | < groupId >org.slf4j</ groupId > |
30 | < groupId >org.springframework.boot</ groupId > |
31 | < artifactId >spring-boot-starter-log4j</ artifactId > |
35 | < groupId >com.alibaba</ groupId > |
36 | < artifactId >fastjson</ artifactId > |
37 | < version >1.2.4</ version > |
42 | < start-class >com.dutycode.api.weixin.BootMain</ start-class > |
48 | < groupId >org.springframework.boot</ groupId > |
49 | < artifactId >spring-boot-maven-plugin</ artifactId > |
56 | < id >spring-releases</ id > |
62 | < id >spring-releases</ id > |
其中,
spring-boot-starter-web依赖中需要exclude掉log4j-over-slf4j,因为spring-boot-starter-log4j中已经有了log4j的slf4j依赖,如果不去掉,会出现重复依赖的情况。这个时候,在初始化log4j的时候就会报出
Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preemptingStackOverflowError.的异常信息,具体可以参见以下地址:
http://www.slf4j.org/codes.html#log4jDelegationLoop
http://blog.csdn.net/lee_decimal/article/details/38515119
第二步,pom依赖可以了,咱们就可以写测试的方法了
启动Main方法
01 | package com.dutycode.api.weixin; |
03 | import org.apache.log4j.PropertyConfigurator; |
04 | import org.springframework.boot.SpringApplication; |
05 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
06 | import org.springframework.context.annotation.ComponentScan; |
09 | @EnableAutoConfiguration |
10 | public class BootMain { |
14 | String log4jPath = BootMain. class .getClassLoader().getResource( "" ).getPath()+ "/log4j.properties" ; |
15 | System.out.println( "初始化Log4j。。。。" ); |
16 | System.out.println( "path is " + log4jPath); |
17 | PropertyConfigurator.configure(log4jPath); |
23 | public static void main(String[] args) { |
26 | SpringApplication.run(BootMain. class , args); |
测试controllor
01 | package com.dutycode.api.weixin.crontrollor; |
03 | import org.apache.log4j.Logger; |
04 | import org.springframework.web.bind.annotation.RequestMapping; |
05 | import org.springframework.web.bind.annotation.RequestMethod; |
06 | import org.springframework.web.bind.annotation.ResponseBody; |
07 | import org.springframework.web.bind.annotation.RestController; |
09 | import com.alibaba.fastjson.JSONObject; |
12 | @RequestMapping (value= "/log" ) |
13 | public class LoggerControllor { |
15 | private Logger logger = Logger.getLogger(LoggerControllor. class ); |
18 | @RequestMapping (value= "/sharelog" , method=RequestMethod.POST) |
19 | public String shareLog( int num1){ |
21 | logger.info( "test mesg" + num1); |
22 | JSONObject obj = new JSONObject(); |
24 | return obj.toJSONString(); |
使用postman测试如下:
反回结果为:{"num":12313124}
至此,启动可以看到效果了
附:
表 1. Spring Boot 推荐的基础 POM 文件
名称 | 说明 |
spring-boot-starter | 核心 POM,包含自动配置支持、日志库和对 YAML 配置文件的支持。 |
spring-boot-starter-amqp | 通过 spring-rabbit 支持 AMQP。 |
spring-boot-starter-aop | 包含 spring-aop 和 AspectJ 来支持面向切面编程(AOP)。 |
spring-boot-starter-batch | 支持 Spring Batch,包含 HSQLDB。 |
spring-boot-starter-data-jpa | 包含 spring-data-jpa、spring-orm 和 Hibernate 来支持 JPA。 |
spring-boot-starter-data-mongodb | 包含 spring-data-mongodb 来支持 MongoDB。 |
spring-boot-starter-data-rest | 通过 spring-data-rest-webmvc 支持以 REST 方式暴露 Spring Data 仓库。 |
spring-boot-starter-jdbc | 支持使用 JDBC 访问数据库。 |
spring-boot-starter-security | 包含 spring-security。 |
spring-boot-starter-test | 包含常用的测试所需的依赖,如 JUnit、Hamcrest、Mockito 和 spring-test 等。 |
spring-boot-starter-velocity | 支持使用 Velocity 作为模板引擎。 |
spring-boot-starter-web | 支持 Web 应用开发,包含 Tomcat 和 spring-mvc。 |
spring-boot-starter-websocket | 支持使用 Tomcat 开发 WebSocket 应用。 |
spring-boot-starter-ws | 支持 Spring Web Services。 |
spring-boot-starter-actuator | 添加适用于生产环境的功能,如性能指标和监测等功能。 |
spring-boot-starter-remote-shell | 添加远程 SSH 支持。 |
spring-boot-starter-jetty | 使用 Jetty 而不是默认的 Tomcat 作为应用服务器。 |
spring-boot-starter-log4j | 添加 Log4j 的支持。 |
spring-boot-starter-logging | 使用 Spring Boot 默认的日志框架 Logback。 |
spring-boot-starter-tomcat | 使用 Spring Boot 默认的 Tomcat 作为应用服务器。 |
所有这些 POM 依赖的好处在于为开发 Spring 应用提供了一个良好的基础。Spring Boot 所选择的第三方库是经过考虑的,是比较适合产品开发的选择。但是 Spring Boot 也提供了不同的选项,比如日志框架可以用 Logback 或 Log4j,应用服务器可以用 Tomcat 或 Jetty。