Log4J timestamping a log file using RollingFileAppender

Posted on 2008-11-13
Last Modified: 2013-12-02
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"/>
Question by:defa0009
    LVL 2

    Expert Comment

    The issue with attempting to use something like:

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

    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("", 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.
    LVL 1

    Author Comment

    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.
    LVL 5

    Expert Comment

    LVL 1

    Author Comment

    It would be if i could roll the file on size and not by day...
    LVL 2

    Expert Comment

    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.
    LVL 1

    Accepted Solution

    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.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    VB Script 3 60
    JKS to store upstart data 2 51
    Receive file in Servlet 1 28
    countHi2 challenge 7 30
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now