赞
踩
spring boot 默认是使用logback来进行日志记录的,但是好多时候,还是喜欢使用log4j,好在,spring boot 支持的~~
首先pom依赖
01 | < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
02 | xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd" > |
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 > |
07 | < parent > |
08 | < groupId >org.springframework.boot</ groupId > |
09 | < artifactId >spring-boot-starter-parent</ artifactId > |
10 | < version >1.1.10.RELEASE</ version > |
11 | </ parent > |
12 |
13 | < dependencies > |
14 | < dependency > |
15 | < groupId >dom4j</ groupId > |
16 | < artifactId >dom4j</ artifactId > |
17 | < version >1.6.1</ version > |
18 | </ dependency > |
19 | < dependency > |
20 | < groupId >org.springframework.boot</ groupId > |
21 | < artifactId >spring-boot-starter-web</ artifactId > |
22 | < exclusions > |
23 | < exclusion > |
24 | < artifactId >log4j-over-slf4j</ artifactId > |
25 | < groupId >org.slf4j</ groupId > |
26 | </ exclusion > |
27 | </ exclusions > |
28 | </ dependency > |
29 | < dependency > |
30 | < groupId >org.springframework.boot</ groupId > |
31 | < artifactId >spring-boot-starter-log4j</ artifactId > |
32 | </ dependency > |
33 |
34 | < dependency > |
35 | < groupId >com.alibaba</ groupId > |
36 | < artifactId >fastjson</ artifactId > |
37 | < version >1.2.4</ version > |
38 | </ dependency > |
39 | </ dependencies > |
40 |
41 | < properties > |
42 | < start-class >com.dutycode.api.weixin.BootMain</ start-class > |
43 | </ properties > |
44 |
45 | < build > |
46 | < plugins > |
47 | < plugin > |
48 | < groupId >org.springframework.boot</ groupId > |
49 | < artifactId >spring-boot-maven-plugin</ artifactId > |
50 | </ plugin > |
51 | </ plugins > |
52 | </ build > |
53 |
54 | < repositories > |
55 | < repository > |
56 | < id >spring-releases</ id > |
57 | < url >https://repo.spring.io/libs-release</ url > |
58 | </ repository > |
59 | </ repositories > |
60 | < pluginRepositories > |
61 | < pluginRepository > |
62 | < id >spring-releases</ id > |
63 | < url >https://repo.spring.io/libs-release</ url > |
64 | </ pluginRepository > |
65 | </ pluginRepositories > |
66 | </ project > |
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; |
02 |
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; |
07 |
08 | @ComponentScan |
09 | @EnableAutoConfiguration |
10 | public class BootMain { |
11 | static { |
12 | try { |
13 | //初始化log4j |
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); |
18 | } catch (Exception e){ |
19 | e.printStackTrace(); |
20 | } |
21 | |
22 | } |
23 | public static void main(String[] args) { |
24 | |
25 | |
26 | SpringApplication.run(BootMain. class , args); |
27 | } |
28 | } |
01 | package com.dutycode.api.weixin.crontrollor; |
02 |
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; |
08 |
09 | import com.alibaba.fastjson.JSONObject; |
10 |
11 | @RestController |
12 | @RequestMapping (value= "/log" ) |
13 | public class LoggerControllor { |
14 |
15 | private Logger logger = Logger.getLogger(LoggerControllor. class ); |
16 | |
17 | @ResponseBody |
18 | @RequestMapping (value= "/sharelog" , method=RequestMethod.POST) |
19 | public String shareLog( int num1){ |
20 | //记录分享日志记录 |
21 | logger.info( "test mesg" + num1); |
22 | JSONObject obj = new JSONObject(); |
23 | obj.put( "num" , num1); |
24 | return obj.toJSONString(); |
25 | } |
26 | } |
反回结果为:{"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。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。