我正在Netbeans中使用Maven开始一个Java项目.我写了一些代码来使用Logger类进行日志记录.但是,日志记录似乎不起作用.在我的程序开始时,我运行:
Logger.getLogger(ProjectMainClass.class.getName()).setLevel(LOG_LEVEL);
Logger.getLogger(ProjectMainClass.class.getName()).log(LOG_LEVEL, "Hello Logger");
第二行从不将我的消息输出到Netbeans的输出屏幕.
System.out.print语句确实显示在输出中.
我觉得我需要设置一些配置选项.我一直在搜索,但我无法弄明白(过去我总是使用System.out和调试器进行调试,但我认为记录器功能更强大(更令人困惑)).
解决方法:
如果您正在使用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.您可以在JRE中的配置文件logging.properties中找到它,例如
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"
标签:java,logging,maven,netbeans
来源: https://codeday.me/bug/20190723/1509467.html