Sammy
Sammy

Reputation: 1218

Force Log4J to create a new file every day (that may be empty)

I configured log4j to write use DailyRollingFileAppenders to write log-entries to some different files. One of these logs is special in that case, that it is a summary of all the other logs and its threshold is warn (not info like the others).

The idea behind this is simple: If there are no entries for a day, than everything is fine and there is no need to look at the detail-logs.

Log4J rolls that file when writing the first entry of the day - so if one opens that log and there was no entry written that day, the log will show days-old entries.

Is there a way to force log4j to roll a file every day without the need of a log-entry? Or alternatively to roll at startup?

Edit: Here are the properties:

**snipped**
log4j.appender.empty=org.apache.log4j.DailyRollingFileAppender
log4j.appender.empty.MaxBackups=30
log4j.appender.empty.Append=true
log4j.appender.empty.File=/some/dir/shouldBeEmpty.txt
log4j.appender.empty.layout=org.apache.log4j.PatternLayout
log4j.appender.empty.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
**snipped**

Just to be sure: I know, I can just call a default-warn at nightly-startup and this will roll that file. But the question is, if it is possible to roll without logging.

Upvotes: 4

Views: 5484

Answers (2)

LostKatana
LostKatana

Reputation: 504

What you are searching for is the CustodianDailyRollingFileAppender. This can be found here.

Here is an example of the usage in an xml properties file:

<appender name="CDRFA" class="de.pickert.utils.logger.CustodianDailyRollingFileAppender">
    <param name="File" value="log/filename.log" />
    <param name="Append" value="true" />
    <param name="DatePattern" value ="yyyy-MM-dd"/>
    <param name="CompressBackups" value ="true"/>
    <!-- param name="MaxFileSize" value ="100mb"/>
    <param name="MaxNumbersOfDays" value ="14"/>
    <param name="DeleteFile" value ="false"/-->
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%p %C %d{yyyy-MM-dd HH\:mm\:ss.SSS} %m \n" />
    </layout>
</appender>

EDIT: Haven't seen that you want to create an empty file as well. Maybe this helps:

TimeBasedRollingPolicy

specifies the use of a log filename that contains the current date. You do not need to specify a value for triggeringPolicy when you use this policy. To specify when a new log file is created, you can specify either of the following options:

  • Creation of (rollover to) a new log file whenever the generated filename differs from the current filename. This is the default behavior. For example, if the filename includes the current year, month, and day, then a new file is created when the system date changes to a new day.
  • Creation of a new log file only when a new session begins. When rollover occurs, the message Log continues in path-and-filename is written to the end of the current file. The message Log continued from path-and-filename is written to the beginning of the newly created file.

Use the following syntax to specify TimeBasedRollingPolicy:

<rollingPolicy class="TimeBasedRollingPolicy">
    <param name="fileNamePattern" value="filename-pattern"/>
    <param name="rollOver" value="TRUE | FALSE"/>
</rollingPolicy>

name="fileNamePattern" value="path-and-filename-pattern"
specifies the path to which the log file is written and the conversion pattern that is used to create the log filename. The conversion pattern can include the following characters:

%d indicates where the current date appears. You can specify a date format or a date and time pattern in braces after %d if you want the date to appear in a format other than yyyymmdd, or if you want to include additional information such as the hour.

%S{key} indicates where system information (such as the host name, operating system, or system description) appears. You must specify a key to indicate the type of system information that appears.

For example, specify c:\logs\MetadataServer_%d_%S{host_name}.log if you want the log files to be written to the path c:\logs\ and the filename to include the current date and the name of the metadata server host machine.

Default: None
Required: Yes
Interaction: path and filename is overwritten by any value that you specify for path-and-filename.

name="rollOver" value="TRUE | FALSE"
indicates whether a new log file is created whenever the generated filename differs from the current filename. Specify one of the following values:

TRUE
creates (rolls over to) a new file whenever the generated filename differs from the current filename.

Quit not sure if this fits your needs

FALSE
creates a new log file only when a new session begins.

Default: TRUE
Required: No

source

Upvotes: 1

Saqib Rezwan
Saqib Rezwan

Reputation: 1408

You can set the log4j configuration from code using two simple class java.util.Properties and org.apache.log4j.PropertyConfigurator. So at first load the whole log4j data (what you have provied with the question) in the java.util.Properties and then set it in org.apache.log4j.PropertyConfigurator. So what you can do, you can take the value from log4j.appender.file.File of your own.

Example:

String fileName = "//some//dir//" + {DD_MM_YY} + ".txt";`
log4jData += "log4j.appender.empty.File=" + fileName;`

Now, if you can set it once by your code, you can set it as many times as you want. Create a worker which will be responsible of two things:

  1. if the application starts/restarts then set the desired log4j properties and
  2. Configure the new log4j properties (based on your data) after a certain period (like 24 hours).

Hopefully, it will work. Sorry for my bad English.

Upvotes: 0

Related Questions