使用logback记录日志

  1. Pom引用依赖

<dependency>

<groupId>ch.qos.logback</groupId>

<artifactId>++++logback++++ -classic</artifactId>

<version>1.2.11</version>

</dependency>

  1. logback.xml
XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>

<configuration scan="true" scanPeriod="30 seconds">

    <!-- stdout start 所有信息输出到控制台 -->
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} [line:%line] - %msg%n</pattern>
        </encoder>
    </appender>
    <!-- stdout end -->
    <appender name="debug-rolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Encoding>UTF-8</Encoding>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                <!-- 为方便排查问题暂时不做压缩,目前存在刷数据情况,故单个日志文件先调整为5G -->
                <fileNamePattern>logs/log-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
				<maxHistory>30</maxHistory>
                <totalSizeCap>1MB</totalSizeCap>
                <MaxFileSize>100KB</MaxFileSize>
            </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} [line:%line] - %msg%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印INFO日志 -->
            <level>DEBUG</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>

    </appender>

     <appender name="info-rolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Encoding>UTF-8</Encoding>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                <!-- 为方便排查问题暂时不做压缩,目前存在刷数据情况,故单个日志文件先调整为5G -->
                <fileNamePattern>logs/log-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
				<maxHistory>30</maxHistory>
                <totalSizeCap>1MB</totalSizeCap>
                <MaxFileSize>100KB</MaxFileSize>
            </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{50} [line:%line] - %msg%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印INFO日志 -->
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>

    </appender>	

    <appender name="error-rolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Encoding>UTF-8</Encoding>
         <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                <!-- 为方便排查问题暂时不做压缩,目前存在刷数据情况,故单个日志文件先调整为5G -->
                <fileNamePattern>logs/mylog-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
				<maxHistory>30</maxHistory>
                <totalSizeCap>1MB</totalSizeCap>
                <MaxFileSize>100KB</MaxFileSize>
            </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0} - %msg%n</pattern>
        </encoder>
        <filter class="ch.qos.logback.classic.filter.LevelFilter"><!-- 只打印错误日志 -->
            <level>ERROR</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>
    </appender>

    <!-- <appender name="log-trace-rolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <Encoding>UTF-8</Encoding>
        <File>${logPath}/log_trace.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${logPath}/log_trace.log.%d{yyyy-MM-dd-HH}</FileNamePattern>
            <maxHistory>24</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
        </encoder>
    </appender> -->


    <!-- 针对个别包日志可以进行单独的设置级别,用于过滤 -->
    <!-- <logger name="log-trace" additivity="false" level="INFO">
        <appender-ref ref="log-trace-rolling"/>
    </logger>
    <logger name="org.apache.zookeeper.ZooKeeper" additivity="false" level="ERROR">
        <appender-ref ref="time-rolling"/>
    </logger>
    <logger name="org.apache.zookeeper.ClientCnxn" additivity="false" level="ERROR">
        <appender-ref ref="time-rolling"/>
    </logger> -->

    <root level="DEBUG">
        <appender-ref ref="debug-rolling"/>
        <appender-ref ref="info-rolling"/>
	 <appender-ref ref="error-rolling"/>
        <appender-ref ref="stdout"/>
    </root>

</configuration>

详细可到以下链接下载

https://download.csdn.net/download/liuyuxuan2388/86543481

相关推荐
落榜程序员7 分钟前
Java 基础-32-枚举-枚举的应用场景
java·开发语言
晓131311 分钟前
第九章Python语言高阶加强-面向对象篇
java·开发语言
快来卷java1 小时前
JVM虚拟机篇(五):深入理解Java类加载器与类加载机制
java·jvm·mysql
禾小西3 小时前
Java 逐梦力扣之旅_[204. 计数质数]
java·算法·leetcode
ゞ 正在缓冲99%…3 小时前
leetcode295.数据流的中位数
java·数据结构·算法·leetcode·
有梦想的攻城狮5 小时前
spring-cloud-alibaba-nacos-config使用说明
java·spring·nacos·springcloud·配置中心
rufeike6 小时前
Rclone同步Linux数据到google云盘
linux·运维·服务器
Yan-英杰6 小时前
【百日精通JAVA | SQL篇 | 第三篇】 MYSQL增删改查
java·数据库·sql
矛取矛求7 小时前
C++ 标准库参考手册深度解析
java·开发语言·c++
cijiancao8 小时前
23 种设计模式中的解释器模式
java·设计模式·解释器模式