Java Logger is not logging to output in Netbeans

旧城冷巷雨未停 提交于 2019-12-06 04:23:21

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

You notice that the output is:

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

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

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

EDIT

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"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!