如果您使用的是 Java Logging API 并且您有一个类似下一个的 java 类:
import java.util.logging.Level;
import static java.util.logging.Level.*;
import java.util.logging.Logger;
public class Main {
private static final Logger LOG = Logger.getLogger(Main.class.getName());
public static void main(String[] args) {
Level[] levels = {
OFF, SEVERE, WARNING, INFO,
CONFIG, FINE, FINER, FINEST, ALL
};
for (Level level : levels) {
LOG.setLevel(level);
LOG.log(level, "Hello Logger");
}
}
}
您注意到输出是:
Jun 7, 2013 6:30:16 PM Main main
SEVERE: Hello Logger
Jun 7, 2013 6:30:16 PM Main main
WARNING: Hello Logger
Jun 7, 2013 6:30:16 PM Main main
INFO: Hello Logger
怎么了?其他级别呢?我究竟做错了什么?不用担心。要开心!这是因为 Java Logging API 的默认级别是INFO
. 您可以logging.properties
在 JRE 的配置文件中找到它,例如
D:\Software\jdk1.6.0_43\jre\lib\logging.properties
在这个文件中,我们可以看到以下行:
# Default global logging level.
# This specifies which kinds of events are logged across
# all loggers. For any given facility this global level
# can be overriden by a facility specific level
# Note that the ConsoleHandler also has a separate level
# setting to limit messages printed to the console.
.level= INFO
并且,如文本所示,控制台处理程序的级别:
# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO
这就是为什么输出中的最大细节级别是INFO
. 您可以更改此文件或代码中的级别(最好不要影响其他进程的级别),例如:
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import static java.util.logging.Level.*;
import java.util.logging.Logger;
public class Main {
private static final Logger LOG = Logger.getLogger(Main.class.getName());
public static void main(String[] args) {
Level[] levels = {
OFF, SEVERE, WARNING, INFO,
CONFIG, FINE, FINER, FINEST, ALL
};
Logger root = Logger.getLogger("");
// .level= ALL
root.setLevel(ALL);
for (Handler handler : root.getHandlers()) {
if (handler instanceof ConsoleHandler) {
// java.util.logging.ConsoleHandler.level = ALL
handler.setLevel(ALL);
}
}
for (Level level : levels) {
LOG.setLevel(level);
LOG.log(level, "Hello Logger");
}
}
}
最后一个代码的输出是:
Jun 7, 2013 6:31:13 PM Main main
SEVERE: Hello Logger
Jun 7, 2013 6:31:13 PM Main main
WARNING: Hello Logger
Jun 7, 2013 6:31:13 PM Main main
INFO: Hello Logger
Jun 7, 2013 6:31:13 PM Main main
CONFIG: Hello Logger
Jun 7, 2013 6:31:13 PM Main main
FINE: Hello Logger
Jun 7, 2013 6:31:13 PM Main main
FINER: Hello Logger
Jun 7, 2013 6:31:13 PM Main main
FINEST: Hello Logger
Jun 7, 2013 6:31:13 PM Main main
ALL: Hello Logger
编辑
您还可以使用另一个文件logging.properties
,将所需级别的参数添加到虚拟机:
-Djava.util.logging.config.file="C:\mylogging.properties"