Search in sources :

Example 11 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 12 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 13 with SimpleFormatter

use of java.util.logging.SimpleFormatter in project azure-tools-for-java by Microsoft.

the class AzureActionsComponent method initLoggerFileHandler.

private void initLoggerFileHandler() {
    try {
        String loggerFilePath = Paths.get(CommonSettings.settingsBaseDir, "corelibs.log").toString();
        System.out.println("Logger path:" + loggerFilePath);
        logFileHandler = new FileHandler(loggerFilePath, false);
        java.util.logging.Logger l = java.util.logging.Logger.getLogger("");
        logFileHandler.setFormatter(new SimpleFormatter());
        l.addHandler(logFileHandler);
        // FIXME: use environment variable to set level
        l.setLevel(Level.INFO);
        l.info("=== Log session started ===");
    } catch (IOException e) {
        e.printStackTrace();
        LOG.error("initLoggerFileHandler()", e);
    }
}
Also used : SimpleFormatter(java.util.logging.SimpleFormatter) IOException(java.io.IOException) FileHandler(java.util.logging.FileHandler)

Example 14 with SimpleFormatter

use of java.util.logging.SimpleFormatter in project azure-tools-for-java by Microsoft.

the class Activator method initAzureToolsCoreLibsLoggerFileHandler.

private void initAzureToolsCoreLibsLoggerFileHandler() {
    try {
        String loggerFilePath = Paths.get(CommonSettings.settingsBaseDir, "corelibs.log").toString();
        System.out.println("Logger path:" + loggerFilePath);
        logFileHandler = new FileHandler(loggerFilePath, false);
        java.util.logging.Logger l = java.util.logging.Logger.getLogger("");
        logFileHandler.setFormatter(new SimpleFormatter());
        l.addHandler(logFileHandler);
        // FIXME: use environment variable to set the level
        l.setLevel(Level.INFO);
        l.info("=== Log session started ===");
    } catch (IOException e) {
        e.printStackTrace();
        log("initAzureToolsCoreLibsLoggerFileHandler@Activator", e);
    }
}
Also used : SimpleFormatter(java.util.logging.SimpleFormatter) IOException(java.io.IOException) FileHandler(java.util.logging.FileHandler)

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

Aggregations

SimpleFormatter (java.util.logging.SimpleFormatter)40 StreamHandler (java.util.logging.StreamHandler)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 Logger (java.util.logging.Logger)13 FileHandler (java.util.logging.FileHandler)9 LogRecord (java.util.logging.LogRecord)8 Handler (java.util.logging.Handler)7 MockResponse (com.google.mockwebserver.MockResponse)6 IOException (java.io.IOException)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 Level (java.util.logging.Level)3 Test (org.junit.Test)3 Config (edu.neu.ccs.pyramid.configuration.Config)2 File (java.io.File)2 Properties (java.util.Properties)2 MemoryHandler (java.util.logging.MemoryHandler)2 MockResponse (okhttp3.mockwebserver.MockResponse)2