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());
}
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);
}
}
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);
}
}
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 {
}
});
}
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"));
}
Aggregations