Java中日志的使用


一、Slf4j日志使用

1、使用slf4j常规打印日志:

package com.edu.cn.dishsell.logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class LoggerTest {
    private final Logger logger = LoggerFactory.getLogger(LoggerTest.class);

    @Test
    public void test() {
        logger.debug("debug...");
        logger.info("info...");
        logger.error("error...");
    }
}

输出:

之所以没有打印出debug,是因为日志的等级低,查看如下源码(mac可通过command+O打开查询类名):

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.slf4j.event;

public enum Level {
    ERROR(40, "ERROR"),
    WARN(30, "WARN"),
    INFO(20, "INFO"),
    DEBUG(10, "DEBUG"),
    TRACE(0, "TRACE");

    private int levelInt;
    private String levelStr;

    private Level(int i, String s) {
        this.levelInt = i;
        this.levelStr = s;
    }

    public int toInt() {
        return this.levelInt;
    }

    public String toString() {
        return this.levelStr;
    }
}

2、使用注解@Slf4j打印日志:

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LoggerTest {

    @Test
    public void test() {
        log.debug("debug...");
        log.info("info...");
        log.error("error...");
    }
}

输出日志结果同上。

3、日志输出变量:

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LoggerTest {

    @Test
    public void test() {
        String name = "java";
        String password = "111111";
        log.debug("debug...");
        // 推荐写法
        log.info("name: {}, password: {}", name, password);
        log.error("error...");
    }
}

输出:

二、Logback的配置

1、application.yml配置 (适用于小型项目配置)

(1)在application.yml中配置日志:

logging:
  pattern:
    console: "%d - %msg%n" #表示输出的是对应的 时间-信息

输出:


(2)设置日志的路径:

logging:
  path: /Users/cyh/Documents/code/java/java项目/dishsell/src/main/java/com/edu/cn/dishsell/log

输出:


(3)为日志设置路径以及日志名称:

logging:
  file: /Users/cyh/Documents/code/java/java项目/dishsell/src/main/java/com/edu/cn/dishsell/log/sell.log

输出:


(4)配置debug:

logging:
  pattern:
    console: "%d - %msg%n" #表示输出的是对应的 时间-信息
  level:
    com.edu.cn.dishsell.logger.LoggerTest: debug

输出:

2、logback-spring.xml配置(适用于大型项目)

(1)先在项目的resources文件下创建logback-spring.xml配置文件

(2)对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>
    <!-- 输出日志文件信息info,按时间滚动,每天产生一个日志文件 -->
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!-- 过滤日志文件中级别高于info的日志信息 -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>DENY</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>
        <encoder>
            <pattern>
               %d - %msg%n
            </pattern>
        </encoder>
        <!-- 滚动策略(每天一个日志文件) -->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 日志输出路径 -->
            <fileNamePattern>/Users/cyh/Documents/code/java/java项目/dishsell/src/main/java/com/edu/cn/dishsell/log/sell/info.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>
    <!-- 错误日志文件信息 -->
    <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/cyh/Documents/code/java/java项目/dishsell/src/main/java/com/edu/cn/dishsell/log/sell/error.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>
    <root level="info">
        <appender-ref ref="consoleLog"></appender-ref>
        <appender-ref ref="fileInfoLog"></appender-ref>
        <appender-ref ref="fileErrorLog"></appender-ref>
    </root>
</configuration>

输出:

  • info日志

info日志

  • error日志

error日志


评论
评论
  目录