pom.xml

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

 

logback.xml(直接放在src/main/resources下)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!--======================================= 本地变量 ======================================== -->
    <!--在没有定义${LOG_HOME}系统变量的时候,可以设置此本地变量。提交测试、上线时,要将其注释掉,使用系统变量。 -->
    <!-- <property name="LOG_HOME" value="D:/data/logs" /> -->

    <!-- 应用名称:和统一配置中的项目代码保持一致(小写) -->
    <property name="APP_NAME" value="log" />
    <!--日志文件保留天数 -->
    <property name="LOG_MAX_HISTORY" value="180" />
    <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径 -->
    <!--应用日志文件保存路径 -->
    <property name="LOG_APP_HOME" value="${APP_NAME}" />

    <!--=========================== 按照每天生成日志文件:默认配置=================================== -->
    <!-- 控制台输出 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>
    <!-- 按照每天生成日志文件:主项目日志 -->
    <appender name="APP"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名 -->
            <FileNamePattern>${LOG_APP_HOME}/base.%d{yyyy-MM-dd}.log
            </FileNamePattern>
            <!--日志文件保留天数 -->
            <MaxHistory>${LOG_MAX_HISTORY}</MaxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{500} - %msg%n</pattern>
        </encoder>
    </appender>
    <!--=============================== 日志输出: 默认主业务日志 ====================================== -->
    <logger name="org.springframework">
        <level value="WARN" />
    </logger>
    <logger name="org.apache.shiro">
        <level value="WARN" />
    </logger>
    <logger name="freemarker">
        <level value="WARN" />
    </logger>
    <logger name="org.hibernate">
        <level value="WARN" />
    </logger>
    <logger name="org.hibernate.SQL">
        <level value="DEBUG" />
    </logger>

    <root level="DEBUG">
        <appender-ref ref="APP" />
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

 

测试

package com.vast.rabbitmqprovider;

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

@SpringBootTest
@RunWith(SpringRunner.class)
@Slf4j
class RabbitmqProviderApplicationTests {

    @Test
    void contextLoads() {
    }

    @Test
    public void testLogs() {
        log.trace("trace-------------------");
        log.debug("debug---------------{}", "debug");
        log.info("info");
        log.warn("warn------------");
        log.error("error-------------");
    }

}

 

控制台输出

2019-12-18 09:08:19.176 [main] INFO  c.v.r.RabbitmqProviderApplicationTests - Starting RabbitmqProviderApplicationTests on 67FZC3DX6H7XS4R with PID 10924 (started by Administrator in F:\baway_workspace\2019_11\rabbitmq-provider)
2019-12-18 09:08:19.186 [main] DEBUG c.v.r.RabbitmqProviderApplicationTests - Running with Spring Boot v2.2.2.RELEASE, Spring v5.2.2.RELEASE
2019-12-18 09:08:19.187 [main] INFO  c.v.r.RabbitmqProviderApplicationTests - No active profile set, falling back to default profiles: default
2019-12-18 09:08:28.290 [main] INFO  c.v.r.RabbitmqProviderApplicationTests - Started RabbitmqProviderApplicationTests in 10.857 seconds (JVM running for 17.303)

2019-12-18 09:08:29.086 [main] DEBUG c.v.r.RabbitmqProviderApplicationTests - debug---------------debug
2019-12-18 09:08:29.086 [main] INFO  c.v.r.RabbitmqProviderApplicationTests - info
2019-12-18 09:08:29.087 [main] WARN  c.v.r.RabbitmqProviderApplicationTests - warn------------
2019-12-18 09:08:29.087 [main] ERROR c.v.r.RabbitmqProviderApplicationTests - error-------------

 

日志文件

 

posted on 2019-12-18 09:15  风又奈何  阅读(785)  评论(0编辑  收藏  举报