Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding log4j.properties in IDEA

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
like image 427
Synesso Avatar asked May 28 '12 02:05

Synesso


People also ask

Where can I find log4j properties file?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file.

How add log4j properties file in classpath in IntelliJ?

Go to Project Structure | Modules | Your Module | Dependencies, click Add, Single-Entry Module Library, specify the path to the "resources" folder. Yet another solution would be to put the log4j. properties file directly under the Source root of your project (in the default package directory).

Does IntelliJ use log4j?

The IntelliJ Platform ships with an implementation of the SLF4J APIs over java. util. logging, so SLF4J logging is fully supported in the platform.


1 Answers

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.

like image 134
Paulius Matulionis Avatar answered Oct 29 '22 01:10

Paulius Matulionis