Vishnu
Vishnu

Reputation: 499

How to customize the default java logging?

I need to have custom log levels(other than java.util.logging.Level), and custom functions in the java util logger to log messages of these levels.

Custom levels: 1. DEBUG 2. ERROR 3. FATAL

So I created XYZLevel extended from Level as follows:

public class OKILevel extends Level {

public OKILevel(String name, int value) {
    super(name, value);
}

/* OKI Log Levels */
public static final Level FATAL = new OKILevel("FATAL",
        Level.SEVERE.intValue() + 100);

public static final Level ERROR = new OKILevel("ERROR",
        Level.SEVERE.intValue());

public static final Level DEBUG = new OKILevel("DEBUG",
        Level.FINE.intValue());

}

Custom functions required: .debug(), .severe() etc similar to .info in logger.

For logger I do:

static final Logger logger = Logger.getLogger(ABC.class.getName());

I am unable to figure out:

static final XYZLogger logger = XYZLogger.getLogger(ABC.class.getName());

extending the Logger doesn't help.

Also, for this application the logging.properties need to be passed in. How do I use these new levels in .level of logging.properties?

Upvotes: 0

Views: 1070

Answers (1)

jmehrens
jmehrens

Reputation: 11045

If you are creating log wrapper then you should leverage some of the standard frameworks before inventing something new.

If you want your application to use a new logger implementation then you have to implement and install a custom LogManager.

Also, for this application the logging.properties need to be passed in. How do I use these new levels in .level of logging.properties?

All of the .level parsing performed via the Level.parse method. Per the docs:

The argument string may consist of either a level name or an integer value.

For example:

  • "SEVERE"

  • "1000"

So you should be able to write:

#Level.SEVERE + 100 = FATAL
some.logger.name.level = 1100

That said there are some known issues with finding the correct level value by int in some releases of Java.

Another option if you just want the rendered output of a level to be different then you should have a look at Best way to override java.util.logging Level class to change the level strings.

Upvotes: 0

Related Questions