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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值