简体   繁体   中英

Java, Spring Tests, how do I configure logging normally defined in web.xml?

I have the following defined for my test:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("/App/webapp")
@ContextConfiguration({
        "classpath:/config/AConfig.xml",
        "classpath:/config/BConfig.xml",
        "classpath:/config/CConfig.xml",
        "classpath:/config/DConfig.xml"
})
public class MyTests{
    ...
} 

In web.xml I use the same configuration plus additional configuration, for instance to configure filters and listeners. How can I enable these when doing tests?

It would be great if Spring just used the same web.xml but I guess that might not be an option.

I have this defined in web.xml:

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:/config/log4j.properties</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

Is there a way to configure logging from aa contextConfig.xml ( bean definition ) instead?

http://codebyexample.info/2012/03/31/how-log4j-and-junit-became-friends/

that may be helpful, in particular the part where the author does:

Logger.getRootLogger().addAppender(someAppender)

You should be able to access Logger.getRootLogger() and configure it there. If you want to use the properties file, you can do:

PropertyConfigurator.configure("/config/log4j.properties");

How to configure log4j.properties for SpringJUnit4ClassRunner?

or manually configure it like:

Logger rootLogger = Logger.getRootLogger();
rootLogger.setLevel(Level.INFO);
rootLogger.addAppender(new ConsoleAppender(
       new PatternLayout("%-6r [%p] %c - %m%n")));

These configurations should be done once before all classes are run, though I don't think they harm too much if you do them in a @Before method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM