spring-junit中使用 log4j或者logback 列印spring日誌
阿新 • • 發佈:2019-01-28
package com.gooddeep.elasticsearch; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.collections.map.HashedMap; import org.apache.log4j.PropertyConfigurator; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.joran.JoranConfigurator; import ch.qos.logback.core.joran.spi.JoranException; import com.gooddeep.redis.RankIng; import com.gooddeep.redis.RankIngDao; /** * 單元測試 * @author lhy * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:config/spring/spring-elasticsearch.xml"}) public class EsTest { static{ LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory(); JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(lc); lc.reset(); try { configurator.doConfigure("/home/lhy/Workspaces/MyEclipse_data/gooddeep/src/main/java/config/logback.xml");//載入logback配置檔案 } catch (JoranException e) { e.printStackTrace(); } //PropertyConfigurator.configure("/home/lhy/Workspaces/MyEclipse_data/gooddeep/src/main/java/config/log4j.properties");//載入logj配置檔案 } @Autowired private AutoReplyService autoReplyService; Logger logger=LoggerFactory.getLogger(EsTest.class); @Test public void test() throws InterruptedException{ logger.debug("###################************************8#######################"); try{ /* AutoReply autoReply=new AutoReply(); autoReply.setKeyword("三星中蝙蝠俠特別版"); autoReply.setReply("你好,笨蛋啊啊啊啊啊啊啊啊啊啊"); autoReply.setUserKey("123456"); autoReplyService.insertOrUpdate(autoReply);*/ Map map=new HashMap(); map.put("keyword", "三星中蝙蝠俠特別版"); // String a[]=new Strin{"keyword"}; List<AutoReply>list=autoReplyService.queryList(map, new String[]{"keyword"}, null, null, null); for(AutoReply autoReply:list) { System.out.println(autoReply.toString()); } }catch(Exception e) { e.printStackTrace(); } //System.out.println(rankIngDao.get("rankIng:df573ca9b2b7497eacd8c75bb9f4b95e").toString()); } }