使用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

相关推荐
群联云防护小杜5 分钟前
如何给负载均衡平台做好安全防御
运维·服务器·网络·网络协议·安全·负载均衡
w_312345413 分钟前
自定义一个maven骨架 | 最佳实践
java·maven·intellij-idea
岁岁岁平安15 分钟前
spring学习(spring-DI(字符串或对象引用注入、集合注入)(XML配置))
java·学习·spring·依赖注入·集合注入·基本数据类型注入·引用数据类型注入
武昌库里写JAVA19 分钟前
Java成长之路(一)--SpringBoot基础学习--SpringBoot代码测试
java·开发语言·spring boot·学习·课程设计
Q_192849990626 分钟前
基于Spring Boot的九州美食城商户一体化系统
java·spring boot·后端
奈何不吃鱼28 分钟前
【Linux】ubuntu依赖安装的各种问题汇总
linux·运维·服务器
爱码小白33 分钟前
网络编程(王铭东老师)笔记
服务器·网络·笔记
张国荣家的弟弟43 分钟前
【Yonghong 企业日常问题 06】上传的文件不在白名单,修改allow.jar.digest属性添加允许上传的文件SH256值?
java·jar·bi
蜜獾云44 分钟前
linux firewalld 命令详解
linux·运维·服务器·网络·windows·网络安全·firewalld
ZSYP-S1 小时前
Day 15:Spring 框架基础
java·开发语言·数据结构·后端·spring