slf4j+log4j2 configuration

1. Add the jar package dependency in the pom file of the newly created maven project (the dependency code is at the end of the step):

The jar packages that need to add dependencies are:

The api interface package of slf4j: slf4j-api

The core package of log4j2: log4j-core

The api interface package of log4j2: log4j-api

slf4j corresponds to the driver package of log4j2 log framework: log4j-slf4j-impl

Asynchronous log function package of log4j2: com.lmax.disruptor

Solve the possible warning jar package of web project log4j: log4j-web

2. Configure the log4j2.xml log configuration file (the content of the log configuration file is placed at the end of the step):

·The default read path of the log configuration file is src/main/resource

If the file is placed in the default read path, there is no need to configure the read path

·If the file is placed in another path, you need to set the read path of the log configuration file in web.xml, the code is as follows:

<context-param>
   <param-name>log4jConfiguration</param-name>
   <param-value>classpath:log4j2.xml</param-value>
</context-param>

3. Use the log function in the code:

  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
 
  public class logTest {
    
    
  
      // 通过slf4j接口创建Logger对象
      private static final Logger logger = LoggerFactory.getLogger(logTest.class);
  
      public static void main(String[] args) {
    
    
         logger.info("this is info");
         logger.error("this is error");
     }
 }

For a detailed description of the log configuration file, please refer to the log4j2 configuration file log4j2.xml. The following are the jar package dependencies that need to be added to pom.xml and an example of the content of the log configuration file log4j2.xml:

<properties>

<!-- jar包版本设置 -->

<slf4j.version>1.7.21</slf4j.version>

<log4j.version>2.11.0</log4j.version>

</properties>

<dependencies>

<!-- 导入slf4j的接口包以及对应日志框架的驱动包 -->

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>${
    
    slf4j.version}</version>

</dependency>

<!--用于slf4j与log4j2保持桥接 -->

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->

<dependency>

    <groupId>org.apache.logging.log4j</groupId>

    <artifactId>log4j-slf4j-impl</artifactId>

    <scope>runtime</scope>

    <version>${
    
    log4j.version}</version>

</dependency>

 

<!-- 导入日志框架核心包与接口包 -->

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->

<dependency>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-core</artifactId>

<scope>runtime</scope>

<version>${
    
    log4j.version}</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->

<dependency>

    <groupId>org.apache.logging.log4j</groupId>

    <artifactId>log4j-api</artifactId>

    <version>${
    
    log4j.version}</version>

</dependency>

 

<!--用于解决web环境下关闭服务器时可能出现的log4j线程无法及时关闭的warn,web工程需要包含log4j-web,非web工程不需要 -->

<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-web -->

<dependency>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-web</artifactId>

<scope>runtime</scope>

<version>${
    
    log4j.version}</version>

</dependency>

 

<!--使用log4j2的AsyncLogger时需要包含disruptor -->

<!-- https://mvnrepository.com/artifact/com.lmax/disruptor -->

<dependency>

    <groupId>com.lmax</groupId>

    <artifactId>disruptor</artifactId>

    <scope>runtime</scope>

    <version>3.4.2</version>

</dependency>

</dependencies>

Example of log configuration file content:

<?xml version="1.0" encoding="UTF-8"?>
<!--status日志级别从低到高分为TRACE < DEBUG < INFO < WARN < ERROR < FATAL-->
<Configuration status="off" monitorInterval="1800">
    <properties>
        <property name="Log_Home">${
    
    sys:catalina.home}/logs/miniapp</property>
        <property name="ERROR_LOG_FILE_NAME">error</property>
    </properties>
    <Appenders>
        <!--输出控制台的配置-->
        <Console name="Console" target="SYSTEM_OUT">
            <!--输出日志的格式
            %L::输出代码中的行号。
            %M:输出产生日志信息的方法名。-->
            <!--"%highlight{%d{HH:mm:ss.SSS} %-5level %logger{36}.%M() @%L - %msg%n}{FATAL=Bright Red, ERROR=Bright Magenta, WARN=Bright Yellow, INFO=Bright Green, DEBUG=Bright Cyan, TRACE=Bright White}"-->

            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36}.%M %L行 : %msg%xEx%n"/>
        </Console>
        <!--这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档-->
        <RollingFile name="RollingFileInfo" fileName="${Log_Home}/info.${date:yyyy-MM-dd}.log" immediateFlush="true"
                     filePattern="${Log_Home}/$${date:yyyy-MM}/info-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36}.%M %L行 : %msg%xEx%n"/>
            <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
            <filters>
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            </filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
        </RollingFile>

        <!--   这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档 -->
        <RollingFile name="RollingFileDebug" fileName="${Log_Home}/debug.${date:yyyy-MM-dd}.log" immediateFlush="true"
                     filePattern="${Log_Home}/$${date:yyyy-MM}/debug-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36}.%M %L行 : %msg%xEx%n"/>
            <filters>
                <ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="NEUTRAL"/>
            </filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
        </RollingFile>

        <!-- 这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档 -->
        <RollingFile name="RollingFileError" fileName="${Log_Home}/error.${date:yyyy-MM-dd}.log" immediateFlush="true"
                     filePattern="${Log_Home}/$${date:yyyy-MM}/error-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36}.%M %L行 : %msg%xEx%n"/>
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <!-- 3rdparty Loggers -->
        <logger name="org.springframework.core" level="error"/>
        <logger name="org.springframework.beans" level="error"/>
        <logger name="org.springframework.context" level="error"/>
        <logger name="org.springframework.web" level="error"/>
        <Logger name="com.opensymphony.xwork2" level="error"/>
        <Logger name="org.apache.struts2" level="error"/>
        <Logger name="com.mchange.v2.log" level="error"/>
        <Logger name="org.hibernate" level="error"/>
        <Root level="info" includeLocation="true">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
            <appender-ref ref="RollingFileDebug"/>
            <appender-ref ref="RollingFileError"/>
        </Root>
    </Loggers>
</Configuration>

Guess you like

Origin blog.csdn.net/u011930054/article/details/103680287