Set Up Log4j (Log for Java) in Intellij IDEA

Use Log4J to filter how much logging your IDE displays. Tap into logging already in code libraries you are using. And shunt output to a log file you can use for other purposes, such as CI reports, performance monitoring etc. Stop using System.out.print!

  1. In IDEA open project settings, click Libraries on Left pane, find Log4J listed in right pane. I use Maven to manage library retrieval and it was listed as Maven: log4j:log4j:signed:1.2.16
  2. Right-click Log4J and select add to modules.
  3. Click the project you will use Log4J in from the list that displays.
  4. In Project tree window find the project you added Log4J to, and open to main/resources. Create this tree if necessary. Create new file log4j.properties.
  5. Paste this in log4j.properties:
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=INFO, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Try it out:

Add this to a Java Class you want to log from:

  private final Logger logger = Logger.getLogger(<>.class);

This should prompt IDEA to ask you if you want to import Logger, which of course, you do. Also replace ClassName with the name of the class you just put your declaration in.

To test this add the following to your main function:

    logger.info("Start of setUp");

You may also like...

3 Responses

  1. JoseJimenez says:

    Nice! Thanks for the info.

  2. vasantha says:

    It worked Thx!!

  3. Alan says:

    Thanks! Worked like a charm!

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha loading...

This site uses Akismet to reduce spam. Learn how your comment data is processed.