2015-08-18 109 views
1

我幾乎已經嘗試了一切,但log4j似乎不壓縮日誌文件。我正在使用1.2.17 jars for log4j和log4j extras。似乎不明白什麼是錯的。它只需創建一個文件GZ但多數民衆贊成一個正常的文件,而無需壓縮 的log4j.xml爲什麼日誌文件沒有用這個log4j配置壓縮?

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> 
<log4j:configuration debug="true" 
xmlns:log4j='http://jakarta.apache.org/log4j/'> 

<appender name="file" class="org.apache.log4j.rolling.RollingFileAppender"> 
<rollingPolicy name="file" class="org.apache.log4j.rolling.TimeBasedRollingPolicy"> 
<param name="ActiveFileName" value="${catalina.home}/logs/broker.log" /> 
<param name="FileNamePattern" value="=${catalina.home}/logs/broker-.%d{yyyyMMdd-HH}.log.gz" /> 
</rollingPolicy> 

<triggeringPolicy class="org.apache.log4j.rolling.SizeBasedTriggeringPolicy"> 
<param name="MaxFileSize" value="10000"/> 
</triggeringPolicy> 

<layout class="org.apache.log4j.PatternLayout"> 
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%"/> 
</layout> 

</appender> 
<root> 
     <level value="INFO" /> 
     <appender-ref ref="file" /> 
    </root> 

</log4j:configuration> 
+0

應給文件名的'。 gz'擴展名自動壓縮文件。如果不是一個不同的appender,我會認爲這將是一個設置。 –

+1

檢查了這一點: http://stackoverflow.com/questions/3329385/compress-log4j-files – Jaydatt

+0

log4j的應用自動文件壓縮,如果fileNamePattern選項的值以.gz結束或.zip –

回答

1

回答我自己的問題,這是我如何使它工作

#Worked with 2.17 version 
log4j.rootLogger=INFO, file 

log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender 
log4j.appender.file.rollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy 
log4j.appender.file.rollingPolicy.maxIndex=5 
log4j.appender.file.triggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy 
log4j.appender.file.triggeringPolicy.MaxFileSize=10000 
log4j.appender.file.rollingPolicy.FileNamePattern=${catalina.home}/logs/broker-%i.log.gz 
log4j.appender.file.rollingPolicy.ActiveFileName=${catalina.home}/logs/broker.log 
log4j.appender.file.layout=org.apache.log4j.PatternLayout 
log4j.appender.file.layout.ConversionPattern=%d [%t] %-5p (%F:%L) - %m%n 
+2

此鏈接幫助我! http://how.vndemy.com/java/log4j/1164-how-to-configure-log4j-using-log4j-properties/#7_Rotate_038_Archiving_File_On_Size –

+0

add apache-log4j-extras:1.2.7並使用鏈接中的配置以上 – AntuanSoft