AppSensei
AppSensei

Reputation: 8400

Log4J warning: Initialize log4j system

I am learning log4j. But, I get this error not sure if I'm missing something. I have all the jars in place.

public class LoggingDemo {
    private static final Logger logger = LoggerFactory.getLogger(LoggingDemo.class);

    public static void main(String[] args) {
        logger.info("Starting App....");
        System.out.println("Testing log4j.......");
        logger.info("Terminating App!");
    }

}

enter image description here

Upvotes: 0

Views: 145

Answers (3)

Sudhakar
Sudhakar

Reputation: 3180

When an error appears like please initialize the log4j that means "log4j.properties" file is unavailable in the project class path.

You need to create a log4j.properties file and add it to your class path.

Or You can pass the JVM argument as shown below

-Dlog4j.configuration=file:.\log4j.properties

So JVM will initilize the log4j from the specified property.

In the log4j file you can specify the following things

root logger configuration. WARN, It is the log4j level and ConsoleAppender is appender which prints the out to the console.

log4j.rootLogger=WARN, ConsoleAppender

It seems issue is duplicate of How to initialize log4j properly?

Upvotes: 0

AppSensei
AppSensei

Reputation: 8400

I figured it out. I was missing a log4j.properties file. Someone who has the same issue should make a log4j.properties file in the src folder with the contents given below.

Output to the Console

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

enter image description here

Upvotes: 0

Guido Simone
Guido Simone

Reputation: 7952

I get this all the time when I am creating a new project. You need to create a log4j.properties file and add it to your class path. See the Configuration section of the manual http://logging.apache.org/log4j/1.2/manual.html

Or just change your code to:

public class LoggingDemo {
    private static final Logger logger = LoggerFactory.getLogger(LoggingDemo.class);

    public static void main(String[] args) {
        BasicConfigurator.configure();
        logger.info("Starting App....");
        System.out.println("Testing log4j.......");
        logger.info("Terminating App!");
    }

}

Upvotes: 1

Related Questions