java Log4j 最大文件大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1187254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:32:55  来源:igfitidea点击:

Log4j Maxfilesize

javatomcatlog4j

提问by

Problem i have with log4j is that when i specify maxfilesize it works properly for the files generated in the src folder of project, but the log files generated in tomcat bin, are not splited into seperate files and then i have a large log file which is not desirable. The following is log4j.xml:

我对 log4j 的问题是,当我指定 maxfilesize 时,它​​对项目的 src 文件夹中生成的文件正常工作,但是在 tomcat bin 中生成的日志文件没有拆分为单独的文件,然后我有一个大日志文件不可取。以下是log4j.xml:

  <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
    <log4j:configuration>
        <appender name="fatalFile" class="org.apache.log4j.RollingFileAppender">
            <param name="MaxFileSize" value="100KB" />
            <param name="MaxBackupIndex" value="5" />
            <param name="File" value="logs/pnusn/fatal.log" />
            <param name="threshold" value="fatal" />
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%5p in Class:%C{2}, Thread:%t, at:%d{ABSOLUTE}:: '%m'%n" />
            </layout>
        </appender>
        <appender name="othersFile" class="org.apache.log4j.RollingFileAppender">
            <param name="MaxFileSize" value="100KB" />
            <param name="MaxBackupIndex" value="5" />
            <param name="File" value="logs/pnusn/others.log" />
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%5p in Class:%C{2}, Thread:%t, at:%d{ABSOLUTE}:: '%m'%n" />
            </layout>
            <filter class="org.apache.log4j.varia.LevelRangeFilter">
                <param name="LevelMin" value="debug" />
                <param name="LevelMax" value="error" />
            </filter>
        </appender>
        <appender name="traceFile" class="org.apache.log4j.RollingFileAppender">
            <param name="MaxFileSize" value="200KB" />
            <param name="MaxBackupIndex" value="20" />
            <param name="File" value="logs/pnusn/trace.log" />
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%5p in Class:%C{2}, Thread:%t, at:%d{ABSOLUTE}:: '%m'%n" />
            </layout>
            <filter class="org.apache.log4j.varia.LevelRangeFilter">
                <param name="LevelMin" value="trace" />
                <param name="LevelMax" value="trace" />
            </filter>
        </appender>
        <root>
            <priority value="trace"></priority>
            <appender-ref ref="fatalFile" />
            <appender-ref ref="othersFile" />
            <appender-ref ref="traceFile" />
        </root>
    </log4j:configuration>

Does anybody know how can i fix it?

有谁知道我该如何解决?

Thanks for your consideration.

感谢您的考虑。

回答by skaffman

The log4j log files are appearing in your tomcat's bin directory because you haven't told it to do otherwise.

log4j 日志文件出现在您的 tomcat 的 bin 目录中,因为您没有告诉它做其他事情。

The tomcat docs heredescribe how to integrate log4j, including the use of the ${catalina.out}substitution to refer to the tomcat root directory. The instructions refer to the log4j properties file format, too, it's not clear if it'll work for log4j.xml

这里tomcat 文档描述了如何集成 log4j,包括使用${catalina.out}替换来引用 tomcat 根目录。这些说明也参考了 log4j 属性文件格式,尚不清楚它是否适用于 log4j.xml

As for the file size problem, I'm not sure why that's happening. Try using the DailyRollingFileAppenderinstead, and see if that works.

至于文件大小问题,我不确定为什么会这样。尝试使用DailyRollingFileAppender代替,看看是否有效。