Java Logger未在Netbeans中记录输出 [英] Java Logger is not logging to output in Netbeans

查看:154
本文介绍了Java Logger未在Netbeans中记录输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Netbeans中使用Maven开始一个Java项目。我写了一些代码来使用Logger类进行日志记录。但是,日志记录似乎不起作用。在我的程序开始时,我运行:

I'm starting a Java project using Maven in Netbeans. I wrote some code to do logging with the Logger class. However, the logging doesn't seem to work. At the beginning of my program, I run:

Logger.getLogger(ProjectMainClass.class.getName()).setLevel(LOG_LEVEL);
Logger.getLogger(ProjectMainClass.class.getName()).log(LOG_LEVEL, "Hello Logger");

第二行永远不会将我的消息输出到Netbeans的输出屏幕。
System.out.print 语句确实显示在输出中。

The second line never outputs my message to the output screen in Netbeans. System.out.print statements do show up in the output.

我觉得我需要设置一些配置选项。我一直在搜索,但我无法弄清楚(过去我总是使用System.out和调试器进行调试,但我认为记录器功能更强大(更令人困惑))。

I feel like I need to set some configuration option. I've searched around, but I can't figure it out (in the past I've always debugged with System.out and debugger, but I think the logger is much more powerful (and more confusing)).

推荐答案

如果您正在使用Java Logging API,并且您有类似下一个的java类:

If you are using Java Logging API and if you have a java class like the next:

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 中找到它,例如

What happens? What about the other levels? What am I doing wrong? Don't worry. Be happy! This is because the default level for Java Logging API is INFO. You can find this in the configuration file logging.properties in the JRE, e.g.

D:\Software\jdk1.6.0_43\jre\lib\logging.properties

在此文件中,我们可以看到以下行:

In this file, we can see the lines:

# 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

并且,如文本所示,控制台处理程序的级别:

And, as the text indicates, the level for the console handler:

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = INFO

这就是为什么输出中的最大细节级别为 INFO 。您可以更改此文件或代码中的级别(最好不影响其他进程的级别),例如:

This is the reason why the maximum level of detail in the output is INFO. You can change the level in this file or in your code (this is best to not affect the level of other processes), e.g.:

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");
        }
    }
}

最后一个代码的输出是:

The output for the last code is:

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 为所需的级别添加一个参数到虚拟机:

You can also use another file logging.properties whith the desired level adding an argument to the virtual machine:

-Djava.util.logging.config.file="C:\mylogging.properties"

这篇关于Java Logger未在Netbeans中记录输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