Search in sources :

Example 6 with SimpleFormatter

use of java.util.logging.SimpleFormatter in project j2objc by google.

the class ConsoleHandlerTest method testConstructor_InvalidProperties.

/*
	 * Test the constructor with invalid relevant log manager properties are
	 * set.
	 */
public void testConstructor_InvalidProperties() throws Exception {
    Properties p = new Properties();
    p.put("java.util.logging.ConsoleHandler.level", INVALID_LEVEL);
    p.put("java.util.logging.ConsoleHandler.filter", className);
    p.put("java.util.logging.ConsoleHandler.formatter", className);
    p.put("java.util.logging.ConsoleHandler.encoding", "XXXX");
    LogManager.getLogManager().readConfiguration(EnvironmentHelper.PropertiesToInputStream(p));
    assertEquals(LogManager.getLogManager().getProperty("java.util.logging.ConsoleHandler.level"), INVALID_LEVEL);
    assertEquals(LogManager.getLogManager().getProperty("java.util.logging.ConsoleHandler.encoding"), "XXXX");
    ConsoleHandler h = new ConsoleHandler();
    assertSame(h.getLevel(), Level.INFO);
    assertTrue(h.getFormatter() instanceof SimpleFormatter);
    assertNull(h.getFilter());
    assertNull(h.getEncoding());
    h.publish(new LogRecord(Level.SEVERE, "test"));
    assertNull(h.getEncoding());
}
Also used : LogRecord(java.util.logging.LogRecord) SimpleFormatter(java.util.logging.SimpleFormatter) Properties(java.util.Properties) ConsoleHandler(java.util.logging.ConsoleHandler)

Example 7 with SimpleFormatter

use of java.util.logging.SimpleFormatter in project okhttp by square.

the class CallTest method asyncLeakedResponseBodyLogsStackTrace.

@Test
public void asyncLeakedResponseBodyLogsStackTrace() throws Exception {
    server.enqueue(new MockResponse().setBody("This gets leaked."));
    client = defaultClient().newBuilder().connectionPool(new ConnectionPool(0, 10, TimeUnit.MILLISECONDS)).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    Level original = logger.getLevel();
    logger.setLevel(Level.FINE);
    logHandler.setFormatter(new SimpleFormatter());
    try {
        final CountDownLatch latch = new CountDownLatch(1);
        client.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                fail();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                // Ignore the response so it gets leaked then GC'd.
                latch.countDown();
            }
        });
        latch.await();
        // There's some flakiness when triggering a GC for objects in a separate thread. Adding a
        // small delay appears to ensure the objects will get GC'd.
        Thread.sleep(200);
        awaitGarbageCollection();
        String message = logHandler.take();
        assertTrue(message.contains("A connection to " + server.url("/") + " was leaked." + " Did you forget to close a response body?"));
        assertTrue(message.contains("okhttp3.RealCall.enqueue("));
        assertTrue(message.contains("okhttp3.CallTest.asyncLeakedResponseBodyLogsStackTrace("));
    } finally {
        logger.setLevel(original);
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SimpleFormatter(java.util.logging.SimpleFormatter) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) MockResponse(okhttp3.mockwebserver.MockResponse) Level(java.util.logging.Level) Test(org.junit.Test)

Example 8 with SimpleFormatter

use of java.util.logging.SimpleFormatter in project okhttp by square.

the class CallTest method leakedResponseBodyLogsStackTrace.

@Test
public void leakedResponseBodyLogsStackTrace() throws Exception {
    server.enqueue(new MockResponse().setBody("This gets leaked."));
    client = defaultClient().newBuilder().connectionPool(new ConnectionPool(0, 10, TimeUnit.MILLISECONDS)).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    Level original = logger.getLevel();
    logger.setLevel(Level.FINE);
    logHandler.setFormatter(new SimpleFormatter());
    try {
        // Ignore the response so it gets leaked then GC'd.
        client.newCall(request).execute();
        awaitGarbageCollection();
        String message = logHandler.take();
        assertTrue(message.contains("A connection to " + server.url("/") + " was leaked." + " Did you forget to close a response body?"));
        assertTrue(message.contains("okhttp3.RealCall.execute("));
        assertTrue(message.contains("okhttp3.CallTest.leakedResponseBodyLogsStackTrace("));
    } finally {
        logger.setLevel(original);
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) SimpleFormatter(java.util.logging.SimpleFormatter) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) Level(java.util.logging.Level) Test(org.junit.Test)

