How to redirect java.util.logging to a file?

I have a Java program using an external library. The main program uses log4jto register their messages, and is used in the library java.util.logging.

My problem is that the log messages from the external library and the main program are mixed in the console.

I would like to redirect all log messages from an external library to a file. I tried to do this using a file logging.properties:

handlers= java.util.logging.FileHandler
.level= INFO
java.util.logging.FileHandler.pattern = foo.log
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter

This file is initialized with:

System.setProperty("java.util.logging.config.file", "logging.properties");

Unfortunately, journal messages from the external library continue to be displayed in the console. Should I use something like slf4jto intercept log messages from java.util.logging?

Thank you for your time.

+5
1

. . config - , . .

Logger rootLogger = Logger.getLogger(""); 
logHandler = new FileHandler(config.getLogFile(), 
                             config.getLogRotateSize()*1024*1024, 
                             config.getLogRotateCount(), false); 
logHandler.setFormatter(new SimpleFormatter()); 
logHandler.setLevel(Level.INFO); 
rootLogger.removeHandler(rootLogger.getHandlers()[0]); 
rootLogger.setLevel(Level.INFO); 
rootLogger.addHandler(logHandler); 

, . . , .

+11

All Articles