简体   繁体   English

java.util.logging:动态设置日志级别

[英]java.util.logging: setting the log level dynamically

I'm trying the following code example (based on code from here ). 我正在尝试以下代码示例(基于来自此处的代码)。 My aim is to set the level of the logging from run time. 我的目标是从运行时设置日志记录级别。

package logchecker;
import java.util.logging.*;

public class Logchecker {

    private static final Logger logger = Logger.getLogger(Logchecker.class.getName());

   public static void main(String[] args) {
      System.out.println("This logger's level is " + logger.getLevel());   // null
      logger.setLevel(Level.ALL);
      System.out.println("This logger's level is " + logger.getLevel());   // null
      logger.info("TEST");
      logger.finest("FINEST TEST");
   }
}

The output is: 输出为:

This logger's level is null 该记录器的级别为null

This logger's level is ALL 该记录器的级别为ALL

Sep 17, 2013 1:46:31 PM logchecker.Logchecker main 2013年9月17日下午1:46:31 logchecker.Logchecker主要

INFO: TEST 信息:测试

It obviously doesn't output the log.finest . 它显然不输出log.finest What am I missing? 我想念什么? I'm running with NetBeans 7.3. 我正在运行NetBeans 7.3。

I needed to set my Handlers level as well. 我还需要设置处理程序级别。 Added the following code to my main: 在我的主服务器上添加了以下代码:

Logger root = Logger.getLogger("");
Handler[] handlers = root.getHandlers();
for(Handler h: handlers){
    h.setLevel(Level.INFO);
}

Of course you can set the level to whatever you need. 当然,您可以根据需要设置级别。

thanks again to the comments for directing me to the solution 再次感谢您为我提供解决方案的评论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM