SpringBoot logging system

Logging framework

The development of a large system of easy steps:

  1.  system.out.println ( "..") to output data key in the console
  2. Some framework runtime information recording system, --- logging framework.
  3. Fast hardware functions --- asynchronous mode, automatic archiving and so on. . .
  4. Further development of the framework have to remove the old, and before modification API,
  5. JDBC database driver, JDC database-driven:

       Write a unified interface layer: Log facade, (an abstraction layer log)

       To project into concrete realization of the log, the frame is achieved before abstraction layer

Mainstream logging framework

JUL、JCL、Jboss-logging、logback、log4j、log4j2、slf4j....

Facade log (log abstraction layer):

JCL (Jakarta Commons Logging) SLF4j(Simple Logging Facade for Java) jboss-logging,

Log achieve:

Log4j JUL(java.util.logging) Log4j2  Logback

 

Springboot: bottom layer is the Spring framework, Spring Framework is the default JCL,

 

SLF4J use

1, how to use the system SLF4J   https://www.slf4j.org 

During development, call log recording method, should not be called directly implement the class log, but the log method calls the abstract into the inside.

Slf4j introduced to the system jar, and the jar logback 

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
  public static void main(String[] args) {
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    logger.info("Hello World");
  }
}

Implementation framework Each log has its own configuration file, then use slf4j, made of logs or configuration file framework to achieve their own profile.

 Remaining problem: 

Development time,

A system: might use (slf4j + logback): Spring (commons-logging default), Hibernate (default: jboss-logging), MyBatis, xxxx

 How unified logging? That the use of other frameworks, but also unified the use slf4j output

 

How to achieve all unified log system to slf4j:

  1. The system logs all the other frameworks to exclude
  2. A tundish to replace the original logging framework
  3. Other implementations jar of imported slf4j 

 

springboot log relationship

If you use the IDEA, then the idea has a great tool that allows us to visualize the view dependencies right choice diagrams --- show dependencies in pom.xml can view

Launcher:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>

 springboot 使用的日志功能组件:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
</dependency>

 底层依赖关系:

归纳:

  1. SpringBoot底层也是使用slf4j+logback的方式进行日志记录
  2. SpringBoot也把其他的日志都替换成了slf4j;
  3. 中间替换包?
@SuppressWarnings("rawtypes")
public abstract class LogFactory {

    static String UNSUPPORTED_OPERATION_IN_JCL_OVER_SLF4J = "http://www.slf4j.org/codes.html#unsupported_operation_in_jcl_over_slf4j";

    static LogFactory logFactory = new SLF4JLogFactory();

 

 

  如果我们要引入其他框架,一定要把这个框架的默认日志依赖移除掉。

  Spring框架用的是commons-logging;

    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

 

 SpringBoot能自动适配所有的日志,而且底层使用slf4j+logback的方式记录日志,引入其他框架的时候,只需要把这个框架依赖的日志框架排除掉即可

 SringBoot默认配置

SpringBoot默认帮我们配置好了日志;

    //记录器
    Logger logger = LoggerFactory.getLogger(getClass());
    @Test
    public void contextLoads() {
        //System.out.println();

        //日志的级别;
        //由低到高   trace<debug<info<warn<error
        //可以调整输出的日志级别;日志就只会在这个级别以以后的高级别生效
        logger.trace("这是trace日志...");
        logger.debug("这是debug日志...");
        //SpringBoot默认给我们使用的是info级别的,没有指定级别的就用SpringBoot默认规定的级别;root级别
        logger.info("这是info日志...");
        logger.warn("这是warn日志...");
        logger.error("这是error日志...");
    }
  日志输出格式:
        %d表示日期时间,
        %thread表示线程名,
        %-5level:级别从左显示5个字符宽度
        %logger{50} 表示logger名字最长50个字符,否则按照句点分割。 
        %msg:日志消息,
        %n是换行符
    -->
    %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n

 

 SpringBoot修改日志的默认配置

logging.level.com.atguigu=trace

#logging.path=
# 不指定路径在当前项目下生成springboot.log日志
# 可以指定完整的路径;
#logging.file=G:/springboot.log

# 在当前磁盘的根路径下创建spring文件夹和里面的log文件夹;使用 spring.log 作为默认文件
logging.path=/spring/log

#  在控制台输出的日志的格式
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
# 指定文件中日志输出的格式
logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} ==== %msg%n

 

logging.file logging.path Example Description
(none) (none)   只在控制台输出
指定文件名 (none) my.log 输出日志到my.log文件
(none) 指定目录 /var/log 输出到指定目录的 spring.log 文件中

 指定配置:

 

 

  

 

Guess you like

Origin www.cnblogs.com/thelovelybugfly/p/11117648.html