• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1517
  • Last Modified:

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"/>
        </layout>
    </appender>
0
defa0009
Asked:
defa0009
  • 3
  • 2
1 Solution
 
szigetiCommented:
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.
0
 
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
defa0009Author Commented:
It would be if i could roll the file on size and not by day...
0
 
szigetiCommented:
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::
http://osdir.com/ml/apache.logging.log4cxx.user/2005-02/msg00062.html

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.
0
 
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.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now