Create log file with date using log4j

梦想的初衷 提交于 2019-12-03 08:42:25
crush

As is mentioned in this StackOverflow Q&A, the purpose of a RollingFileAppender is to automatically create a new log file at some defined interval. In the case of the DailyRollingFileAppender, that interval is 12:00 AM of each day.

What this means is that the first file created by log4j will have the file name you specified here:

log4j.appender.FILE.File=log4j/QueryLog.log

And, from then forward, each day a new log file will be created with the date appended to it.

To always name the file with the date appended, you could use DatedFileAppender by Geoff Mottram

jandresrodriguez

This line sets the log file name, in your log4j properties you have: log4j.appender.FILE.File=log4j/QueryLog.log

You can see the answer here Setting a log file name to include current date in Log4j

The solution to log directly to a file with current active date/time such as XYZ.log.20150101.log instead of XYZ.log could be done by simply removing ActiveFileName property when using the rolling package org.apache.log4j.rolling.RollingFileAppender in the apache-log4j-extras 1.1 with log4j 1.2.x.

<appender name="defaultFileAppender" class="org.apache.log4j.rolling.RollingFileAppender">
    <param name="append" value="true" />
    <param name="Threshold" value="INFO" />
    <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
        <param name="FileNamePattern"
            value="${catalina.base}/logs/application/custom-application-logger.%d{yyyy-MM-dd_HH_mm}" />
    </rollingPolicy>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
            value="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %-10t [%-40.40c] %x - %m%n" />
    </layout>
</appender>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!