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!
- 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
- Right-click Log4J and select add to modules.
- Click the project you will use Log4J in from the list that displays.
- 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.
- 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");