loggers with different handler properties

  • Thread starter Simo-Pekka Sadeluoto
  • Start date
S

Simo-Pekka Sadeluoto

I have two java.util.logging.Loggers that both should use the same
Handler class but with different properties. How should I configure this
using my own properties defined with java.util.logging.config.file?

My current properties file has something like this (unimportant stuff
stripped out):

handlers = java.util.logging.FileHandler
java.util.logging.FileHandler.pattern = /tmp/a_%u.log
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter

But what I would like to do is use two Loggers that write stuff to
different files with different Formatters. Or to be exact, I have
written my own Handler and Formatter implementations, but let's not make
it too complicated. My code looks like this:

Logger a = Logger.getLogger("a");
Logger b = Logger.getLogger("b");
a.log(Level.INFO, "test a");
b.log(Level.INFO, "test b");

But how do I tell to logger b that it should use FileHandler with
pattern "/tmp/b_%u.log" and SimpleFormatter? Now both use FileHandler
properties defined in the properties file. Of course I could hard-code
it like this:

Handler h = new FileHandler("/tmp/b_%u.log");
h.setFormatter(new SimpleFormatter());
b.addHandler(h);
b.setUseParentHandlers(false); // nothing to /tmp/a_%u.log

All I loose this way is the flexibility! I also tried adding facility
specific properties like this to configuration:

b.pattern = /tmp/b_%u.log
b.formatter = java.util.logging.SimpleFormatter

But this does not affect the behaviour of Logger b at all. And how could
it, pattern and formatter are properties of Handler and not of Logger.
Have they really not thought of this, all the examples I found only set
..level per facility!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top