Search log4j.properties in IDEA

Update - this problem was my own work.

At one point, this particular test class had a test to ensure that something was registered. In setup, I previously uninstalled all applications and added my own appender to accept time test statements. This test has long gone, but this nugget has remained in the configuration: 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 in this way:

 "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 known issue that many beginners have seen over and over again. Today I feel a little stupid. [/ P>

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 without problems.

File contents:

 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 
+6
source share
1 answer

I am using log4j with slf4j in IntelliJ IDEA and it works fine for me. Just enable these banks depending on your application in IntelliJ:

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

Instead of putting log4j configuration in your application. But DO NOT forget to mark this location of log4j.properties in IntelliJ as Sources, or if you use it in 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 won't see any warnings like: No appenders could be found for logger .

Hope this helps.

+6
source

Source: https://habr.com/ru/post/916809/


All Articles