Search in sources :

Example 1 with AppenderSkeleton

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");
    }
}
Also used : Appender(org.apache.log4j.Appender) AppenderSkeleton(org.apache.log4j.AppenderSkeleton) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) Log(org.apache.commons.logging.Log) Priority(org.apache.log4j.Priority) Log4JLogger(org.apache.commons.logging.impl.Log4JLogger) File(java.io.File) Test(org.junit.Test)

Example 2 with AppenderSkeleton

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"));
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) LoggerRepository(org.apache.log4j.spi.LoggerRepository) Hierarchy(org.apache.log4j.Hierarchy) AppenderSkeleton(org.apache.log4j.AppenderSkeleton) RootLogger(org.apache.log4j.spi.RootLogger) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 3 with AppenderSkeleton

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);
}
Also used : AppenderSkeleton(org.apache.log4j.AppenderSkeleton) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Logger(org.apache.log4j.Logger) Properties(java.util.Properties) LoggingEvent(org.apache.log4j.spi.LoggingEvent) HttpServletRequest(javax.servlet.http.HttpServletRequest) JettyHTTPD(water.JettyHTTPD) Test(org.junit.Test)

Example 4 with AppenderSkeleton

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() {
        }
    });
}
Also used : LoggingEvent(org.apache.log4j.spi.LoggingEvent) AppenderSkeleton(org.apache.log4j.AppenderSkeleton)

Aggregations

AppenderSkeleton (org.apache.log4j.AppenderSkeleton)4 LoggingEvent (org.apache.log4j.spi.LoggingEvent)3 Test (org.junit.Test)3 Enumeration (java.util.Enumeration)2 HashMap (java.util.HashMap)2 File (java.io.File)1 IOException (java.io.IOException)1 LinkedList (java.util.LinkedList)1 Properties (java.util.Properties)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Log (org.apache.commons.logging.Log)1 Log4JLogger (org.apache.commons.logging.impl.Log4JLogger)1 Appender (org.apache.log4j.Appender)1 Hierarchy (org.apache.log4j.Hierarchy)1 Logger (org.apache.log4j.Logger)1 Priority (org.apache.log4j.Priority)1 LoggerRepository (org.apache.log4j.spi.LoggerRepository)1 RootLogger (org.apache.log4j.spi.RootLogger)1