Java Logger未在Netbeans中记录输出

时间:2013-06-07 20:24:48

标签: java maven logging netbeans

我在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和调试器调试,但我认为记录器功能更强大(更令人困惑))。

1 个答案:

答案 0 :(得分:9)

如果您正在使用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"