public static void assertMessagesLogged(Collection<String> expectedPatternsRO, Collection<String> unexpectedPatterns, String filterPattern, boolean verbose) { assertLogMessages(expectedPatternsRO, unexpectedPatterns, null, null, filterPattern, verbose); }
public MethodExecutionTrace(Properties log4jProperties) { MemoryAppender.initialize(); if (log4jProperties != null) { PropertyConfigurator.configure(log4jProperties); } }
@Override public void failed(Throwable e, Description description) { if (e instanceof AssumptionViolatedException) { System.out.println(format("%s skipped programmatically with reason: %s", getFullMethodName(description) , e.getMessage())); } else { System.out.println(getFullMethodName(description) + " FAILED with exception " + e + getDuration()); e.printStackTrace(System.out); System.out.println("=================== BEGIN STORED LOG MESSAGES ==========================="); MemoryAppender.printAllMessages(); System.out.println("=================== END STORED LOG MESSAGES ==========================="); } MemoryAppender.initialize(); }
private void injectGatewayName(LoggingEvent event) { if (!injectGatewayName(event, "renderedMessage")) { injectGatewayName(event, "message"); } }
@Override public void close() { debug("MemoryAppender instance " + this + " closed"); }
public static void printAllMessages() { if (lastInstance == null) { System.out.println("Unable to print out trace level root logger messages - please " + "configure MemoryAppender on the <root> logger in log4j - config.xml"); } else { System.out.println(String.format("Printing last %d of %d log messages", eventsList.size(), messageCount.get())); lastInstance.appendAll(); } }
@Override public void evaluate() throws Throwable { base.evaluate(); long initialTime = System.currentTimeMillis(); do { try { MemoryAppender.assertMessagesLogged(expectedPatterns, forbiddenPatterns, filterPattern, true); return; } catch (AssertionError e) { if (initialTime + timeOutMillis < System.currentTimeMillis()) { throw e; } Thread.sleep(SLEEP_TIME_MILLIS); } } while (true); } };
unexpectedExceptions = unexpectedExceptions == null ? Collections.emptyList() : unexpectedExceptions; for (LoggingEvent event : MemoryAppender.getEvents()) { String message = event.getMessage().toString(); if (filterPattern == null || message.matches(filterPattern)) {
@Override protected void subAppend(LoggingEvent event) { if (printNow) { super.subAppend(event); } else { // set name of current thread on the event so it's correct when/if we print the message later event.getThreadName(); if (gatewayName != null) { injectGatewayName(event); } eventsList.add(event); // To avoid OOM, limit number of cached messages if (messageCount.incrementAndGet() > MAX_MESSAGES) { eventsList.poll(); // remove oldest message } } }
@Override public void succeeded(Description description) { System.out.println(getFullMethodName(description) + " success" + getDuration()); MemoryAppender.initialize(); }
/** * Use this constructor to set up a particular log4j configuration using a properties file * available on the classpath, or null if you do not want to load any log4j configuration * properties. * @param log4jPropertiesResourceName */ public MethodExecutionTrace(String log4jPropertiesResourceName) { healthCheck(); MemoryAppender.initialize(); if (log4jPropertiesResourceName != null) { // Initialize log4j using a properties file available on the class path Properties log4j = new Properties(); try ( InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(log4jPropertiesResourceName) ) { if (in == null) { throw new RuntimeException(String.format("Could not load resource %s", log4jPropertiesResourceName)); } log4j.load(in); PropertyConfigurator.configure(log4j); } catch (IOException e) { throw new RuntimeException(String.format("Could not load resource %s", log4jPropertiesResourceName), e); } } }