1. 程式人生 > >springMVC配置log4j2列印日誌

springMVC配置log4j2列印日誌

1.匯入jar包:

<properties>  
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
        <!-- log4j日誌檔案管理包版本 -->
        
        <log4j.version>2.7</log4j.version>
    </properties>  



<!-- log4j2 -->  
        
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>${log4j.version}</version>
            <scope>runtime</scope>
        </dependency>

2.建立log4j2.xml檔案:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Configuration status="off" monitorInterval="1800">
<properties>
	<property name="LOG_HOME">D://work\AllWorkspace\logs</property>
	<property name="ERROR_LOG_FILE_NAME">error</property>
	<property name="INFO_LOG_FILE_NAME">info</property>
	<property name="PATTERN">%d %-5p [%t] %C{1} (%F:%L) - %m%n</property>
</properties>

     <Appenders>  
         <Console name="Console" target="SYSTEM_OUT">  
             <PatternLayout pattern="${PATTERN}"/>  
         </Console>  
   
         <RollingRandomAccessFile name="ErrorLog"  
                                  fileName="${LOG_HOME}/${ERROR_LOG_FILE_NAME}.log"  
                                  filePattern="${LOG_HOME}/${ERROR_LOG_FILE_NAME}.log.%d{yyyy-MM-dd}.gz">  
             <PatternLayout pattern="${PATTERN}"/>  
             <Policies>  
                 <TimeBasedTriggeringPolicy/>  
                 <SizeBasedTriggeringPolicy size="100 MB"/>  
             </Policies>  
             <DefaultRolloverStrategy max="20"/>  
         </RollingRandomAccessFile>  
   
         <RollingRandomAccessFile name="InfoLog"  
                                  fileName="${LOG_HOME}/${INFO_LOG_FILE_NAME}.log"  
                                  filePattern="${LOG_HOME}/${INFO_LOG_FILE_NAME}.log.%d{yyyy-MM-dd}.gz">  
             <PatternLayout pattern="${PATTERN}"/>  
             <Policies>  
                 <TimeBasedTriggeringPolicy/>  
                 <SizeBasedTriggeringPolicy size="100 MB"/>  
             </Policies>  
             <DefaultRolloverStrategy max="20"/>  
         </RollingRandomAccessFile> 
         
		<Socket name="Logstash" host="192.168.3.59" port="4560" protocol="TCP">
            <JsonLayout locationInfo="true" compact="true" eventEol="true" />
        </Socket>
   
     </Appenders>  
   
     <Loggers>  
         <!-- 3rdparty Loggers -->  
         <logger name="org.springframework.core" level="info"></logger>  
         <logger name="org.springframework.beans" level="info"></logger>  
         <logger name="org.springframework.context" level="info"></logger>  
         <logger name="org.springframework.web" level="error"></logger>  
   
         <logger name="org.zyyd" level="all" includeLocation="true" additivity="false">
             <appender-ref ref="Console"/>  
             <appender-ref ref="InfoLog"/>  
         </logger> 
          
         <logger name="org.zyyd.base.util.LogUtil" level="all" includeLocation="true" additivity="false">
             <appender-ref ref="Console"/>  
             <appender-ref ref="Logstash"/>  
         </logger>  
   
         <root level="info" includeLocation="true">  
             <appender-ref ref="Console"/>  
             <appender-ref ref="ErrorLog"/>  
             <appender-ref ref="InfoLog"/>  
         </root>

         <!--對應的mybatis查詢日誌-->
         <!--debug對應的只到語句,而TRACE還會列印結果條數-->
         <!--name只到dao則只是語句,而如果是org.zyyd.base.dao.mapper則會列印每行的結果-->
         <logger name="org.zyyd.base.dao" level="DEBUG" additivity="false">
             <appender-ref ref="Console"/>
             <appender-ref ref="InfoLog"/>
         </logger>
         <!--<Logger name="org.zyyd.base.dao" level="debug" additivity="false">-->
             <!--<AppenderRef ref="mybatisSql" />-->
         <!--</Logger>-->

     </Loggers>  
</Configuration>

3.在mybatis配置檔案那宣告下使用的是log4j2

<!-- sessionFactory 將spring和mybatis整合 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- <property name="configLocation" value="classpath:sqlMapConfig.xml" /> -->
        <property name="mapperLocations" value="classpath:org/zyyd/base/dao/mapper/*.xml" />    <!-- 載入mapper檔案 如果如介面在同一路徑下可不寫 -->
        <property name="typeAliasesPackage" value="org.zyyd.base.entity"></property>	<!-- 掃描實體類 -->
        <property name="typeHandlersPackage" value="org.zyyd.base.entity.typehandler"></property>
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>   //宣告mybatis配置檔案
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>
                            params=mysql
                        </value>
                    </property>
                </bean>
            </array>
        </property>

    </bean>

mybatis-config.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="LOG4J2" />
    </settings>
</configuration>

 

大功完成!