Spring Log4j2 log4j2.xml

时间:2021-08-15 00:05:41
<dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>1.7.21</version>
        </dependency>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>demo</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <listener>
        <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
    </listener>

    <filter>
        <filter-name>log4jServletFilter</filter-name>
        <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>log4jServletFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console" />
        </Root>
        <logger name="org.springframework.web" level="info">
            <AppenderRef ref="Console" />
        </logger>
        <logger name="java.sql.Connection" level="info">
            <AppenderRef ref="Console" />
        </logger>
        <logger name="java.sql.Statement" level="debug">
            <AppenderRef ref="Console" />
        </logger>
        <logger name="java.sql.PreparedStatement" level="debug">
            <AppenderRef ref="Console" />
        </logger>
        <logger name="java.sql.ResultSet" level="info">
            <AppenderRef ref="Console" />
        </logger>
        <logger name="com.ibatis.common.jdbc.SimpleDataSource" level="info">
            <AppenderRef ref="Console" />
        </logger>
        <logger name="com.ibatis.common.jdbc.ScriptRunner" level="info">
            <AppenderRef ref="Console" />
        </logger>
        <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate"
            level="info">
            <AppenderRef ref="Console" />
        </logger>
        <logger name="com.ibatis" level="debug">
            <AppenderRef ref="Console" />
        </logger>
    </Loggers>
</Configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!-- status=debug 可以查看log4j的装配过程 -->
<configuration status="off" monitorInterval="1800">
    <properties>

        <property name="LOG_PATH">/home/oracle/logs/dingyingsi</property>
        <!--
        <property name="LOG_PATH">C:/Users/dingyingsi/logs/dingyingsi</property>
        -->
        <property name="LOG_NAME">dingyingsi</property>
    </properties>
    <appenders>
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout
                pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />
        </Console>
        <RollingRandomAccessFile name="ProductionLog"
            fileName="${LOG_PATH}/${LOG_NAME}.log" filePattern="${LOG_PATH}/${LOG_NAME}.%d{yyyy-MM-dd}.log">
            <PatternLayout
                pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %level [%thread][%file:%line] - %msg%n" />
            <Policies>
                <TimeBasedTriggeringPolicy interval="1"
                    modulate="true" />
            </Policies>
        </RollingRandomAccessFile>
    </appenders>
    <loggers>
        <root level="info">
            <appender-ref ref="ProductionLog" />
            <!--
            <appender-ref ref="Console" />
            -->
        </root>
        <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="java.sql.Connection" level="info" />
        <logger name="java.sql.Statement" level="info" />
        <logger name="java.sql.PreparedStatement" level="info" />
        <logger name="java.sql.ResultSet" level="info" />
        <logger name="java.sql.PreparedStatement" level="info" />
        <logger name="com.ibatis.common.jdbc.SimpleDataSource" level="info" />
        <logger name="com.ibatis.common.jdbc.ScriptRunner" level="info" />
        <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="info" />
        <logger name="com.ibatis" level="info" />
        <logger name="org.apache.http" level="info" />
    </loggers>
</configuration>