-
Notifications
You must be signed in to change notification settings - Fork 70
Spring Boot
luoml edited this page Jul 22, 2017
·
7 revisions
Spring Boot 支持日志框架:Java Util Logging , Log4J2 及 Logback,默认使用 Logback 。
- 默认配置文件方式
// application.properties
logging.file=spring-boot-logback.log
logging.level.root=WARN
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
- 引用外部配置文件
Spring Boot 默认会加载 classpath:logback-spring.xml
或 classpath:logback-spring.groovy
。
// application.properties
// 自定义文件名
logging.config=classpath:my-logback.xml
// my-logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="PATTERN" value="%d [%t] %p %c - %m%n" />
<springProfile name="default">
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>spring-boot-log.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>spring-boot-logback.%d{yyyyMMdd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${PATTERN}</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${PATTERN}</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</springProfile>
</configuration>
- 配置 POM 文件
- 排除
spring-boot-starter-logging
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
- 引入
spring-boot-starter-log4j2
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
- 配置
log4j2.xml
Spring Boot 默认会加载logging.config=classpath:log4j2-spring.xml
或logging.config=classpath:log4j2.xml
。
// log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.springframework.web.servlet" level="info">
<AppenderRef ref="Console"/>
</Logger>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Spring Boot 默认使用 Jackson 。
参考 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#appendix
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
<!-- web mvc、restful、jackson、aop、...... -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- JSP标准标签库 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
spring.mvc.view.prefix=/WEB-INF/jsp
spring.mvc.view.suffix=.jsp
在 Spring Boot 中,自定义 Servlet 有两种方式。
- 配置 POM 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 自定义 Servlet ,继承 HttpServlet
public class MyServlet1 extends HttpServlet {
private static final long serialVersionUID = -7344886891091037086L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.print("MyServlet1");
out.flush();
out.close();
}
}
- 注册 Servlet
@Bean
public ServletRegistrationBean registerServlet() {
return new ServletRegistrationBean(new MyServlet1(), "/servlet/myservlet1");
}
- 配置 POM 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 自定义 Servlet ,继承 HttpServlet;注意增加
@WebServlet
注解
@WebServlet("/servlet/myservlet2")
public class MyServlet2 extends HttpServlet {
private static final long serialVersionUID = -7344886891091037086L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.print("MyServlet2");
out.flush();
out.close();
}
}
- 在 Spring Boot 启动类,启用
@ServletComponentScan
,扫描 Servlet
@ServletComponentScan
@SpringBootApplication
public class SpringBootServletApplication {
......
}