MasNotsram
MasNotsram

Reputation: 2273

Incredibly simple Log4j 2.0 example not working

This is a stupidly simple example, and yet for some reason it's not working. I must be missing something obvious.

I am trying to make a very simple log4j 2.0 example program. I have added these two jars to the classpath:

log4j-api-2.0-beta8.jar

log4j-core-2.0-beta8.jar

And have done the simplest example possible, using the default configuration:

package testlog;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class TestLog {

    static Logger logger = LogManager.getLogger(TestLog.class.getName());


    public static void main(String[] args) {

        logger.trace("Hello World");

        System.out.println("Test over");
    }
}

But for some reason, all I get is 'Test over', I never get the Hello World, anywhere I can find anyway. Am I looking in the wrong place? It;s my understanding that with the default configuration it should be printed to the console, with the 'Test Over'. I have changed the log level to info, still the same. I have tried pulling out the Logger functionality into a class, still the same. I am following this tutorial on the log4j documentation page:

http://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticConfiguration

I can't understand what could possibly be wrong. Could anyone shed any light on what I've done wrong? Thanks in advance.

Update The logger DOES work with logger.error(). This would mean it is a problem with the default filters/level no?

Upvotes: 2

Views: 4161

Answers (1)

Udo Klimaschewski
Udo Klimaschewski

Reputation: 5325

You missed that line in the documentation:

Note that by default Log4j assigns the root logger to Level.ERROR.

Do a

logger.error("Hello World");

and it will be displayed.

If you want to display info and/or trace levels, you have to configure your logger.

Upvotes: 11

Related Questions