Reputation: 373
I am struggling with getting java.util.logging
to log to a file, instead of just to the console in Eclipse.
public class TestDefaultConfiguration {
private static Logger logger = Logger.getLogger(TestDefaultConfiguration.class.getName());
public static void main(String[] args) {
System.out.println("-- main method starts --");
logger.info("an info msg");
logger.warning("a warning msg!");
logger.severe("a severe msg!");
}
}
Here are the properties:
C:\Program Files\Java\jre1.8.0_152\lib\logging.properties
I edited the default properties file to this:
.level= INFO
# default file output is in user's home directory.
#java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.pattern = C:/temp/test/MyLogFile2.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
#java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
When I debug the test class, I can see in logger.manager.props
that these properties are picked up. So far, so good.
Why then is no log-file created in C:/temp/test/MyLogFile2.log
?
Not sure if I should add in Eclipse configuration VM arguments:
-Djava.util.logging.config.file="C:/Program Files/Java/jre1.8.0_152/lib/logging.properties"
But if I do, it doesn't make a difference.
Any suggestions?
Upvotes: 2
Views: 2721
Reputation: 11035
From your logging.properties, you haven't shown the section where you are attaching the handler to a logger. This is explained in the file as comments
# "handlers" specifies a comma separated list of log Handler
# classes. These handlers will be installed during VM startup.
# Note that these classes must be on the system classpath.
# By default we only configure a ConsoleHandler, which will only
# show messages at the INFO and above levels.
handlers= java.util.logging.ConsoleHandler
# To also add the FileHandler, use the following line instead.
#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
Use the logger debugging code to see if the handler is attached to the root logger.
Add the following line to your properties file:
handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
Upvotes: 3