Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Logger does not print anything to console

Tags:

java

logging

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.

like image 334
o.o Avatar asked Jun 08 '16 21:06

o.o


3 Answers

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

like image 55
Zuko Avatar answered Oct 03 '22 20:10

Zuko


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
like image 45
skoriy Avatar answered Oct 03 '22 20:10

skoriy


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?

like image 29
Agricola Avatar answered Oct 03 '22 19:10

Agricola