Spring Boot添加记录日志

在Spring Boot 中记录日志只需两步:
1、在 src/main/resources 下面创建logback.xml 文件。


<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <logger name="org.springframework.web" level="DEBUG"/>
configuration>

2、在Java代码中创建实例,并在需要输出日志的地方使用。

private static final Logger logger = LoggerFactory.getLogger(SpringBootSampleApplication.class);

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new MyServlet(), "/xs/*");// ServletName默认值为首字母小写,即myServlet
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringBootSampleApplication.class, args);

        logger.debug("日志输出测试 Debug");
        logger.trace("日志输出测试 Trace");
        logger.info("日志输出测试 Info");
    }

测试:

2018-01-12 16:08:57.248  INFO 956 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-01-12 16:08:59.611  INFO 956 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-01-12 16:08:59.796  INFO 956 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-01-12 16:08:59.810  INFO 956 --- [           main] com.dx.SpringBootSampleApplication       : Started SpringBootSampleApplication in 7.898 seconds (JVM running for 9.363)
2018-01-12 16:08:59.811  WARN 956 --- [           main] com.dx.SpringBootSampleApplication       : 日志输出测试 Trace
2018-01-12 16:08:59.811  INFO 956 --- [           main] com.dx.SpringBootSampleApplication       : 日志输出测试 Info

你可能感兴趣的:(spring-boot,日志)