Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

java - Change global setting for Logger instances

I'm using java.util.logging.Logger as the logging engine for my application. Each class uses it's own logger, i.e., each class has:

private final Logger logger = Logger.getLogger(this.getClass().getName());

I want to set a logging level for all my classes, and be able to change it (i.e., have the setting in one place). Is there a way to do this, other that using a global Level variable and manually set each logger to it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One easy way is to use a logging properties file, by including this VM argument:

-Djava.util.logging.config.file="logging.properties" 

where "logging.properties" is the path to a file containing logging configuration. For relative paths, the working directory of the process is significant.

In that file, include a line like this:

.level= INFO

This sets the global level, which can be overridden for specific handlers and loggers. For example, a specific logger's level can be overridden like this:

 com.xyz.foo.level = SEVERE

You can get a template for a logging properties file from jre6liblogging.properties.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...