How to use java logging with logging.properties

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/logging.properties. 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)));

Logger.getLogger(CLASS_NAME)

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

Reference:

http://www.phptr.com/articles/article.asp?p=361635&seqNum=4

你可能感兴趣的:(How to use java logging with logging.properties)