This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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!
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.