Search in sources :

Example 1 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)

Example 2 with SimpleFormatter

use of java.util.logging.SimpleFormatter in project android_frameworks_base by ParanoidAndroid.

the class CookiesTest method testCookiesAreNotLogged.

/**
     * Test that we don't log potentially sensitive cookie values.
     * http://b/3095990
     */
public void testCookiesAreNotLogged() throws IOException, URISyntaxException {
    // enqueue an HTTP response with a cookie that will be rejected
    server.enqueue(new MockResponse().addHeader("Set-Cookie: password=secret; Domain=fake.domain"));
    server.play();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Logger logger = Logger.getLogger("org.apache.http");
    StreamHandler handler = new StreamHandler(out, new SimpleFormatter());
    logger.addHandler(handler);
    try {
        HttpClient client = new DefaultHttpClient();
        client.execute(new HttpGet(server.getUrl("/").toURI()));
        handler.close();
        String log = out.toString("UTF-8");
        assertTrue(log, log.contains("password"));
        assertTrue(log, log.contains("fake.domain"));
        assertFalse(log, log.contains("secret"));
    } finally {
        logger.removeHandler(handler);
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) StreamHandler(java.util.logging.StreamHandler) SimpleFormatter(java.util.logging.SimpleFormatter) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(java.util.logging.Logger) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 3 with SimpleFormatter

use of java.util.logging.SimpleFormatter in project android_frameworks_base by ResurrectionRemix.

the class CookiesTest method testCookiesAreNotLogged.

/**
     * Test that we don't log potentially sensitive cookie values.
     * http://b/3095990
     */
public void testCookiesAreNotLogged() throws IOException, URISyntaxException {
    // enqueue an HTTP response with a cookie that will be rejected
    server.enqueue(new MockResponse().addHeader("Set-Cookie: password=secret; Domain=fake.domain"));
    server.play();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Logger logger = Logger.getLogger("org.apache.http");
    StreamHandler handler = new StreamHandler(out, new SimpleFormatter());
    logger.addHandler(handler);
    try {
        HttpClient client = new DefaultHttpClient();
        client.execute(new HttpGet(server.getUrl("/").toURI()));
        handler.close();
        String log = out.toString("UTF-8");
        assertTrue(log, log.contains("password"));
        assertTrue(log, log.contains("fake.domain"));
        assertFalse(log, log.contains("secret"));
    } finally {
        logger.removeHandler(handler);
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) StreamHandler(java.util.logging.StreamHandler) SimpleFormatter(java.util.logging.SimpleFormatter) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(java.util.logging.Logger) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 4 with SimpleFormatter

use of java.util.logging.SimpleFormatter in project jdk8u_jdk by JetBrains.

the class SerializeLogRecord method generate.

/**
     * Serializes a log record, encode the serialized bytes in base 64, and
     * prints pseudo java code that can be cut and pasted into this test.
     * @param record the log record to serialize, encode in base 64, and for
     *               which test data will be generated.
     * @return A string containing the generated pseudo java code.
     * @throws IOException Unexpected.
     * @throws ClassNotFoundException  Unexpected.
     */
public static String generate(LogRecord record) throws IOException, ClassNotFoundException {
    // Format the given logRecord using the SimpleFormatter
    SimpleFormatter formatter = new SimpleFormatter();
    String str = formatter.format(record);
    // Serialize the given LogRecord
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(record);
    oos.flush();
    oos.close();
    // Now we're going to perform a number of smoke tests before
    // generating the Java pseudo code.
    //
    // First checks that the log record can be deserialized
    final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    final ObjectInputStream ois = new ObjectInputStream(bais);
    final LogRecord record2 = (LogRecord) ois.readObject();
    // Format the deserialized LogRecord using the SimpleFormatter, and
    // check that the string representation obtained matches the string
    // representation of the original LogRecord
    String str2 = formatter.format(record2);
    if (!str.equals(str2))
        throw new RuntimeException("Unexpected values in deserialized object:" + "\n\tExpected:  " + str + "\n\tRetrieved: " + str);
    // Now get a Base64 string representation of the serialized bytes.
    final String base64 = Base64.getEncoder().encodeToString(baos.toByteArray());
    // Check that we can deserialize a log record from the Base64 string
    // representation we just computed.
    final ByteArrayInputStream bais2 = new ByteArrayInputStream(Base64.getDecoder().decode(base64));
    final ObjectInputStream ois2 = new ObjectInputStream(bais2);
    final LogRecord record3 = (LogRecord) ois2.readObject();
    // Format the new deserialized LogRecord using the SimpleFormatter, and
    // check that the string representation obtained matches the string
    // representation of the original LogRecord
    String str3 = formatter.format(record3);
    if (!str.equals(str3))
        throw new RuntimeException("Unexpected values in deserialized object:" + "\n\tExpected:  " + str + "\n\tRetrieved: " + str);
    //System.out.println(base64);
    //System.out.println();
    // Generates the Java Pseudo code that can be cut & pasted into
    // this test (see Jdk8SerializedLog and Jdk9SerializedLog below)
    final StringBuilder sb = new StringBuilder();
    sb.append("    /**").append('\n');
    sb.append("     * Base64 encoded string for LogRecord object.").append('\n');
    sb.append("     * Java version: ").append(System.getProperty("java.version")).append('\n');
    sb.append("     **/").append('\n');
    sb.append("    final String base64 = ").append("\n          ");
    final int last = base64.length() - 1;
    for (int i = 0; i < base64.length(); i++) {
        if (i % 64 == 0)
            sb.append("\"");
        sb.append(base64.charAt(i));
        if (i % 64 == 63 || i == last) {
            sb.append("\"");
            if (i == last)
                sb.append(";\n");
            else
                sb.append("\n        + ");
        }
    }
    sb.append('\n');
    sb.append("    /**").append('\n');
    sb.append("     * SimpleFormatter output for LogRecord object.").append('\n');
    sb.append("     * Java version: ").append(System.getProperty("java.version")).append('\n');
    sb.append("     **/").append('\n');
    sb.append("    final String str = ").append("\n          ");
    sb.append("\"").append(str.replace("\n", "\\n")).append("\";\n");
    return sb.toString();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) LogRecord(java.util.logging.LogRecord) SimpleFormatter(java.util.logging.SimpleFormatter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 5 with SimpleFormatter

use of java.util.logging.SimpleFormatter in project platform_frameworks_base by android.

the class CookiesTest method testCookiesAreNotLogged.

/**
     * Test that we don't log potentially sensitive cookie values.
     * http://b/3095990
     */
public void testCookiesAreNotLogged() throws IOException, URISyntaxException {
    // enqueue an HTTP response with a cookie that will be rejected
    server.enqueue(new MockResponse().addHeader("Set-Cookie: password=secret; Domain=fake.domain"));
    server.play();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Logger logger = Logger.getLogger("org.apache.http");
    StreamHandler handler = new StreamHandler(out, new SimpleFormatter());
    logger.addHandler(handler);
    try {
        HttpClient client = new DefaultHttpClient();
        client.execute(new HttpGet(server.getUrl("/").toURI()));
        handler.close();
        String log = out.toString("UTF-8");
        assertTrue(log, log.contains("password"));
        assertTrue(log, log.contains("fake.domain"));
        assertFalse(log, log.contains("secret"));
    } finally {
        logger.removeHandler(handler);
    }
}
Also used : MockResponse(com.google.mockwebserver.MockResponse) StreamHandler(java.util.logging.StreamHandler) SimpleFormatter(java.util.logging.SimpleFormatter) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Logger(java.util.logging.Logger) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Aggregations

SimpleFormatter (java.util.logging.SimpleFormatter)99 FileHandler (java.util.logging.FileHandler)58 Logger (java.util.logging.Logger)36 File (java.io.File)28 Handler (java.util.logging.Handler)23 IOException (java.io.IOException)17 StreamHandler (java.util.logging.StreamHandler)16 Properties (java.util.Properties)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 LogRecord (java.util.logging.LogRecord)13 BalancerRunner (org.mobicents.tools.sip.balancer.BalancerRunner)13 Config (edu.neu.ccs.pyramid.configuration.Config)11 ConsoleHandler (java.util.logging.ConsoleHandler)10 Formatter (java.util.logging.Formatter)9 Test (org.junit.Test)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 Pair (edu.neu.ccs.pyramid.util.Pair)3