user3748486
user3748486

Reputation: 49

Setting Up Log4J in IntelliJ IDEA 13

I'm building a simple app in IntelliJ IDEA 13 and can't seem to figure out how to get log4j working. My app is a simple demo that I made to make sure I could build something functional, all it does is multiply two random numbers and uses apache tomcat to put it on a localhost that I can access via my browser.

Here is the class code:

package Sample;

log4j-api-2.0.jar;
log4j-core-2.0.jar;

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


public class HelloWorld {

    public static double getMessage() {
        return Math.random()* Math.random();

     }

    private static Logger log = LogManager.getRootLogger();

    log.debug("Debugging Message");
    log.info("Informational message");
    log.warn("Warning Message");
    System.in.read();

}

I'm getting the error "class or interface expected" at the import lines and jar file lines so I don't think I've placed the corresponding files in the right directory. That's also causing the rest of the logging lines (from private static Logger... on) to generate errors.

Upvotes: 3

Views: 22369

Answers (1)

Dave Newton
Dave Newton

Reputation: 160201

1. The following isn't valid Java:

log4j-api-2.0.jar;
log4j-core-2.0.jar;

You only need the import lines, e.g.,

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

2. The .jar files must be associated with your project.

You can:

  • Right-click the "External Libraries" section and add them that way, or...
  • Use Maven and add them as project dependencies, or...
  • Use some other dependency management and/or build tool, e.g., Ant + Ivy, Gradle, etc.

3. You need to move the logging statements into a place where code is valid, like in a method:

package sample;

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

public class HelloWorld {
    private static final Logger log = LogManager.getRootLogger();

    public static void main(String[] args) {
        log.debug("Debugging Message");
        log.info("Informational message");
        log.warn("Warning Message");
    }    
}

Upvotes: 5

Related Questions