Finding log4j.properties in IDEA

intellij-idea, log4j

Solution

I am using log4j with slf4j in IntelliJ IDEA and it works perfectly for me. Just include these jars into your application dependencies in IntelliJ:

log4j-1.2.9.jar
slf4j-api-1.5.11.jar
slf4j-log4j12-1.5.11.jar

Than put somewhere in your app the log4j configuration. But DO NOT forget to mark that location of the log4j.properties in IntelliJ as Sources or if you using it on tests as Test Sources:

log4j.properties:

log4j.rootLogger=INFO,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %X{file} %c{1} - %m%n

log4j.logger.your.app=DEBUG,yourApp
log4j.additivity.your.app=false
log4j.logger.yourApp=DEBUG,yourApp
log4j.additivity.yourApp=false
log4j.appender.yourApp=org.apache.log4j.ConsoleAppender
log4j.appender.yourApp.layout=org.apache.log4j.PatternLayout
log4j.appender.yourApp.layout.ConversionPattern=%d [%t] %-5p %X{file} %c{1} %m%n
log4j.appender.yourApp.ImmediateFlush=true

Then in your java class use this to get the logger:

private static final Logger LOGGER = LoggerFactory.getLogger(YourApp.class);

And you will not see any of warnings like: `No appenders could be found for logger`.

Hope this helps.

Problem

Update - this problem was of my own doing. At one stage this particular test class had a test to ensure that something was logged. In the setup, I had previously removed all appenders and added my own appender for making test-time assertions. That test is long since gone, but this nugget remained in the setup: `Logger.getRootLogger().removeAllAppenders();`. Sorry for the false alarm. :) In IDEA I have the following test: ``` @Test public void shouldLog() { URL resource = Thread.currentThread().getContextClassLoader() .getResource("log4j.properties"); System.out.println("resource = " + resource); final Logger logger = Logger.getLogger(getClass()); logger.info("Hello world"); } ``` It outputs thusly: ``` "C:\Program Files\Java\jdk1.5.0_18\bin\java" -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 11.1\lib\idea_rt.jar" -ea -Dfile.encoding=UTF-8 com.intellij.rt.execution.CommandLineWrapper C:\DOCUME~1\JMAWSO~1.NT3\LOCALS~1\Temp\classpath2294205410661538428.tmp @vm_params C:\DOCUME~1\JMAWSO~1.NT3\LOCALS~1\Temp\vm_params5645362020129462784.tmp com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 au.com.gmm.repo.RepoThreadCleanupServiceTest,shouldLog resource = file:/C:/user/jem/projects/GMM/out/test/cashflow/log4j.properties log4j:WARN No appenders could be found for logger (au.com.gmm.repo.RepoThreadCleanupServiceTest). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Process finished with exit code 0 ``` This is a famous problem, seen by many newbies over and over again. I feel a little silly being stumped by it today. http://logging.apache.org/log4j/1.2/faq.html#noconfig says `log4j uses Thread.getContextClassLoader().getResource() to locate the default configuration files`. However, my test checks `Thread.currentThread().getContextClassLoader().getResource("log4j.properties")` and finds the properties file with no problem. The content of the file is: ``` log4j.rootLogger=DEBUG, CONSOLE log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss.SSS} %c - %m%n ```

Original source