按照log.xml指定Log打印文件

前端之家收集整理的这篇文章主要介绍了按照log.xml指定Log打印文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

依赖jar包

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>
        <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
        </dependency>

logback.xml 日志文件

<?xml version="1.0"?>
<configuration>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="test" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>c:/data/log/test.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>c:/data/log/test-%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">    
            <maxFileSize>1024MB</maxFileSize>
        </triggeringPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%-20(%d{HH:mm:ss.SSS} [%thread]) %-5level %logger{80} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="info" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>c:/data/log/info.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>c:/data/log/info-%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%msg%n</pattern>
        </encoder>
    </appender>
    <!-- 定义的日志基本打印位置,可以定义 日志基本level,如果定义 就打印响应的日志基本,不定义就是所有-->
    <logger name="monitorLog" additivity="false">
        <appender-ref ref="info" />
        <appender-ref ref="console" />
    </logger>
    <!-- 没有定义的日志名称,的打印日志的级别 -->
    <root level="INFO">
        <appender-ref ref="test" />
        <appender-ref ref="console" />
    </root>
</configuration>

代码测试

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestLog {
    private static Logger log = LoggerFactory.getLogger(TestLog.class);
    private static Logger monitorLog = LoggerFactory.getLogger("monitorLog");
    public static void main(String[] args) {
        log.error("zhong1");
        monitorLog.info("zhong");

    }
}

猜你在找的XML相关文章