java.util.logging: how to set level by logger package (or prefix)?

血红的双手。 提交于 2019-12-17 19:39:10

问题


My app uses many libraries and I'm using java.util.logging for logging. I'd like to be able to set different logging levels for each library by doing something like:

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

Is is possible?


回答1:


You shouldn't use "*". A sample logging.properties could be such as:

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

And if all "org" level should be logged as WARNING then

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



回答2:


I was able to get it working like this:

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



回答3:


It would have been nice to control logging using only logging.properties:

org = FINE
com = SEVERE

Unfortunately, the corresponding log must have actually been created. Changing your conf file won't do that work for you. Add the loggers yourself and it will work:

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

Nested loggers in your application work the same way:

# 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


来源:https://stackoverflow.com/questions/3920930/java-util-logging-how-to-set-level-by-logger-package-or-prefix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!