MHarris
MHarris

Reputation: 1821

Is it possible to set up two FileHandlers in the same logging.properties file?

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.

Upvotes: 3

Views: 4626

Answers (3)

user1820801
user1820801

Reputation: 114

it is possible, i don't know how to declare it in the logging properties however

public static void main(String args[])  {

    Logger logger=Logger.getLogger("");
    FileHandler fh=new FileHandler("<yourPath" +
            "%g.txt",20000,5);
    fh.setFormatter(new SimpleFormatter());
    FileHandler fd=new FileHandler("yourSecondPath" +
            "%g.txt",20000,5);
    fd.setFormatter(new SimpleFormatter());

    logger.addHandler(fd);
}

you can easily replace simpleFormaters by your own formater and set different level logging for the two handlers but again i don't find any information on the syntax to use in the logging.properties file in order to obtain this behaviour

Upvotes: 0

MHarris
MHarris

Reputation: 1821

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.

Upvotes: 4

Rob Grant
Rob Grant

Reputation: 7358

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);
  }
}

Upvotes: -1

Related Questions