How to use java logging with

Developer has two way to let java use specified properties file to configure its core Logger

1. Use the system property

From description of  java.util.logging .LogManager --

       the "java.util.logging.config.file" system property can be used to specify a properties file (in java.util.Properties format). The initial logging configuration will be read from this file.

But if you try to use the System.setProperty to specify the file path, surely you fail

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

you will find that the LogManager still use the properties file which locate in

JRE/lib/ that's because the LogManager is initialized during VM startup, before main executes. 

The correct way to do this is sepcify the path use command

 java -Djava.util.logging.config.file=configFile MainClass

when start up the JVM

You can find more information in google search by this key words

         System.setProperty( "java.util.logging.config.file",

2. If you want to configure this in your source code the only way is to write code as the following code fragment.

LogManager manager = LogManager.getLogManager();

manager.readConfiguration(new FileInputStream(new File(FILE_PATH)));


In this way you can let you logger work as you describe in your log file

also pls set handlers= java.util.logging.FileHandler, else all your setting will not take effect


想对作者说点什么? 我来说一句