“Logback 的 FileAppender”的版本间的差异

来自姬鸿昌的知识库
跳到导航 跳到搜索
(建立内容为“https://www.bilibili.com/video/BV1iJ411H74S?p=28”的新页面)
 
第1行: 第1行:
 
https://www.bilibili.com/video/BV1iJ411H74S?p=28
 
https://www.bilibili.com/video/BV1iJ411H74S?p=28
 +
 +
=== 示例 ===
 +
 +
==== logback.xml ====
 +
<syntaxhighlight lang="xml">
 +
<?xml version="1.0" encoding="UTF-8" ?>
 +
<configuration>
 +
    <!--
 +
        配置集中管理属性
 +
        可以直接该属性的 value 值
 +
        格式:${name}
 +
    -->
 +
    <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m%n"></property>
 +
    <!--
 +
    日志输出格式:
 +
        %-5level 日志级别
 +
        %d{yyyy-MM-dd HH:mm:ss.SSS} 时间
 +
        %c 类的全限定名
 +
        %M 方法名
 +
        %L 行号
 +
        %thread 线程名称
 +
        %m 或者 %msg 为信息
 +
        %n 换行
 +
    -->
 +
    <!-- 定义日志文件保存路径属性 -->
 +
    <property name="log_dir" value="/logs"></property>
 +
 +
    <!-- 控制台日志输出的 appender -->
 +
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
 +
        <!-- 控制输出流对象 默认 System.out 改为 System.err -->
 +
        <target>System.err</target>
 +
        <!-- 日志消息格式配置 -->
 +
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
 +
            <pattern>${pattern}</pattern>
 +
        </encoder>
 +
    </appender>
 +
 +
    <!-- 日志文件输出的 appender -->
 +
    <appender name="file" class="ch.qos.logback.core.FileAppender">
 +
        <!-- 日志文件的保存路径 -->
 +
        <file>${log_dir}/logback.log</file>
 +
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
 +
            <pattern>${pattern}</pattern>
 +
        </encoder>
 +
    </appender>
 +
 +
    <!-- root logger 配置 -->
 +
    <root level="ALL">
 +
        <appender-ref ref="console"/>
 +
        <appender-ref ref="file"/>
 +
    </root>
 +
 +
</configuration>
 +
</syntaxhighlight>
 +
 +
==== LogbackTest.java ====
 +
<syntaxhighlight lang="console">
 +
package io.github.jihch;
 +
 +
import org.junit.Test;
 +
import org.slf4j.Logger;
 +
import org.slf4j.LoggerFactory;
 +
 +
public class LogbackTest {
 +
 +
    public static final Logger LOGGER = LoggerFactory.getLogger(LogbackTest.class);
 +
 +
    // 快速入门
 +
    @Test
 +
    public void test() {
 +
        // 日志输出
 +
        LOGGER.error("error");
 +
        LOGGER.warn("warn");
 +
        LOGGER.info("info");
 +
        LOGGER.debug("debug");
 +
        LOGGER.trace("trace");
 +
    }
 +
 +
}
 +
 +
</syntaxhighlight>
 +
 +
==== 控制台输出 ====
 +
<syntaxhighlight lang="console">
 +
[ERROR] 2023-02-27 11:43:16.758 io.github.jihch.LogbackTest test 15 [main] error
 +
[WARN ] 2023-02-27 11:43:16.760 io.github.jihch.LogbackTest test 16 [main] warn
 +
[INFO ] 2023-02-27 11:43:16.760 io.github.jihch.LogbackTest test 17 [main] info
 +
[DEBUG] 2023-02-27 11:43:16.760 io.github.jihch.LogbackTest test 18 [main] debug
 +
[TRACE] 2023-02-27 11:43:16.761 io.github.jihch.LogbackTest test 19 [main] trace
 +
</syntaxhighlight>
 +
 +
==== /logs/logback.log ====
 +
<syntaxhighlight lang="console">
 +
[ERROR] 2023-02-27 11:43:16.758 io.github.jihch.LogbackTest test 15 [main] error
 +
[WARN ] 2023-02-27 11:43:16.760 io.github.jihch.LogbackTest test 16 [main] warn
 +
[INFO ] 2023-02-27 11:43:16.760 io.github.jihch.LogbackTest test 17 [main] info
 +
[DEBUG] 2023-02-27 11:43:16.760 io.github.jihch.LogbackTest test 18 [main] debug
 +
[TRACE] 2023-02-27 11:43:16.761 io.github.jihch.LogbackTest test 19 [main] trace
 +
 +
</syntaxhighlight>

2023年2月27日 (一) 03:46的版本

https://www.bilibili.com/video/BV1iJ411H74S?p=28

示例

logback.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <!--
        配置集中管理属性
        可以直接该属性的 value 值
        格式:${name}
    -->
    <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c %M %L [%thread] %m%n"></property>
    <!--
    日志输出格式:
        %-5level 日志级别
        %d{yyyy-MM-dd HH:mm:ss.SSS} 时间
        %c 类的全限定名
        %M 方法名
        %L 行号
        %thread 线程名称
        %m 或者 %msg 为信息
        %n 换行
    -->
    <!-- 定义日志文件保存路径属性 -->
    <property name="log_dir" value="/logs"></property>

    <!-- 控制台日志输出的 appender -->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <!-- 控制输出流对象 默认 System.out 改为 System.err -->
        <target>System.err</target>
        <!-- 日志消息格式配置 -->
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
    </appender>

    <!-- 日志文件输出的 appender -->
    <appender name="file" class="ch.qos.logback.core.FileAppender">
        <!-- 日志文件的保存路径 -->
        <file>${log_dir}/logback.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
    </appender>

    <!-- root logger 配置 -->
    <root level="ALL">
        <appender-ref ref="console"/>
        <appender-ref ref="file"/>
    </root>

</configuration>

LogbackTest.java

package io.github.jihch;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogbackTest {

    public static final Logger LOGGER = LoggerFactory.getLogger(LogbackTest.class);

    // 快速入门
    @Test
    public void test() {
        // 日志输出
        LOGGER.error("error");
        LOGGER.warn("warn");
        LOGGER.info("info");
        LOGGER.debug("debug");
        LOGGER.trace("trace");
    }

}

控制台输出

[ERROR] 2023-02-27 11:43:16.758 io.github.jihch.LogbackTest test 15 [main] error
[WARN ] 2023-02-27 11:43:16.760 io.github.jihch.LogbackTest test 16 [main] warn
[INFO ] 2023-02-27 11:43:16.760 io.github.jihch.LogbackTest test 17 [main] info
[DEBUG] 2023-02-27 11:43:16.760 io.github.jihch.LogbackTest test 18 [main] debug
[TRACE] 2023-02-27 11:43:16.761 io.github.jihch.LogbackTest test 19 [main] trace

/logs/logback.log

[ERROR] 2023-02-27 11:43:16.758 io.github.jihch.LogbackTest test 15 [main] error
[WARN ] 2023-02-27 11:43:16.760 io.github.jihch.LogbackTest test 16 [main] warn
[INFO ] 2023-02-27 11:43:16.760 io.github.jihch.LogbackTest test 17 [main] info
[DEBUG] 2023-02-27 11:43:16.760 io.github.jihch.LogbackTest test 18 [main] debug
[TRACE] 2023-02-27 11:43:16.761 io.github.jihch.LogbackTest test 19 [main] trace