Configure the root logger with java logging API

16,814

Solution 1

I would suggest to use Apache Commons Logging or log4j directly. It's much more comfortable and flexible.

EDIT:

If you want to configure your logging from a properties file, you should go with Vinay Sajip's answer. To get the root Logger inside your program and configure it using the API, you could ask for the Logger named "", like it says here

private final static Logger logger = Logger.getLogger("");

Solution 2

Use the following to get the root logger:

Logger system = Logger.getLogger("");

You can now access the root logger as any other logger.

In my case I was willing to make it quiet. I succeeded to do it with the following code:

private static void setLevel(Logger pLogger, Level pLevel) {
    Handler[] handlers = pLogger.getHandlers(); 
    for (Handler h : handlers) {
        h.setLevel(pLevel);
    }
    pLogger.setLevel(pLevel);
}

...

setLevel(system, Level.OFF);

Hope this helps.

Solution 3

I presume you mean the java.util.logging (JUL) implementation: if so, have a look at the relevant part of the Java Almanac. The overview information for JUL configuration is here. Basically, the approach is to use a single configuration file for all loggers.

Update: Removed the link to the Java Almanac, as it is no longer valid (linkrot strikes again), as mentioned in jschoen's comment. Please use Google to find alternative sources of information.

Share:
16,814
Sebastian Müller
Author by

Sebastian Müller

Financial Engineer, Software Engineer, Product Management, Mathematics, Finance, Insurance, IT Architect

Updated on August 29, 2022

Comments

  • Sebastian Müller
    Sebastian Müller over 1 year

    how can I configure the behaviour of the root logger in the logging api? I don't want to configure the behaviour of each logger separately, instead it would be very convenient if I have a single property file where I can set the behaviour of all loggers.

  • Sebastian Müller
    Sebastian Müller over 14 years
    Why is it much more comfortable? I have set up everything for logging with the logging api, it would not be comfortable to change everything.
  • Tim Büthe
    Tim Büthe over 14 years
    @Xelluloid: Yes, you're right, it would be more comfortable if you had used log4j in the first place. I don't knew that you already went to far to change this sort of thing... my bad.
  • Sebastian Müller
    Sebastian Müller over 14 years
    this does not work as I mentioned in my comment under my question it seems that I do not access the root logger by ""
  • Sebastian Müller
    Sebastian Müller over 14 years
    oh sorry I've done a mistake =) now it works fine, thanks to you
  • Trevor Boyd Smith
    Trevor Boyd Smith about 13 years
    -1, the questions says "using xyz library" how do i do "abc"... your answer says "Don't use xyz library... oh by the way here is what some other guy said" which is rude and not constructive. <br> That's like someone saying "my windows computer has a problem with abc" and then the person being asked the question responds with "Don't use Windows. You should be using Linux (or Mac)".
  • Trevor Boyd Smith
    Trevor Boyd Smith about 13 years
    Saying "I would suggest to use Apache Commons Logging or log4j directly. It's much more comfortable and flexible." is only appropriate if the question is "I'm using java.util.logging but not happy with it. what other similar libraries are there that I can use?"
  • Jacob Schoen
    Jacob Schoen over 11 years
    Don't know if I caught it on a bad day or not but your link to Java Almanac seems to be broke.