1

I want to add log4j (for consol) to my tests in project (I use hibernate and spring). I added log4j.properties in test/resources:

log4j.rootLogger=INFO, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

and write in test class:

import org.apache.log4j.Logger;
private Logger logger = Logger.getLogger(MyTest.class);

    @Test
    public void test1() {
        logger.info("info test");
        logger.debug("debug test");
    }

gradle:

testCompile 'log4j:log4j:1.2.17'

But I have not this text ("info test") in console, I have in console:

13:01:24.626 [main] DEBUG o.h.p.w.spi.MetamodelGraphWalker - Visiting attribute path : maxKeyNumber
13:01:24.626 [main] DEBUG o.h.p.w.spi.MetamodelGraphWalker - Visiting attribute path : updated
13:01:24.626 [main] DEBUG o.h.p.w.spi.MetamodelGraphWalker - Visiting attribute path : credential
13:01:24.626 [main] DEBUG o.h.p.w.spi.MetamodelGraphWalker - Visiting attribute path : aid
13:01:24.626 [main] DEBUG o.h.p.w.spi.MetamodelGraphWalker - Visiting attribute path : cvmConfig
13:01:24.626 [main] DEBUG o.h.p.w.spi.MetamodelGraphWalker - Visiting attribute path : dki
13:01:24.626 [main] DEBUG o.h.p.w.spi.MetamodelGraphWalker - Visiting attribute path : isMsdSupport
13:01:24.627 [main] DEBUG o.h.p.w.spi.MetamodelGraphWalker - Visiting attribute path : serviceCode
Paushchyk Julia
  • 516
  • 3
  • 8
  • 24

3 Answers3

0

Can you verify log4j.properties resource is in the classpath and is found at runtime? Usually if you add test/resources folder to the source path in eclipse project, the bin folder should have it (the output folder name can be different, but this file must be on the classpath).

  • Maybe you prompt me - how can to verify log4j.properties resource is in the classpath and is found at runtime? I use intellij idea and I set in structure project test/resource folder as resource. – Paushchyk Julia Jun 24 '15 at 10:24
  • A way to check build path is [here](http://stackoverflow.com/questions/3765998/add-a-properties-file-to-intellijs-classpath). Also search your entire project on disk for log4j.properties. You must find two of them... one in test/resources folder and see where the other one is. If you find only 1 under test/resources, you don't have it on classpath. Now, find where *.class files are going. That is your classpath. – Akshay Mahajan Jun 24 '15 at 10:55
0

Added log4j.xml file in src folder and change the file location below at place of xyz.log<!-- FILE Appender --> <appender name="FILE" class="org.apache.log4j.FileAppender"> <param name="File" value="D:\\xyz.log" /> <param name="MaxFileSize" value="1MB" /> <param name="MaxBackupIndex" value="100" /> <param name="Append" value="false" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %5p %c{2}: %m%n" /> </layout> </appender.

>

0

You need to add console appender to log4j. I have done it with log4j.xml, not with the property file. I hope you can transform this to your log4j.properties file.

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d - %m%n"/>
    </layout>
</appender>

And Finally you need to add this appender to root logger.

    <root>
    <priority value="debug"/>
    <appender-ref ref="error"/>
    <appender-ref ref="console"/>
    <appender-ref ref="trace"/>
</root>

Hope this helps

Nithya
  • 136
  • 6