Log4J timestamping a log file using RollingFileAppender

Administrators... if there is a better section for this question then please move this question there.

So what I would like is to timestamp the log file with the system time everytime
a log file is created. Iv'e heard about a timstamp attribute but have not seen how
this is used.

Note: I don't want to use DailRollingFileAppender if I don't have to.

For example...

    <appender name="SERVER" class="org.apache.log4j.RollingFileAppender">
        <param name="file" value="${jboss.server.home.dir}/log/server.log.TIME_STAMP_GOES_HERE"/>
        <param name="MaxFileSize" value="2MB"/>
        <param name="MaxBackupIndex" value="128"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d] [%t] %-6p(%c:%L) - %m {%x}%n"/>
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The issue with attempting to use something like:

<param name="file" value="${jboss.server.home.dir}/log/server.log.${myproperty.date}"/>

is that the file will always be created with that same date, in other words, this properties file is loaded once when your application is loaded.

If you *really* wanted to have the time at the end of the file, a hack would be to create a seperate thread in your application which calls System.setProperty("myproperty.date", new Date()) every minute or so, and in *addition* to this, you need to call DOMConfigurator.configureAndWatch("/conf/your-config.xml", 1000) once  to instruct log4j to reload the configuration file every so often in order to load the new file name!

I think you're better off staying with RollingFileAppender or DailyRollingFileAppender.
defa0009Author Commented:
Thanks... I knew that setting a system property would not work. I am
thinking I may be out of luck on this one but there is an attribute called
timestamp for the appender and I'm hoping the answer lies there.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

defa0009Author Commented:
It would be if i could roll the file on size and not by day...
Hmm, I think one of my core assumptions was wrong. You don't need to reload the configuration file at all, the file name is always generated based on the actual property value at the time the file is rolled.

This link describes something similar to what I described above, using properties to timestamp a rolling file::

The only issue now is deciding on how to update the system properties. Again, a seperate thread can be started, or perhaps you can even wrap your logger calls, and inside the wrapper update the properties every X seconds using time deltas.
defa0009Author Commented:
Thanks for your input... I have thought of that solution already but that means having a
class to set those system properties and that won't do. I can only edit the log4j.xml
configuration file. I don't think I can do what I want using a RollingFileAppender. I need
to roll on size and need that rolled file timestamped. I am very surprised that the Log4J
developers did not think of adding this functionality.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java App Servers

From novice to tech pro — start learning today.