How to configure log4j.properties for SpringJUnit4ClassRunner?

后端 未结 7 645
深忆病人
深忆病人 2021-01-30 04:06

Suddenly this keeps happening during a JUnit test. Everything was working, I wrote some new tests and this error occured. If I revert it, it won\'t go away. Why is that?

7条回答
  •  说谎
    说谎 (楼主)
    2021-01-30 04:39

    The new tests you wrote (directly or indirectly) use classes that log using Log4j.

    Log4J needs to be configured for this logging to work properly.

    Put a log4j.properties (or log4j.xml) file in the root of your test classpath.

    It should have some basic configuration such as

    # Set root logger level to DEBUG and its only appender to A1.
    log4j.rootLogger=DEBUG, 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
    
    # An alternative logging format:
    # log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n
    

    An appender outputs to the console by default, but you can also explicitly set the target like this:

    log4j.appender.A1.Target=System.out
    

    This will redirect all output in a nice format to the console. More info can be found here in the Log4J manual,

    Log4J Logging will then be properly configured and this warning will disappear.

提交回复
热议问题