use of org.apache.log4j.AppenderSkeleton in project hadoop by apache.
the class TestAdHocLogDumper method testDumpingSchedulerLogs.
@Test
public void testDumpingSchedulerLogs() throws Exception {
Map<Appender, Priority> levels = new HashMap<>();
String logHierarchy = TestAdHocLogDumper.class.getName();
String logFilename = "test.log";
Log log = LogFactory.getLog(logHierarchy);
if (log instanceof Log4JLogger) {
for (Enumeration appenders = Logger.getRootLogger().getAllAppenders(); appenders.hasMoreElements(); ) {
Object obj = appenders.nextElement();
if (obj instanceof AppenderSkeleton) {
AppenderSkeleton appender = (AppenderSkeleton) obj;
levels.put(appender, appender.getThreshold());
}
}
}
AdHocLogDumper dumper = new AdHocLogDumper(logHierarchy, logFilename);
dumper.dumpLogs("DEBUG", 1000);
LOG.debug("test message 1");
LOG.info("test message 2");
File logFile = new File(logFilename);
Assert.assertTrue(logFile.exists());
Thread.sleep(2000);
long lastWrite = logFile.lastModified();
Assert.assertTrue(lastWrite < Time.now());
Assert.assertTrue(logFile.length() != 0);
// make sure levels are set back to their original values
if (log instanceof Log4JLogger) {
for (Enumeration appenders = Logger.getRootLogger().getAllAppenders(); appenders.hasMoreElements(); ) {
Object obj = appenders.nextElement();
if (obj instanceof AppenderSkeleton) {
AppenderSkeleton appender = (AppenderSkeleton) obj;
Assert.assertEquals(levels.get(appender), appender.getThreshold());
}
}
}
boolean del = logFile.delete();
if (!del) {
LOG.info("Couldn't clean up after test");
}
}
use of org.apache.log4j.AppenderSkeleton in project commons by twitter.
the class JULBridgeLogManagerTest method checkAssimilateTakesOver.
@Test
public void checkAssimilateTakesOver() {
// Create a test log4j environment
final List<LoggingEvent> events = new LinkedList<LoggingEvent>();
org.apache.log4j.Logger log4jRoot = new RootLogger(org.apache.log4j.Level.ALL);
LoggerRepository loggerRepository = new Hierarchy(log4jRoot);
loggerRepository.setThreshold(org.apache.log4j.Level.INFO);
log4jRoot.addAppender(new AppenderSkeleton() {
@Override
public boolean requiresLayout() {
return false;
}
@Override
public void close() {
}
@Override
protected void append(LoggingEvent event) {
events.add(event);
}
});
JULBridgeLogManager.assimilate(loggerRepository);
Logger.getLogger("test.1").log(Level.INFO, "test message 1");
Logger.getLogger("test.2").log(Level.FINE, "test message 2");
Logger.getLogger("test.3").log(Level.WARNING, "test message 3");
assertThat(events.size(), is(2));
assertThat(events.get(0).getLoggerName(), is("test.1"));
assertThat(events.get(0).getMessage(), is((Object) "test message 1"));
assertThat(events.get(1).getLoggerName(), is("test.3"));
assertThat(events.get(1).getMessage(), is((Object) "test message 3"));
}
use of org.apache.log4j.AppenderSkeleton in project h2o-3 by h2oai.
the class CustomHttpFilterTest method testNoLog.
@Test
public void testNoLog() throws Exception {
final Logger _logger = Logger.getLogger("water.default");
_logger.addAppender(new AppenderSkeleton() {
@Override
protected void append(LoggingEvent event) {
if (event.getRenderedMessage().contains("GET"))
throw new RuntimeException("All GETs should be filtered");
}
@Override
public void close() {
_logger.removeAppender(this);
}
@Override
public boolean requiresLayout() {
return false;
}
});
// let's filter out all GETs
RequestServer.setFilters(RequestServer.defaultFilter(), new RequestServer.HttpLogFilter() {
@Override
public boolean filter(RequestUri uri, Properties header, Properties parms) {
String[] path = uri.getPath();
return path[1].equals("GET");
}
});
// mock up a "GET /flow/index.html" call
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
when(request.getServletPath()).thenReturn("/flow/index.html");
when(request.getRequestURI()).thenReturn("/flow/index.html");
// mock up the headers
// define the headers you want to be returned
HashMap<String, String> headers = new HashMap<>();
headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36");
headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
headers.put("Cookie", "_yummy");
headers.put("Accept-Encoding", "gzip, deflate, sdch");
headers.put("Host", H2O.getIpPortString());
headers.put("Upgrade-Insecure-Requests", "1");
headers.put("Accept-Language", "en-US,en;q=0.8");
headers.put("Connection", "keep-alive");
// create an Enumeration over the header keys
final Iterator<String> iterator = headers.keySet().iterator();
Enumeration headerNames = new Enumeration<String>() {
@Override
public boolean hasMoreElements() {
return iterator.hasNext();
}
@Override
public String nextElement() {
return iterator.next();
}
};
when(request.getHeaderNames()).thenReturn(headerNames);
when(request.getHeader("User-Agent")).thenReturn(headers.get("User-Agent"));
when(request.getHeader("Accept")).thenReturn(headers.get("Accept"));
when(request.getHeader("Cookie")).thenReturn(headers.get("Cookie"));
when(request.getHeader("Accept-Encoding")).thenReturn(headers.get("Accept-Encoding"));
when(request.getHeader("Host")).thenReturn(headers.get("Host"));
when(request.getHeader("Upgrade-Insecure-Requests")).thenReturn(headers.get("Upgrade-Insecure-Requests"));
when(request.getHeader("Accept-Language")).thenReturn(headers.get("Accept-Language"));
when(request.getHeader("Connection")).thenReturn(headers.get("Connection"));
when(request.getParameterMap()).thenReturn(new HashMap<String, String[]>());
when(response.getOutputStream()).thenReturn(new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
}
});
// start the request lifecycle
H2O.getJetty().getServer().getChildHandlersByClass(JettyHTTPD.GateHandler.class)[0].handle("/", null, request, response);
new RequestServer().doGet(request, response);
}
use of org.apache.log4j.AppenderSkeleton in project intellij-plugins by JetBrains.
the class LightTestCase method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
ourShouldFail = false;
Logger.getRootLogger().addAppender(new AppenderSkeleton() {
@Override
protected void append(LoggingEvent loggingEvent) {
if (loggingEvent.level.isGreaterOrEqual(Priority.ERROR)) {
ourShouldFail = true;
}
}
@Override
public boolean requiresLayout() {
return false;
}
@Override
public void close() {
}
});
}
Aggregations