Logback初探

2020-05-30 16:02:34来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Logback初探

Logback官网

简介

Logback旨在作为流行的log4j项目的后继者,从log4j离开的地方接手。

Logback的体系结构足够通用,可以应用在不同的情况下。目前,logback分为三个模块:logback-core,logback-classic和logback-access。

logback-core模块为其他两个模块奠定了基础。logback-classic模块可以与log4j的显着改进版本同化。此外,经典的logback原生实现了SLF4J API,因此您可以轻松地在logback和其他日志框架(例如log4j或java.util.logging(JUL))之间来回切换。

logback-access模块与Servlet容器(例如Tomcat和Jetty)集成,以提供HTTP访问日志功能。请注意,您可以轻松地在logback-core之上构建自己的模块。

1.新建module,logback-demo

2.pom.xml添加依赖

<?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">
    <parent>
        <artifactId>log-study</artifactId>
        <groupId>com.lxcourse</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>logback-demo</artifactId>

    <dependencies>
        <!--  slf4j日志门面       -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.28</version>
        </dependency>

        <!-- logback-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

        <!--   Junit     -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3.快速体验类

package com.lxcourse.logback;

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

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

    @Test
    public void testQuick(){
        LOGGER.error("error");
        LOGGER.warn("warn");
        LOGGER.info("info");
        LOGGER.debug("debug");
        LOGGER.trace("trace");
    }
}

输出:

14:16:51.106 [main] ERROR com.lxcourse.logback.LogbackDemo - error
14:16:51.109 [main] WARN com.lxcourse.logback.LogbackDemo - warn
14:16:51.109 [main] INFO com.lxcourse.logback.LogbackDemo - info
14:16:51.109 [main] DEBUG com.lxcourse.logback.LogbackDemo - debug

4.使用配置文件logback.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

        <target>System.err</target>
        <encoder>
            <pattern>${pattern}</pattern>
        </encoder>
    </appender>

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

具体配置参考官网教程

configuration

layouts

执行LogbackDemo.testQuick(),输出如下:

[ERROR] 2020-05-29 14:59:38.233 com.lxcourse.logback.LogbackDemo testQuick [main] error
[WARN ] 2020-05-29 14:59:38.236 com.lxcourse.logback.LogbackDemo testQuick [main] warn
[INFO ] 2020-05-29 14:59:38.237 com.lxcourse.logback.LogbackDemo testQuick [main] info
[DEBUG] 2020-05-29 14:59:38.238 com.lxcourse.logback.LogbackDemo testQuick [main] debug
[TRACE] 2020-05-29 14:59:38.238 com.lxcourse.logback.LogbackDemo testQuick [main] trace

5.将日志输出为文件

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>

    <property name="log_dir" value="/Users/doubledumbao/logs"></property>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

        <target>System.err</target>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
    </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 level="ALL">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="file" />
    </root>
</configuration>

6.将日志输出为html格式

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>

    <property name="log_dir" value="/Users/doubledumbao/logs"></property>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

        <target>System.err</target>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
    </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>
    <!-- 将日志输出为html格式-->
    <appender name="htmlFile" class="ch.qos.logback.core.FileAppender">
        <file>${log_dir}/logback.html</file>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="ch.qos.logback.classic.html.HTMLLayout">
                <pattern>%-5level%d{yyyy-MM-dd HH:mm:ss.SSS}%C%M%thread%msg</pattern>
            </layout>
        </encoder>
    </appender>

    <root level="ALL">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="file" />
        <appender-ref ref="htmlFile" />
    </root>
</configuration>

7.日志拆分和归档压缩的appender对象

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>

    <property name="log_dir" value="/Users/doubledumbao/logs"></property>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

        <target>System.err</target>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
    </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>
    <!-- 将日志输出为html格式-->
    <appender name="htmlFile" class="ch.qos.logback.core.FileAppender">
        <file>${log_dir}/logback.html</file>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="ch.qos.logback.classic.html.HTMLLayout">
                <pattern>%-5level%d{yyyy-MM-dd HH:mm:ss.SSS}%C%M%thread%msg</pattern>
            </layout>
        </encoder>
    </appender>
    <!--  日志拆分和归档压缩的appender对象  -->
    <appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log_dir}/roll_logback.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log_dir}/rolling.%d{yyyy-MM-dd}.log%i.gz</fileNamePattern>
            <maxFileSize>1MB</maxFileSize>
        </rollingPolicy>
    </appender>

    <root level="ALL">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="rollFile" />
    </root>
