Logback的使用与配置

Java基础

浏览数:115

2019-8-21

AD:资源代下载服务

Logback的使用与配置

1.Logback的基本使用

package com.hcx;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * Created by HCX on 2019/3/5.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class LoggerTest {

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

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

使用@Slf4j注解简化书写:

依赖:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

代码:

package com.hcx;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * Created by HCX on 2019/3/5.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class LoggerTest {

    @Test
    public void test1(){
        String name="hcx";
        String password = "123456";
        log.debug("debug.");
        log.info("name:{},password:{}",name,password);
        log.error("error");
    }
}

注意:如果注解@Slf4j注入后找不到变量log,需要给idea安装lombok插件:
File → setting → Plugins ,点击Browse repositories:

安装插件1.png

输入 lombok 搜索插件, 点install安装,安装完重启idea:

安装插件2.png

2.Logback的配置

方式一:application.yml
较简单的配置

application.yml:

logging:
  pattern:
    console: "%d -%msg%n" #只显示日期和内容

  file: /var/log/tomcat/myproject.log
  level: debug #指定日志级别
  level:
    com.hcx.LoggerTest:debug #指定具体类的日志级别

方式二:logback-spring.xml
较复杂的配置

需求:区分info和error日志,每天产生一个日志文件

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和log 输出到不同的文件-->
    <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--根据范围过滤-->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>DENY</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>

        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路径-->
            <fileNamePattern>/var/log/tomcat/myproject/info.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>

    <!--配置项:配置文件的输出 针对不同级别 输出到不同的文件-->
    <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <!--根据范围过滤-->
        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
            <!--设置error配置项只输出error级别的日志-->
            <level>ERROR</level>
        </filter>

        <encoder>
            <pattern>
                %msg%n
            </pattern>
        </encoder>
        <!--滚动策略-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--路径-->
            <fileNamePattern>/var/log/tomcat/myproject/error.%d.log</fileNamePattern>
        </rollingPolicy>
    </appender>

    <!--应用到那里 root:这个项目都适用-->
    <root level="info">
        <appender-ref ref="consoleLog"/>
        <appender-ref ref="fileInfoLog"/>
        <appender-ref ref="fileErrorLog"/>
    </root>
</configuration>

作者:JS_HCX