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.FileHandler
s 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开发者_JAVA技巧 want somehow.
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.
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
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);
}
}
精彩评论