Reputation: 924
I want my Logger with a FileHandler to use the settings defined in my config file. Do I have to use Logger.getLogger or Logger.addHandler(new FileHandler()) after I have used LogManager.readConfiguration(InputStream) or is there more to it? In other words, what is the order that I am supposed to do the following 3 things (getLogger, addHandler, readConfigurations) assuming it has to do with the order and isn't something else?
I wasn't able to find many examples of this and the ones I found had the LogManager.readConfiguration after Logger.getLogger, but that doesn't seem to work.
Here is the config file:
handlers = java.util.logging.FileHandler
.level = ALL
# Default
java.util.logging.FileHandler.limit = 10000000
java.util.logging.FileHandler.count = 10
java.util.logging.FileHandler.append = true
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level = FINE
EDIT: Here is the part of the code relating to the configuration.
String configFilename = "C:\\Users\\dalawh\\Documents\\config.properties";
LogManager manager = LogManager.getLogManager();
String property = manager.getProperty(this.getClass().getPackage().getName()); //DEBUGGING
LogManager.getLogManager().readConfiguration(new FileInputStream(configFilename));
String property2 = manager.getProperty(this.getClass().getPackage().getName()); //DEBUGGING
Logger logger = java.util.logging.Logger.getLogger(this.getClass().getPackage().getName());
String property3 = manager.getProperty(this.getClass().getPackage().getName()); //DEBUGGING
String filename = "C:\\Users\\dalawh\\Documents\\log.log";
this.fileHandler = new FileHandler(filename);
this.logger.addHandler(this.fileHandler);
Upvotes: 2
Views: 14992
Reputation: 11045
Related to this is: configure Logger via global config file.
A call to LogManager.readConfiguration will determine what logger.getLogger will return. If you define a handler in your properties file you will not have to add the handler later. If you want to add a handler using code then the solution you are looking for is:
If you define java.util.logging.config.file
as a system property in your startup script you don't have to write any code.
-Djava.util.logging.config.file="path to file"
For more information read the LogManager class level documentation.
If you can't define that property on startup then you can resort to the LogManager.readConfiguration(InputStream) method to perform the setup. You don't have to call any other methods unless you run into JDK-8033661 readConfiguration does not cleanly reinitialize the logging system or JDK-5035854 LogManager.readConfiguration does not properly modify existing loggers.
If neither of those options work you can resort to just getting a logger and calling the configuration methods as needed.
To troubleshoot your issue add the following:
String configFilename = "C:\\Users\\dalawh\\Documents\\config.properties";
System.out.println(new File(configFilename).length());
LogManager manager = LogManager.getLogManager();
LogManager.getLogManager().readConfiguration(new FileInputStream(configFilename));
String property = manager.getProperty("java.util.logging.FileHandler.formatter"); //DEBUGGING
System.out.println(property);
Here is a simple test to prove that the log manager works.
public class LogManagerTest {
public static void main(String[] arg) throws IOException {
read(LogManager.getLogManager(), create());
FileHandler h = new FileHandler();
h.close();
System.out.println(h.getFormatter());
}
private static Properties create() {
Properties props = new Properties();
props.setProperty("java.util.logging.FileHandler.formatter",
"java.util.logging.SimpleFormatter");
return props;
}
private static void read(LogManager manager, Properties props) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream(512);
props.store(out, "No comment");
manager.readConfiguration(new ByteArrayInputStream(out.toByteArray()));
}
}
Upvotes: 3