nils
nils

Reputation: 1382

Multiple formats in logging.properties

I have two handlers in my logging.properties:

handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler

Both use the SimpleFormatter:

java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

But now I want two different formats for these handlers. The console handler should print only a short message. The file handler on the other hand should print a more detailed message. But how to use different formats for both formatters? The following did not work:

java.util.logging.FileHandler.formatter.format = ...

And using

java.util.logging.SimpleFormatter.format

below the corresonding formatters did not work either. Any ideas? I don't want to implement own formatters just as a workaround...

Upvotes: 9

Views: 1994

Answers (1)

jmehrens
jmehrens

Reputation: 11045

The java.util.logging.SimpleFormatter only supports one JVM wide format. If your project includes JavaMail 1.5.2 or later you can use the CompactFormatter on your ConsoleHandler and the SimpleFormatter on your FileHandler.

##logging.properties#
handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.formatter=com.sun.mail.util.logging.CompactFormatter
com.sun.mail.util.logging.CompactFormatter.format=%4$s: %5$s [%1$tc]%n
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%1$tc %2$s%n%4$s: %5$s%6$s%n

Upvotes: 3

Related Questions