版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/saytime https://blog.csdn.net/saytime/article/details/78962999 </div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-f57960eb32.css">
<div id="content_views" class="markdown_views">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
<blockquote>
SpringBoot默认使用日志框架logback
一、依赖
<!-- spring boot start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<!-- 排除自带的logback依赖 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- springboot-log4j -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
二、Log4j配置文件
# Log4j配置
log4j.rootCategory=INFO,stdout
# 控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
- 1
- 2
- 3
- 4
- 5
- 6
- 7
三、使用Log4j
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zh
* @ClassName cn.aduu.web.HelloController
* @Description
*/
@RestController
public class HelloController{
private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
@RequestMapping("hello")
public String hello() throws JsonProcessingException {
logger.info("hello world");
return "hello world";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
四、部分日志打印效果
2017-10-28 20:38:35,522 INFO SimpleUrlHandlerMapping:362 - Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-10-28 20:38:35,564 INFO OptionalLiveReloadServer:58 - LiveReload server is running on port 35729
2017-10-28 20:38:35,588 INFO AnnotationMBeanExporter:431 - Registering beans for JMX exposure on startup
2017-10-28 20:38:35,589 INFO AnnotationMBeanExporter:916 - Bean with name 'druidDataSource' has been autodetected for JMX exposure
2017-10-28 20:38:35,590 INFO AnnotationMBeanExporter:671 - Located MBean 'druidDataSource': registering with JMX server as MBean [com.alibaba.druid.pool:name=druidDataSource,type=DruidDataSource]
2017-10-28 20:38:35,603 INFO TomcatEmbeddedServletContainer:201 - Tomcat started on port(s): 8088 (http)
2017-10-28 20:38:35,605 INFO MainApplication:57 - Started MainApplication in 0.911 seconds (JVM running for 259.963)
2017-10-28 20:38:36,607 INFO DispatcherServlet:489 - FrameworkServlet 'dispatcherServlet': initialization started
2017-10-28 20:38:36,609 INFO DispatcherServlet:508 - FrameworkServlet 'dispatcherServlet': initialization completed in 2 ms
2017-10-28 20:38:36,610 INFO HelloController:27 - hello world
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-258a4616f7.css" rel="stylesheet">
</div>