SpringBoot(八):SpringBoot整合Log4j

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 rel="stylesheet" href="http://csdnimg.cn/release/phoenix/production/markdown_views-10f5517761.css">
                                </div>

转载自:http://blog.csdn.net/saytime

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 rel="stylesheet" href="http://csdnimg.cn/release/phoenix/production/markdown_views-10f5517761.css">
                                </div>

转载自:http://blog.csdn.net/saytime

猜你喜欢

转载自blog.csdn.net/u013181912/article/details/79057363
今日推荐