springboot工作总结

springboot 日志

2017-08-01  本文已影响104人  二月_春风

先说个结论:
Springboot默认支持的是logback日志控件。

定义一个UserDao

package com.zhihao.miao.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class UserDao {

    private Logger logger = LoggerFactory.getLogger(getClass());

    public void addUser(){
        logger.debug("user dao debug log");
        logger.info("user dao info log");
        logger.warn("user dao warn log");
        logger.error("user dao error log");
    }
}

定义一个UserService:

import com.zhihao.miao.dao.UserDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserDao userDao;

    public void addUser(){
        logger.debug("user service debug log");
        logger.info("user service info log");
        logger.warn("user service warn log");
        logger.error("user service error log");
        userDao.addUser();
    }
}

启动类启动,调用UserService.addUser

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class,args);
        context.getBean(UserService.class).addUser();
    }
}

说明springboot默认的日志级别是info级别。

怎么去调整日志级别呢?

logging.level.root=debug

我们发现控制台上除了打印出了我们配置的dao和service的debug日志外,还打印出springboot的dubug的日志。

还可以定制某些包下的日志级别

logging.level.com.zhihao.miao.dao=debug

默认日志组件的源码spring-boot-1.5.4jar包下的org.springframework.boot.logging这个包下。

定义了7种日志级别

自己还可以指定日志的输出文件

logging.file=/Users/naeshihiroshi/work/workspace/selfStudy/springboot-demos/springboot.log

输出日志路径,此时的日志名字是spring.log

logging.path=/Users/naeshihiroshi/work/workspace/selfStudy/springboot-demos/

上面二种配置日志文件大小10M之后就会分割了。

还可以指定日志输出的格式,我们这边给控制台指定一种格式,给日志文件指定一种格式:

logging.level.com.zhihao.miao.dao=debug

logging.path=/Users/naeshihiroshi/work/workspace/selfStudy/springboot-demos/

logging.pattern.console=%-20(%d{yyyy-MM-dd} [%thread]) %-5level %logger{80} - %msg%n
logging.file.console=%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level %logger{80} - %msg%n
控制台输出 日志文件格式

结论
Springboot默认支持的是logback日志控件。可以在application.properties中配置,也可以在classptah下放一个logback.xml,logback-spring.xml(官方推荐使用logback-spring.xml)的文件,也可定制日志的输出。

源码分析

默认日志组件的源码spring-boot-1.5.4jar包下的org.springframework.boot.logging这个包下。

默认支持三种日志组件,java,log4j2,logback三种日志组件,默认的日志组件是logback。查看logback包下的base.xml文件,

默认的日志级别是INFO,包括控制台和文件。

再看defaults.xml,定义了控制台和文件的日志格式,

file-appender.xml文件定义了文件大小为10M的时候进行分割等等


定制成其他日志组件

springboot默认的是logback日志组件,如果我想定制成log4j2组件呢?

步骤大概如下:

具体步骤如下,修改依赖:

<dependencies>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter</artifactId>
          <exclusions>
              <exclusion>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-logging</artifactId>
              </exclusion>
          </exclusions>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-log4j2</artifactId>
      </dependency>
</dependencies>

将log4j2-spring.xml(推荐)或者log4j2.xml文件放在classpath路径下,
log4j2-spring.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>  
<configuration>  

    <appenders>
        <Console name="console" target="SYSTEM_OUT" follow="true">  
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{80} - %msg%n" />  
        </Console>
    </appenders>  
    
    <loggers>   
        <root level="DEBUG">
            <appender-ref ref="console" />
        </root>
    </loggers>
    
</configuration>

重启项目,验证是正确的。

上一篇 下一篇

猜你喜欢

热点阅读