• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Changing Timestamp of HtmlLayout of log4j

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Esha Goel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 3285
    13
    Mac OS X Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey John,

    Nice solution, I like the creativity there! I'm blatantly ripping off your code for future personal use
     
    John N Armstrong
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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!
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 3285
    13
    Mac OS X Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Sweet...brute force.
     
    Martijn Verburg
    author
    Posts: 3285
    13
    Mac OS X Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Brent and welcome to Javaranch! I'm not sure what your Brute force comment alludes to?
     
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    reply
      Bookmark Topic Watch Topic
    • New Topic