Example 9 with SimpleFormatter

use of java.util.logging.SimpleFormatter in project randomizedtesting by randomizedtesting.

the class WithNestedTestClass method setupNested.

@BeforeClass
public static final void setupNested() throws IOException {
    runningNested = true;
    zombieToken = new Object();
    // capture sysout/ syserr.
    sw = new StringWriter();
    sysout = System.out;
    syserr = System.err;
    System.setOut(new PrintStream(new TeeOutputStream(System.out, new WriterOutputStream(sw))));
    System.setErr(new PrintStream(new TeeOutputStream(System.err, new WriterOutputStream(sw))));
    // Add custom logging handler because java logging keeps a reference to previous System.err.
    loggingMessages = new StringWriter();
    logger = Logger.getLogger("");
    handlers = logger.getHandlers();
    for (Handler h : handlers) logger.removeHandler(h);
    logger.addHandler(new Handler() {

        final SimpleFormatter formatter = new SimpleFormatter();

        @Override
        public void publish(LogRecord record) {
            loggingMessages.write(formatter.format(record) + "\n");
        }

        @Override
        public void flush() {
        }

        @Override
        public void close() throws SecurityException {
        }
    });
}
Also used : PrintStream(java.io.PrintStream) StringWriter(java.io.StringWriter) LogRecord(java.util.logging.LogRecord) SimpleFormatter(java.util.logging.SimpleFormatter) Handler(java.util.logging.Handler) BeforeClass(org.junit.BeforeClass)

Example 10 with SimpleFormatter

use of java.util.logging.SimpleFormatter in project jersey by jersey.

the class ResourceBundleTest method testBadResource.

@Test
public void testBadResource() throws Exception {
    final ResourceConfig resourceConfig = new ResourceConfig(BadResource.class);
    ByteArrayOutputStream logOutput = new ByteArrayOutputStream();
    Handler logHandler = new StreamHandler(logOutput, new SimpleFormatter());
    GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig, false);
    // TODO: there should be a better way to get the log output!
    final Enumeration<String> loggerNames = LogManager.getLogManager().getLoggerNames();
    while (loggerNames.hasMoreElements()) {
        String name = loggerNames.nextElement();
        if (name.startsWith("org.glassfish")) {
            LogManager.getLogManager().getLogger(Errors.class.getName()).addHandler(logHandler);
        }
    }
    GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig, false);
    logOutput.flush();
    final String logOutputAsString = logOutput.toString();
    Assert.assertFalse(logOutputAsString.contains("[failed to localize]"));
    Assert.assertTrue(logOutputAsString.contains("BadResource"));
}
Also used : StreamHandler(java.util.logging.StreamHandler) SimpleFormatter(java.util.logging.SimpleFormatter) StreamHandler(java.util.logging.StreamHandler) Handler(java.util.logging.Handler) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

SimpleFormatter (java.util.logging.SimpleFormatter)42 StreamHandler (java.util.logging.StreamHandler)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 Logger (java.util.logging.Logger)13 FileHandler (java.util.logging.FileHandler)11 Handler (java.util.logging.Handler)8 LogRecord (java.util.logging.LogRecord)8 IOException (java.io.IOException)7 MockResponse (com.google.mockwebserver.MockResponse)6 HttpClient (org.apache.http.client.HttpClient)6 HttpGet (org.apache.http.client.methods.HttpGet)6 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)6 ConsoleHandler (java.util.logging.ConsoleHandler)4 File (java.io.File)3 Level (java.util.logging.Level)3 Test (org.junit.Test)3 Config (edu.neu.ccs.pyramid.configuration.Config)2 Properties (java.util.Properties)2 Formatter (java.util.logging.Formatter)2 MemoryHandler (java.util.logging.MemoryHandler)2