java.util.logging:如何通过logger包(或前缀)设置级别?

时间:2010-10-13 05:07:37

标签: logging package prefix java.util.logging log-level

我的应用使用了很多库,而我正在使用java.util.logging进行日志记录。我希望能够通过以下方式为每个库设置不同的日志记录级别:

org.datanucleus.*.level = WARNING
com.google.apphosting.*.level = WARNING
com.myapp.*.level = FINE

有可能吗?

3 个答案:

答案 0 :(得分:6)

你不应该使用“*”。 示例logging.properties可以是:

handlers=java.util.logging.ConsoleHandler
.level=ALL

java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter

org.datanucleus.level=WARNING
org.datanucleus.handler=java.util.logging.ConsoleHandler

com.myapp.level=FINE
com.myapp.handler=java.util.logging.ConsoleHandler

如果所有“组织”级别都应记录为警告,那么

org.level=WARNING
org.handler=java.util.logging.ConsoleHandler

答案 1 :(得分:1)

我能够让它像这样工作:

handlers= java.util.logging.ConsoleHandler

.level= INFO

java.util.logging.ConsoleHandler.level = ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

com.myapp.level = ALL
com.myapp.handler=java.util.logging.ConsoleHandler

答案 2 :(得分:0)

仅使用logging.properties控制日志记录会很不错:

org = FINE
com = SEVERE

不幸的是,必须实际创建相应的日志。更改您的conf文件将无法为您完成这项工作。自己添加记录器,它将起作用:

private static final Logger ORG_ROOT_LOGGER = Logger.getLogger("org");
private static final Logger COM_ROOT_LOGGER = Logger.getLogger("com");

应用程序中的嵌套记录器以相同的方式工作:

# perhaps in the main entry point for your application?
private static final Logger APP_ROOT_LOGGER = Logger.getLogger("com.myapp");

# in each package or class you want to have separately controlled loggers
private static final Logger LOG = Logger.getLogger(HelloWorldApp.class.getName());

# in logging.properties
com.myapp.level = FINE  # sufficient to make all your loggers log as FINE
com.myapp.HelloWorldApp.level = SEVERE  # turn off msgs from that particularly chatty app