log4j2單獨文件日誌打印配置

一、建立一個單獨的Appenderapp

<RollingFile name="TESTLOG" fileName="${LOG_PATH}/test_error.log"
             filePattern="${LOG_PATH}/test_error-%d{yyyyMMdd}-%i.log.gz">
    <PatternLayout pattern="%d{yyyy/MM/dd HH:mm:ss.SSS} %t [%p] %c{1} (%F:%L) %msg%n" />
    <Filters>
        <ThresholdFilter level="ERROR"  onMatch="ACCEPT" onMismatch="DENY"/>
    </Filters>
    <Policies>
        <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
        <SizeBasedTriggeringPolicy size="256MB"/>
    </Policies>
    <DefaultRolloverStrategy>
        <Delete basePath="${LOG_PATH}" maxDepth="2">
            <IfFileName glob="test_error-*.log.gz" />
            <IfLastModified age="3d" />
        </Delete>
    </DefaultRolloverStrategy>
</RollingFile>

二、添加對應的Loggeride

<Loggers>
    <Root level="INFO">
        <Appender-ref ref="Console"/>
        <Appender-ref ref="FILEINFO"/>
        <Appender-ref ref="FILEERROR"/>
    </Root>
    <Logger name="com.xxx.dao.mapper" level="debug" additivity="false">
        <Appender-ref ref="FILEINFO"/>
    </Logger>
    <!--日誌再單獨一份輸出到指定文件下, 如想在正常的日誌中再打印一次配置additivity="ture" -->
    <Logger name="test_log" level="info" additivity="false">
        <Appender-ref ref="TESTLOG"/>
    </Logger>
</Loggers>

三、代碼中spa

private final static Logger logger = LoggerFactory.getLogger("test_log");

@Override
public void printLog() {
    //打印到TESTLOG對應Appender的目錄文件
    logger.info("-------test-----------");
}

 

這樣日誌就單獨打印到test_error.log了, debug

相關文章
相關標籤/搜索