javalogging

Java Logger does not print anything to console


I'm just starting to learn about Logger in Java; however, I don't understand how it works.

I have this code right after my class declaration:

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

Then in my methods I use it like this (examples):

LOGGER.log(Level.FINE, "Testing");
LOGGER.info("More testing...");

...
} catch(Exception e) {
    LOGGER.log(Level.SEVERE, e.toString(), e);
}

But nothing prints to the console. Is it being saved in a file somewhere or am I just using it incorrectly?

I am using Eclipse Mars 2 with Java 1.6.0_22 if it makes a difference.

EDIT: I am using the java.util.logging.Logger library.


Solution

  • 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