1

我需要一些过滤器来截断我的 Java EE 应用程序写入的大量日志信息。我正在使用Struts2。

这是我的文件log4j.properties

# Define the root logger with appender file
log = E:\\Uiip\\ProjectWork\\Workspace
log4j.rootLogger = DEBUG, DEBUG_APPENDER
log4j.logger.OTHER_LOGGER=DEBUG, INFO_APPENDER

log4j.additivity.OTHER_LOGGER = false

#File appender for log debug
log4j.appender.DEBUG_APPENDER=org.apache.log4j.FileAppender
log4j.appender.DEBUG_APPENDER.File=${log}/logDebug.txt

#File Appender for log info
log4j.appender.INFO_APPENDER=org.apache.log4j.FileAppender
log4j.appender.INFO_APPENDER.File=${log}/logInfo.txt

# Define the layout for file appender log debug
log4j.appender.DEBUG_APPENDER.layout=org.apache.log4j.PatternLayout
log4j.appender.DEBUG_APPENDER.layout.conversionPattern=%d [%t] %m%n

# Define the layout for file appender log info
log4j.appender.INFO_APPENDER.layout=org.apache.log4j.PatternLayout
log4j.appender.INFO_APPENDER.append=false
log4j.appender.INFO_APPENDER.layout.ConversionPattern= %d [%t] %m%n

每次我启动我的应用程序时,仅用于登录,logdebug 文件就变成了大约 1MB!

我可以使用什么来过滤我的调试日志文件?

Ps 信息文件工作正常!

4

2 回答 2

0

您不能使用属性文件配置进行过滤。请改用 xml 配置。带有过滤器的示例配置

<appender name="MyFileAppender" class="org.apache.log4j.FileAppender">
    <param name="File" value="myfile.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{HH:mm:ss} [%t] %m%n"/>
    </layout>
    <filter class="org.apache.log4j.varia.StringMatchFilter">
        <param name="StringToMatch" value="MyStringToMatch" />
        <param name="AcceptOnMatch" value="true" />
    </filter>
    <filter class="org.apache.log4j.varia.DenyAllFilter"/>
</appender>
于 2013-04-17T14:35:46.810 回答
0

您可以将错误级别设置为categories,例如:

# This will always be printed (except TRACE level...);
log4j.category.org.apache.struts2=DEBUG

# This will be printed only in INFO or higher
log4j.category.org.springframework.beans.factory.xml=INFO
log4j.category.org.springframework.jdbc.core.StatementCreatorUtils = INFO

# This will be printed only in ERROR or higher
log4j.category.com.opensymphony.xwork2=ERROR
log4j.category.freemarker.beans=ERROR
log4j.category.freemarker.cache=ERROR

阅读有关日志级别的更多信息

于 2013-04-17T11:45:27.913 回答