loggers with different handler properties

Discussion in 'Java' started by Simo-Pekka Sadeluoto, Jul 8, 2003.

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

    --
    "The more things change the more they suck!"
    http://www.iki.fi/sipe/ - Butthead
     
    Simo-Pekka Sadeluoto, Jul 8, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Francois Bouffard

    Loggers and reloads

    Francois Bouffard, May 26, 2004, in forum: Python
    Replies:
    2
    Views:
    310
    =?ISO-8859-1?Q?Fran=E7ois_Bouffard?=
    May 27, 2004
  2. Jeff Shannon

    Logging with multiple loggers/handlers

    Jeff Shannon, Jul 6, 2004, in forum: Python
    Replies:
    3
    Views:
    356
    Jeff Shannon
    Aug 7, 2004
  3. ANT Loggers

    , Aug 22, 2006, in forum: Java
    Replies:
    0
    Views:
    479
  4. ANT Loggers

    , Aug 22, 2006, in forum: Java
    Replies:
    0
    Views:
    433
  5. Rhino
    Replies:
    8
    Views:
    546
    markspace
    Apr 30, 2010
Loading...

Share This Page