tags:

views:

107

answers:

2

Using the logging classes in java.util.logging, is it possible to set up two different FileHandlers with different formatters that will write different logging information to two different files?

I'm currently using a logging.properties file, and the handlers line is not encouraging:

handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler

I don't see how I could distinguish between two java.util.logging.FileHandlers later on in the file.

Looking at Related Questions, it looks like switching to Log4J would give me the desired flexibility, but I'd rather avoid a dependency on another library if the JSE logging library can be finagled into doing what I want somehow.

A: 

You can create a custom log level by extending the Level class; just give it a unique ID.

import java.util.logging.*;

public class CustomLogLevel extends Level
{
  public static void main(String[] args) 
  {
    Logger log = Logger.getLogger("robertgrant.org");
    Level templevel = Level.WARNING;
    Level level = new CustomLogLevel("Rob Level", templevel.intValue());
    Level customlevel = level.parse("Rob Level");
    log.log(customlevel, "This is from a custom level");
  }

  public CustomLogLevel(String name, int value){
    super(name, value);
  }
}
Robert Grant
How could I have made the question more understandable? If you look at it again, there's no mention of Custom Log Levels anywhere.Thank you for your time.
MHarris
I wondered whether this would solve your problem for you; if you set up two loggers associated with different logfiles, then you can access a specific logger using a custom log level.
Robert Grant
Oh I see! Doesn't help me set up the two loggers in the first place, which was the thrust of my question (apologies for the ambiguity), but that would certainly help disambiguate once I'd got them set up. Thanks!
MHarris
+1  A: 

A detailed read through of the relevant API suggests a resounding No.

The choice then is between dynamically creating the logger in code, as demonstrated in the answer to this question and just giving up and using Log4J, or another more sophisticated logging library.

MHarris