Multiple formats in logging.properties
Asked Answered
K

1

9

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...

Klystron answered 7/5, 2014 at 8:55 Comment(6)
Implementing your own formatter is not a workaround. It is the proper way to implement the format you want.Dragoon
Yes, but I can specify the format I want with the SimpleFormatter. So why should I develop an own formatter which basically does the same just to be able to configure the logger properly? That sound like a workaround to me.Klystron
If you specify a format for the SimpleFormatter, it will be taken by all instances of the SimpleFormatter. That is not what you want, right? You want a different one per handler. So, you can implement a formatter per format you require.Dragoon
Basically I want the same (but not the identical) formatter - with different configurations. So I guess this is not possible?Klystron
No, it is not possible.Dragoon
I see. Nevertheless: Thank you very much.Klystron
H
3

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
Heritage answered 13/5, 2014 at 2:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.