Java Logger does not print anything to console

My assumption is that you are using Log4J. If that is correct, the logger should be saving to a file by default not printing to a console (which is usually done with System.out.println("message")). Do you have a log4j.properties file?


The nothing is displayed because there are no handlers attached to the Logger with name MyClass.class.getName();

But,there is one handler by default in the parent logger instance.This handler is ConsoleHandler that has log level INFO.

It's easy to verify:

public class StackAnswerAboutLogger {

private static final Logger LOGGER = Logger.getLogger(StackAnswerAboutLogger.class.getName());
private static final Logger PARENT_LOGGER = Logger.getLogger("");

public static void main(String[] args) {
    LOGGER.info("Count of LOGGER handlers: " + Integer.toString(LOGGER.getHandlers().length));
    LOGGER.info("Count of PARENT_LOGGER handlers: " + Integer.toString(LOGGER.getHandlers().length));
    Handler defaultConsoleHandler = PARENT_LOGGER.getHandlers()[0];
    LOGGER.info("Default Console Handler Log Level: " + defaultConsoleHandler.getLevel().toString());
  }

}

Output:

Jan 10, 2020 10:23:53 PM init.StackAnswerAboutLogger main
INFO: Count of LOGGER handlers: 0
Jan 10, 2020 10:23:54 PM init.StackAnswerAboutLogger main
INFO: Count of PARENT_LOGGER handlers: 0
Jan 10, 2020 10:23:54 PM init.StackAnswerAboutLogger main
INFO: Default Console Handler Log Level: INFO

To display the log with ANY level, you could configure either PARENT_LOGGER handlers or LOGGER ones.

Handler defaultConsoleHandler = PARENT_LOGGER.getHandlers()[0];
LOGGER.info("Default log level of PARENT_LOGGER: " +PARENT_LOGGER.getLevel().toString());
LOGGER.info("Default Console Handler Log Level: "+defaultConsoleHandler.getLevel().toString());
defaultConsoleHandler.setLevel(Level.ALL);
PARENT_LOGGER.setLevel(Level.ALL);
LOGGER.log(Level.FINEST, "here is finest log");
LOGGER.log(Level.SEVERE, "here is severe log"); 

Output:

Jan 10, 2020 10:38:10 PM init.StackAnswerAboutLogger main
INFO: Default log level of PARENT_LOGGER: INFO
Jan 10, 2020 10:38:10 PM init.StackAnswerAboutLogger main
INFO: Default Console Handler Log Level: INFO
Jan 10, 2020 10:38:10 PM init.StackAnswerAboutLogger main
FINEST: here is finest log
Jan 10, 2020 10:38:10 PM init.StackAnswerAboutLogger main
SEVERE: here is severe log

This seems to work for me in a brand new Eclipse project.

import java.util.logging.Logger;

public class Foobar {

    public static final Logger LOGGER = Logger.getLogger(Foobar.class.getName());;

    public Foobar() {
        LOGGER.info("Constructed");
    }

    public static void main(String[] args) {
        new Foobar();
    }
}

eclipse console


Here's what you should know, java.util.Logging is controlled by a root logger file found in the JRE/lib folder called logging.properties that defaults to Level.INFO, hence Fine messages are not displayed by default since FINE is lower than INFO,

 private static final Logger LOGGER = Logger.getLogger(MyClass.class.getName());

 Handler handlerObj = new ConsoleHandler();
 handlerObj.setLevel(Level.ALL);
 LOGGER.addHandler(handlerObj);
 LOGGER.setLevel(Level.ALL);
 LOGGER.setUseParentHandlers(false);

Now you can use your LOGGER object just fine and should work. checkout Java Logging Overview

 LOGGER.log(Level.FINEST, "finest");

Remember that there's a reason the Log Level is set to FINE, so,lowering the level could also print unnecessary info from core libraries. Let me know if it helps.

Regards Douglas

Tags:

Java

Logging