天天看点

SpringBoot(八):SpringBoot整合Log4j

版权声明:本文为博主原创文章,转载请注明出处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" target="_blank" rel="external nofollow" >
                          <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} %p %c{}:%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

四、部分日志打印效果

-- ::,  INFO SimpleUrlHandlerMapping: - Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
-- ::,  INFO OptionalLiveReloadServer: - LiveReload server is running on port 35729
-- ::,  INFO AnnotationMBeanExporter: - Registering beans for JMX exposure on startup
-- ::,  INFO AnnotationMBeanExporter: - Bean with name 'druidDataSource' has been autodetected for JMX exposure
-- ::,  INFO AnnotationMBeanExporter: - Located MBean 'druidDataSource': registering with JMX server as MBean [com.alibaba.druid.pool:name=druidDataSource,type=DruidDataSource]
-- ::,  INFO TomcatEmbeddedServletContainer: - Tomcat started on port(s): 8088 (http)
-- ::,  INFO MainApplication: - Started MainApplication in  seconds (JVM running for )
-- ::,  INFO DispatcherServlet: - FrameworkServlet 'dispatcherServlet': initialization started
-- ::,  INFO DispatcherServlet: - FrameworkServlet 'dispatcherServlet': initialization completed in  ms
-- ::,  INFO HelloController: - hello world           
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-258a4616f7.css" target="_blank" rel="external nofollow"  rel="stylesheet">
              </div>
           

继续阅读