Search code examples
javaloggingresourcebundlejava.util.logging

Is there a minimalist way to get a default ResourceBundle when you do not need localization?


I want to inherit to extend java.util.logging.Logger. The only visible constructor is

protected Logger(String name, String resourceBundleName)

This is despite the fact that when you want to get an instance, they offer two ways:

static Logger   getLogger(String name)
static Logger   getLogger(String name, String resourceBundleName)  

Is there a way to obtain some sort of default ResourceBundle or default name of a ResourceBundle so that I can use the constructor without creating an internationalization properties file and associated object?

Of course I could simply produce a wrapper class and use getLogger(String name) to avoid the need for the ResourceBundle name. Unfortunately that means I would have to implement a wrapper method for every Logger method I want to use. (Too much typing).

Edit: The following attempt to get the default localization data from Logger does not work.

import java.util.logging.Logger;

class JLogger extends Logger
{
    static Logger dummyLogger = Logger.getLogger("com.dummy.utilities");

    private JLogger(String name)
    {
        super(name, JLogger.dummyLogger.getResourceBundleName());
    }

    static JLogger getJLogger(String name)
    {
        return new JLogger(name);
    }

    public void severe() // a zero arguments version of severe
    {
        severe("");
    }
}

The getResourceBundleName() just returns a null. The super(name, resourceName) constructor builds something which does not do any logging if the second argument is null.


Solution

  • Unfortunately that means I would have to implement a wrapper method for every Logger method I want to use. (Too much typing).

    No need to do that, it's been done already. Have a look at SLF4J and Commons Logging, they provide sensible wrappers around java.util.logging, hiding the awfulness of that API.

    Those two APIs can also delegate to a proper underlying log implementation such as Logback or Log4J, bypassing java.util.logging altogether.