This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Other Open Source Projects and the fly likes Changing Timestamp of HtmlLayout of log4j Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Changing Timestamp of HtmlLayout of log4j" Watch "Changing Timestamp of HtmlLayout of log4j" New topic
Author

Changing Timestamp of HtmlLayout of log4j

Esha Goel
Greenhorn

Joined: Jan 24, 2008
Posts: 4
Hi All,

I am using HTMLLayout for logging using log4j.

I want to change the default timestamp that comes with HTMLLayout.
something like this 2008-01-28 17:13:02,057 .

Please let me know how can we do that.

Thanks in advance
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Yes this can be done in the log4j.properties (or lg4j.xml) file, it's called a conversion pattern. See here for details.

Hint: It takes Java SimpleDateFormat compatible formats

Hope that helps!


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Esha Goel
Greenhorn

Joined: Jan 24, 2008
Posts: 4
Trying conversion pattern like below is not working, as HTML layout donot extend pattern layout.

log4j.appender.MY_FILE=org.apache.log4j.RollingFileAppender
log4j.appender.MY_FILE_FILE.File=logs/myfile/MyLog.html
log4j.appender.MY_FILE_FILE.MaxFileSize=2500KB
log4j.appender.MY_FILE_FILE.MaxBackupIndex=5
log4j.appender.MY_FILE_FILE.Append=true
log4j.appender.MY_FILE_FILE.layout=org.apache.log4j.HTMLLayout
#log4j.appender.MY_FILE_FILE.layout.ConversionPattern=[%d{MMM dd HH:mm:ss}] %-5p (%F:%L) - %m%n
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi Esha,

Hmm, you seem to be quite right. In that case I'm not sure of any option but to write a custom class that extends HTMLLayout, see here for a guide.
John N Armstrong
Greenhorn

Joined: Nov 27, 2008
Posts: 2
I had this same desire: to alter the format of the timestamp in log records produced by Log4J's HTMLLayout. After some extensive Googling, it appeared that there were only 3 options:

  • Re-invent the wheel and write a custom HTMLLayout class
  • Extend the Log4J HTMLLayout class and replace the format() method
  • Modify the source code for the Log4J HTMLLayout class


  • Unfortunately, none of these were very appealing options; all I needed was to change the format of the timestamp!

    Then it hit me: there's another option that involves overriding the format() method, but not rewriting it in its entirety. What I decided to do was write a custom HTML layout class and intercept the log record (by overriding the format() method) and use RegEx to locate and replace the timestamp with one having the desired format. And the beauty of it is that the format can be specified externally as a property in the log4j configuration file!

    Sample code for my layout class is shown below.

    Martijn Verburg
    author
    Bartender

    Joined: Jun 24, 2003
    Posts: 3274
        
        5

    Hey John,

    Nice solution, I like the creativity there! I'm blatantly ripping off your code for future personal use
    John N Armstrong
    Greenhorn

    Joined: Nov 27, 2008
    Posts: 2
    No problem, Martijn. Glad I could help. I just hate it when I can't do something that should be simple. Fortunately, this one yielded without too much trouble -- once I finally hit on the solution!
    veera sachin
    Greenhorn

    Joined: Dec 03, 2008
    Posts: 1
    hi.. i too faced same html layout date/time problem.. i created custom layout but how can i import this custom class into log4j API?? or how will Jboss take this class?? please help??

    thanks in advance...
    Martijn Verburg
    author
    Bartender

    Joined: Jun 24, 2003
    Posts: 3274
        
        5

    Hi Veera and welcome to Javaranch!

    Note the



    So you can simply add this class to the log4j package or (probably better), just make sure it's in the CLASSPATH that your log4j.jar is in.
    Brent Rr
    Greenhorn

    Joined: May 19, 2010
    Posts: 1
    Sweet...brute force.
    Martijn Verburg
    author
    Bartender

    Joined: Jun 24, 2003
    Posts: 3274
        
        5

    Hi Brent and welcome to Javaranch! I'm not sure what your Brute force comment alludes to?
    iti garg
    Greenhorn

    Joined: Oct 03, 2008
    Posts: 9
    Hi John,

    I tried to run the solution that you had provided above i.e. overriding of format method. But i failed to run that. Please can you provide me solution how to run that? I mean to run that class i must have main method inside that class. So, in that main method how to call format()?
    If we write main method in that class and call format method then What would be the parameter for format method?
    Or is there any method to call that class from log4j.properties?

    Please reply me with solution as soon as possible.

    Thanks & Regard,
    Iti Garg
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Changing Timestamp of HtmlLayout of log4j