(五)使用slf4j和logback进行日志记录

原文:https://www.cnblogs.com/taiyonghai/p/9290641.html

引入jar包

此处如果是引用了spring boot则不需要再引一下的jar包了,spring boot中已集成了这些包

 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>2.0.0-alpha1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.3.0-alpha5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.3.0-alpha5</version>
        </dependency>  

配置文件的位置

在resource文件夹配置logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n </pattern>
        </encoder>
    </appender>
    <property name="LOG_HOME" value="D:/logback" />
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n lvchengda</pattern>
            <charset>utf-8</charset>
        </encoder>
        <file>${LOG_HOME}/log/output.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${LOG_HOME}/log/output.log.%i</fileNamePattern>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>1kb</MaxFileSize>
        </triggeringPolicy>
        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/logs/aa.%d{yyyy-MM-dd}.%i.log.zip</fileNamePattern>
            <maxFileSize>50MB</maxFileSize>  <!-- 日志文件过大会使的编辑器打开非常慢,因此设置日志最大50MB -->
            <maxHistory>30</maxHistory>  <!-- 保存30天 -->
            <totalSizeCap>10GB</totalSizeCap>  <!-- 总日志大小 -->
        </rollingPolicy>
    </appender>

    <root level="INFO">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>

</configuration>

代码运用:  

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

public class TestLog {
    static Logger logger = LoggerFactory.getLogger(TestLog.class);
    public static void main(String[] arge){
        logger.debug("taiyonghai debug");
        logger.info("taiyonghai info");
        logger.error("taiyonghai error");
        logger.warn("taiyonghai warn");
    }
}

 

上一篇:运行java报错 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".


下一篇:日志框架1---多种日志门面和日志实现初步了解(转载)