將log4j2的配置文件log4j2.xml拆分成多個xml文件


  在日常的項目開發中,我們可能會使用log4j2分離系統日志與業務日志 ,這樣一來,log4j2.xml 這個配置文件可能就會變得非常臃腫、龐大,那么我們可以將這個文件拆分成多個配置文件嗎? 答案是肯定可以的,現在我們就來拆拆看:

  假如最初的配置文件是這樣的:

<?xml version="1.0" encoding="UTF-8"?>

<configuration status="OFF">
    <Properties>
        <property name="log_pattern"> %d %-5p [%c] %m%n</property> 
        <property name="basePath">/logs</property>

        <property name="common-msg">${basePath}/common.log</property>
        <property name="rollingfile_common-msg">${basePath}/common%d{yyyy-MM-dd}.log</property>

        <property name="error-msg">${basePath}/error.log</property>
        <property name="rollingfile_error-msg">${basePath}/error%d{yyyy-MM-dd}.log</property>

    </Properties>
    <!--先定義所有的appender -->
    <appenders>
        <!--輸出控制台的配置 -->
        <Console name="Console" target="SYSTEM_OUT">
            <!--控制台只輸出level及以上級別的信息(onMatch),其他的直接拒絕(onMismatch) -->
            <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" />
            <!--輸出日志的格式 -->
            <PatternLayout pattern="${log_pattern}" />
        </Console>


        <RollingFile name="rollingfile_common-msg"
            filename="${common-msg}" filePattern="${rollingfile_common-msg}"
            append="true">
            <Filters>
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
             </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>

        <RollingFile name="rollingfile_error-msg"
            filename="${error-msg}" filePattern="${rollingfile_error-msg}"
            append="true">
             <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout>
                <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %class{36} %L %M - %msg%xEx%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
    </appenders>
    <loggers>
        <AsyncLogger name="common.test.log4j2" level="info" additivity="false"  includeLocation="true">
            <AppenderRef ref="rollingfile_common-msg"  />
            <AppenderRef ref="rollingfile_error-msg" />
        </AsyncLogger>
    </loggers>
</configuration>

  根據上面配置文件結構,假設我們按照 Properties、appenders、loggers  總共拆成三個配置文件(具體怎么拆分可以根據自己的實際需要進行);

首先看看第一個文件,包含Properties配置(直接定義成一個父配置文件吧,log4j2.xml):

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF" xmlns:xi="http://www.w3.org/2001/XInclude" name="XIncludeDemo">
    <!-- 定義下面的引用名 -->
    <Properties>
        <property name="log_pattern"> %d %-5p [%c] %m%n</property> 
        <property name="basePath">/logs</property>

        <property name="common-msg">${basePath}/multTest.log</property>
        <property name="rollingfile_common-msg">${basePath}/multTest%d{yyyy-MM-dd}.log</property>

        <property name="error-msg">${basePath}/multTesterror.log</property>
        <property name="rollingfile_error-msg">${basePath}/multTesterror%d{yyyy-MM-dd}.log</property>

    </Properties>
</configuration>

再來看看包含appenders的配置文件(log4j2_appender.xml):

    <appenders>
        <!--輸出控制台的配置 -->
        <Console name="Console" target="SYSTEM_OUT">
            <!--控制台只輸出level及以上級別的信息(onMatch),其他的直接拒絕(onMismatch) -->
            <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" />
            <!--輸出日志的格式 -->
            <PatternLayout pattern="${log_pattern}" />
        </Console>


        <RollingFile name="rollingfile_common-msg"
            filename="${common-msg}" filePattern="${rollingfile_common-msg}"
            append="true">
            <Filters>
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
             </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>

        <RollingFile name="rollingfile_error-msg"
            filename="${error-msg}" filePattern="${rollingfile_error-msg}"
            append="true">
             <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout>
                <Pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %class{36} %L %M - %msg%xEx%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy />
            </Policies>
        </RollingFile>
    </appenders>

接下來該包含loggers的配置文件了(log4j2_logger.xml):

    <loggers>
        <AsyncLogger name="common.test.log4j2" level="info" additivity="false"  includeLocation="true">
            <AppenderRef ref="rollingfile_common-msg"  />
            <AppenderRef ref="rollingfile_error-msg" />
        </AsyncLogger>
    </loggers>

原來的log4j2.xml 已經拆分成三個配置文件了,但是三個文件之間還沒有關系,是無法使用的,最后,我們在log4j2.xml中加入關系映射:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF" xmlns:xi="http://www.w3.org/2001/XInclude" name="XIncludeDemo">
    <!-- 定義下面的引用名 -->
    <Properties>
        <property name="log_pattern"> %d %-5p [%c] %m%n</property> 
        <property name="basePath">/logs</property>

        <property name="common-msg">${basePath}/multTest.log</property>
        <property name="rollingfile_common-msg">${basePath}/multTest%d{yyyy-MM-dd}.log</property>

        <property name="error-msg">${basePath}/multTesterror.log</property>
        <property name="rollingfile_error-msg">${basePath}/multTesterror%d{yyyy-MM-dd}.log</property>

    </Properties>
 <xi:include href="log4j2_appender.xml" /> <!--配置關系 --> <xi:include href="log4j2_logger.xml" />
</configuration>

到此為止,log4j2.xml拆分已經完成了。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM