如何使用过滤器截断日志文件?

问题描述 投票:0回答:2

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

这是我的文件

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!

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

P.S.:信息文件工作正常!

debugging struts2 log4j logfile
2个回答
0
投票

您可以将错误级别设置为

categorie
s,例如:

# 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

了解更多关于日志级别


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>
© www.soinside.com 2019 - 2024. All rights reserved.