spring boot快速入门 10: 日志使用

Posted 刘晓明

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot快速入门 10: 日志使用相关的知识,希望对你有一定的参考价值。

第一步:pom 文件

<?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.payease</groupId>
    <artifactId>sell</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>sell</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

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


        <!-- ===================== mysql驱动 ======================== -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- ===================== jpa ======================== -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- ===================== @Getter @Setter @Slf4j @Data ======================== -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

第二步:你的测试类

package com.payease;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * Created by liuxiaoming on 2017/11/8.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
//@Data
public class LoggerTest {

    //private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);

    @Test
    public void test1(){
       // logger.debug("debug ...");
       // logger.info("info ...");
       // logger.error("error ...");
        //日志中打印变量的使用样例

        String name = "liuxiaoming";
        String password = "123456";
        log.debug("debug ..."); //@Slf4j
        log.info("info ..."+"name:" + name + " ,password:" + password);   //@Slf4j
        log.error("error ..."+"name:{} ,password:{}",name,password); //@Slf4j
    }
}

第三步:配置文件application.yml文件中的配置

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 1234
    url: jdbc:mysql://127.0.0.1/sell?characterEncoding=utf-8&useSSL=false
  jpa:
    show-sql: true
server:
  context-path: /sell
logging:
  pattern:
    console: "%d - %msg%n"  #日志格式 日期 - 信息 空格
#  path: /Users/liuxiaoming/Documents/ideawork/sell_log #日志路径 默认名字spring.log
  file: /Users/liuxiaoming/Documents/ideawork/sell_log/sell.log #日志文件+路径
  level:  #日志级别
    com.payease.LoggerTest: debug #日志级别指定某个类 也可以步制定类 直接在level: 后面配置

第二种日志的配置方式:

第一步:注释道application.yml中关于日志的配置

第二步:新建logback-spring.xml 日志配置文件

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

<configuration>
    <!-- 控制台的显示 -->
    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                %d - %msg%n
            </pattern>
        </layout>
    </appender>
    <!-- 日志文件:fileInfoLog -->
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 根据范围来过滤 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>DENY</onMatch>  <!-- 禁止 -->
            <onMismatch>ACCEPT</onMismatch>  <!-- 接受 -->
        </filter>
        <!--滚动-->
        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路径-->
            <fileNamePattern>/Users/liuxiaoming/Documents/ideawork/sell_log/info.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>

    <!-- 日志文件:fileErrorLog -->
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 根据范围来过滤 -->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <level>ERROR</level>
        </filter>
        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路径-->
            <fileNamePattern>/Users/liuxiaoming/Documents/ideawork/sell_log/error.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>

    <!--日志级别-->
    <root level="info">
        <appender-ref ref="consoleLog" />
        <appender-ref ref="fileInfoLog" />
        <appender-ref ref="fileErrorLog" />
    </root>

</configuration>

第三步:启动项目 查看生成了两个配置文件

 

以上是关于spring boot快速入门 10: 日志使用的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 入门 - 日志管理

Spring Boot 日志管理

Spring Boot 永远滴神!10分钟快速入门

[公益课程]Spring Boot 2.x 实战入门

spring boot2.0快速入门

Spring Boot快速入门