Customizing log format in logging.properties
Asked Answered
N

2

5

I need some direction in configuring the log format in Tomcat 7. I am relatively new at logging configurations so please excuse humor this questions if it seems a bit basic...

Using the standard logging in Tomcat configured in logging.properties displays a log in the format of:

Jun 6, 2011 9:27:00 AM com.class.Control_WS callWebService 
INFO: Response received from Control_WS:[Y]

I would like to customize these logs to compress on to one line as well as expanding the date format to include milliseconds.

Example:

[2011-05-04T11:37:00.037|INFO|javax.enterprise.system.stream.out|Response recieved from Control_WS:[Y]]

Is that something I can do using the JUL or do I need to switch to LOG4J?

Any simple examples that can be provided or direction would be much appreciated.

Noguchi answered 6/6, 2011 at 16:33 Comment(0)
P
9

If you are using Java 7 (or later :-)) you no longer have to create a custom formatter class for java.util.logging (JUL). In Java 7 there's a new property, java.util.logging.SimpleFormatter.format, which controls how JUL's SimpleFormatter prints the information. So as long as you are using SimpleFormatter (which is the default anyway) then this will work.

Some pitfalls:

  • Be sure to actually use Java 7. :-)
  • Be sure to use a valid format string. JUL will revert silently to the default format (the standard ugly two-liner) if the format you provide is invalid.
Parquet answered 26/9, 2012 at 12:10 Comment(0)
J
3

It looks like you would do this by creating a custom class that extends Formatter and then specify it in your logging.properties as the formatter for your handler. For example, if you are using a ConsoleHandler you would use this:

java.util.logging.ConsoleHandler.formatter = com.mycompany.MyFormatter

Then place the class file (either by itself, or inside a jar) somewhere in the classpath of tomcat. I would create a jar with it in there and then put that jar in ${catalina.home}/lib/ext

Jetblack answered 27/10, 2011 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.