码迷,mamicode.com
首页 > 编程语言 > 详细

(028)Spring Boot之日志处理

时间:2019-12-09 01:13:50      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:none   时间   etl   import   ott   pattern   cto   通过   文件名   

  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

  运行结果如下:

技术图片

 

 

 

 

 

(028)Spring Boot之日志处理

标签:none   时间   etl   import   ott   pattern   cto   通过   文件名   

原文地址:https://www.cnblogs.com/javasl/p/11966674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!