Dennis
Dennis

Reputation: 616

Java Logger is not logging to output in Netbeans

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

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

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)).

Upvotes: 3

Views: 9508

Answers (1)

Paul Vargas
Paul Vargas

Reputation: 42010

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"

Upvotes: 12

Related Questions