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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值