本站消息

站长简介/公众号


站长简介:高级软件工程师,曾在阿里云,每日优鲜从事全栈开发工作,利用周末时间开发出本站,欢迎关注我的微信公众号:程序员总部,程序员的家,探索程序员的人生之路!分享IT最新技术,关注行业最新动向,让你永不落伍。了解同行们的工资,生活工作中的酸甜苦辣,谋求程序员的最终出路!

  价值13000svip视频教程,java大神匠心打造,零基础java开发工程师视频教程全套,基础+进阶+项目实战,包含课件和源码

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2021-02(247)

2021-03(318)

2021-04(122)

2021-05(71)

2021-06(55)

Logback初探

发布于2020-11-20 09:42     阅读(436)     评论(0)     点赞(6)     收藏(0)


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



所属网站分类: 技术文章 > 博客

作者:java小王子

链接:http://www.javaheidong.com/blog/article/4994/ea17d1ada380c0679a19/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

6 0
收藏该文
已收藏

评论内容:(最多支持255个字符)