zoukankan      html  css  js  c++  java
  • (028)Spring Boot之日志处理

      springboot默认使用的日志是logback,本篇记录如何使用默认日志,如何使用其他日志 

    (一)通过application.properties配置默认日志

      (1)默认的日志级别是info,默认日志只打印在控制台

      贴出pom.xml,不用添加额外依赖

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.edu.spring</groupId>
        <artifactId>springboot_web</artifactId>
        <version>1.0.0</version>
    
        <name>springboot_web</name>
        <!-- FIXME change it to the project's website -->
        <url>http://www.example.com</url>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.4.RELEASE</version>
        </parent>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
        </dependencies>
    
    </project>
    View Code

      UserDao.java,测试类

    package com.edu.spring.springboot.dao;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    
    @Component
    public class UserDao {
    
        private Logger logger=LoggerFactory.getLogger(UserDao.class);
        
        public void log(){
            logger.debug("UserDao log debug");
            logger.info("UserDao log info");
            logger.warn("UserDao log warn");
            logger.error("UserDao log error");
        }
    }
    View Code

      App.java

    package com.edu.spring.springboot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    @SpringBootApplication
    public class App    
    { 
        public static void main(String[] args) throws Exception{
            ConfigurableApplicationContext context=SpringApplication.run(App.class, args); 
            System.out.println("-----------UserDao.class------------");
            context.getBean(UserDao.class).log();
            context.close();
        }
    } 
    View Code

      运行结果可以看出:默认的日志级别是info,默认日志只打印在控制台

      (2)修改日志级别,在application.properties添加如下属性,将包com.edu.spring.springboot.dao下的日志级别修改为debug

    logging.level.com.edu.spring.springboot.dao=debug  

    运行结果如下:

      (3)修改日志格式:logging.pattern.console,将日志的输出格式修改为输出时间和内容

    logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %m%n

      运行结果如下:

      注意:logging.level.root=info 表示所有包下的日志的级别是info

        logging.level.root=off 表示关闭所有包下的日志 

        每一个包下都可以单独定义级别,不单独定义的按照默认级别

      (3)配置日志文件的路径,logging.file或者logging.path;配置日志格式,logging.pattern.file

    logging.file=d:/springboottemp/logs/mylog.log
    logging.pattern.file=%d{yyyy-MM-dd} %t %-4r %p %F[%L]: %m%n

      运行结果如下:

       注意:控制台仍然会输出日志

        如果配置 logging.path=d:/springboottemp/logs 默认生成的文件名是 spring.log

     (二)通过logback.xml或者logback-spring.xml配置默认日志

      在resources中新建logback.xml 或者 logback-spring.xml,去掉application.properties中所有配置

    <?xml version="1.0" encoding="UTF-8" ?>
    <configuration>
        <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
            <layout>
                <pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </layout>
        </appender>
        <root level="debug">
            <appender-ref ref="consoleLog"/>
        </root>
    </configuration>
    View Code

      运行结果如下:

      注意:大小以10M分割日志

    (三)如何使用其他日志,log4j2为例

      首先排除掉默认的logback,然后引入log4j2的依赖

    <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>

      其次在resources中新建log4j2.xml 去掉application.properties中所有配置

    <?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} |-%-5level[%thread] %c [%L] -| %msg%n" />
            </Console>
        </appenders>
        <loggers>
            <root level="debug">
                <appenderref ref="CONSOLE" />
            </root>
        </loggers>
    </configuration>
    View Code

      运行结果如下:

  • 相关阅读:
    国内最火的3款前端开发框架
    Cordova是做什么的
    老师你好。使用cordova生成的hellowold 的安卓5.0版本太高。怎么才可以生成4.4的呢?
    一个类似bootstrap的foundation
    role在标签中的作用是什么?
    如何做到根据不同的进度用不同的颜色显示整个进度条
    wall 和panel有啥区别
    git ignore
    eclipse js 引用跳转
    计划
  • 原文地址:https://www.cnblogs.com/javasl/p/11966674.html
Copyright © 2011-2022 走看看