</configuration>
@Test
    public void testRollingFile(){
        for (int i = 0; i < 10000; i++) {
            LOGGER.error("error");
            LOGGER.warn("warn");
            LOGGER.info("info");
            LOGGER.debug("debug");
            LOGGER.trace("trace");
        }
    }

8.设置日志过滤级别

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>

    <property name="log_dir" value="/Users/doubledumbao/logs/logger"></property>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

        <target>System.err</target>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
    </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>
    <!-- 将日志输出为html格式-->
    <appender name="htmlFile" class="ch.qos.logback.core.FileAppender">
        <file>${log_dir}/logback.html</file>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="ch.qos.logback.classic.html.HTMLLayout">
                <pattern>%-5level%d{yyyy-MM-dd HH:mm:ss.SSS}%C%M%thread%msg</pattern>
            </layout>
        </encoder>
    </appender>
    <!--  日志拆分和归档压缩的appender对象  -->
    <appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log_dir}/roll_logback.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log_dir}/rolling.%d{yyyy-MM-dd}.log%i.gz</fileNamePattern>
            <maxFileSize>1MB</maxFileSize>
        </rollingPolicy>
        <!-- 日志级别过滤器   -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <!--   日志过滤规则     -->
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <root level="ALL">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="rollFile" />
    </root>
</configuration>

9.异步日志

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>

    <property name="log_dir" value="/Users/doubledumbao/logs/logger"></property>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

        <target>System.err</target>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
    </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>
    <!-- 将日志输出为html格式-->
    <appender name="htmlFile" class="ch.qos.logback.core.FileAppender">
        <file>${log_dir}/logback.html</file>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="ch.qos.logback.classic.html.HTMLLayout">
                <pattern>%-5level%d{yyyy-MM-dd HH:mm:ss.SSS}%C%M%thread%msg</pattern>
            </layout>
        </encoder>
    </appender>
    <!--  日志拆分和归档压缩的appender对象  -->
    <appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log_dir}/roll_logback.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log_dir}/rolling.%d{yyyy-MM-dd}.log%i.gz</fileNamePattern>
            <maxFileSize>1MB</maxFileSize>
        </rollingPolicy>
        <!-- 日志级别过滤器   -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <!--   日志过滤规则     -->
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- 异步日志   -->
    <appender name="async" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="rollFile"/>
    </appender>

    <root level="ALL">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="async" />
    </root>
</configuration>

10.自定义logger对象

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <property name="pattern" value="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %C %M [%thread] %msg%n"></property>

    <property name="log_dir" value="/Users/doubledumbao/logs/logger"></property>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">

        <target>System.err</target>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
    </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>
    <!-- 将日志输出为html格式-->
    <appender name="htmlFile" class="ch.qos.logback.core.FileAppender">
        <file>${log_dir}/logback.html</file>
        <encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="ch.qos.logback.classic.html.HTMLLayout">
                <pattern>%-5level%d{yyyy-MM-dd HH:mm:ss.SSS}%C%M%thread%msg</pattern>
            </layout>
        </encoder>
    </appender>
    <!--  日志拆分和归档压缩的appender对象  -->
    <appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${log_dir}/roll_logback.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${pattern}</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${log_dir}/rolling.%d{yyyy-MM-dd}.log%i.gz</fileNamePattern>
            <maxFileSize>1MB</maxFileSize>
        </rollingPolicy>
        <!-- 日志级别过滤器   -->
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <!--   日志过滤规则     -->
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>
    </appender>

    <!-- 异步日志   -->
    <appender name="async" class="ch.qos.logback.classic.AsyncAppender">
        <appender-ref ref="rollFile"/>
    </appender>


    <!--root Logger-->
    <root level="ALL">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="async"/>
    </root>

    <!-- 自定义logger对象  additivity="false" 自定义logger对象是否继承rootLogger -->
    <logger name="com.lxcourse" level="info" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>
</configuration>


原文链接:https://www.cnblogs.com/zhaoran8775/p/12990760.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Springboot日志初探

下一篇:高吞吐量的分布式发布订阅消息系统Kafka之Producer源码分析