Search in sources :

Example 16 with SimpleFormatter

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

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 17 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 18 with SimpleFormatter

use of java.util.logging.SimpleFormatter in project Payara by payara.

the class ACCLogger method createHandler.

/**
 * Creates a logging handler to send logging to the specified file, using
 * the indicated level.
 * @param filePath path to the log file to which to log
 * @param level level at which to log
 * @return logging Handler if filePath is specified and valid; null otherwise
 */
private static Handler createHandler(final LogService logService, final Level level) throws IOException {
    Handler handler = null;
    final String filePath = (logService == null) ? null : logService.getFile();
    if (filePath == null || filePath.equals("")) {
        return null;
    }
    handler = new FileHandler(filePath, true);
    handler.setFormatter(new SimpleFormatter());
    handler.setLevel(level);
    File lockFile = new File(filePath + ".lck");
    lockFile.deleteOnExit();
    return handler;
}
Also used : SimpleFormatter(java.util.logging.SimpleFormatter) FileHandler(java.util.logging.FileHandler) Handler(java.util.logging.Handler) File(java.io.File) FileHandler(java.util.logging.FileHandler)

Example 19 with SimpleFormatter

use of java.util.logging.SimpleFormatter in project Payara by payara.

the class LoggerFactoryJDK14 method configureFileHandler.

/**
 * This method throws  SecurityException  if a security manager exists and if
 * the caller does not have <tt>LoggingPermission("control"))</tt> or the
 * calling code is not placed in the doPrivileged() block.
 */
protected void configureFileHandler(LoggerJDK14 logger) {
    String name = logger.getName();
    // NOI18N
    String baseName = name + ".FileHandler";
    LogManager logManager = LogManager.getLogManager();
    // NOI18N
    String pattern = logManager.getProperty(baseName + ".pattern");
    if (pattern != null) {
        // If pattern != null, create and attach a FileHandler to logger.
        // Look various properties . If not found, fall back to
        // defaults
        int defaultLimit = 0;
        // NOI18N
        String limit = logManager.getProperty(baseName + ".limit");
        if (limit != null) {
            try {
                defaultLimit = Integer.parseInt(limit);
                if (defaultLimit < 0)
                    defaultLimit = 0;
            } catch (NumberFormatException e) {
            }
        }
        int defaultCount = 1;
        // NOI18N
        String count = logManager.getProperty(baseName + ".count");
        if (count != null) {
            try {
                defaultCount = Integer.parseInt(count);
                if (defaultCount < 1)
                    defaultCount = 1;
            } catch (NumberFormatException e) {
            }
        }
        boolean defaultAppend = false;
        // NOI18N
        String append = logManager.getProperty(baseName + ".append");
        if (append != null) {
            defaultAppend = Boolean.valueOf(append).booleanValue();
        }
        FileHandler fileHandler = null;
        try {
            fileHandler = new FileHandler(pattern, defaultLimit, defaultCount, defaultAppend);
        } catch (Exception e) {
            MessageFormat messageFormat = new MessageFormat(getMessages().getString(// NOI18N
            "errorlogger.filehandler.initialize.exception"));
            getErrorLogger().log(Logger.WARNING, messageFormat.format(new String[] { name }), e);
        }
        if (fileHandler != null) {
            // Initialize various attributes for the new fileHandler
            // --Level
            // NOI18N
            String level = logManager.getProperty(baseName + ".level");
            if (level != null) {
                try {
                    fileHandler.setLevel(Level.parse(level));
                } catch (IllegalArgumentException e) {
                }
            }
            // --Formatter
            Formatter defaultFormatter = null;
            // Initialize various attributes for the new fileHandler
            // NOI18N
            String formatter = logManager.getProperty(baseName + ".formatter");
            if (formatter != null) {
                try {
                    Class clz = ClassLoader.getSystemClassLoader().loadClass(formatter);
                    defaultFormatter = (Formatter) clz.newInstance();
                } catch (Exception e) {
                    // We got one of a variety of exceptions in creating the
                    // class or creating an instance.
                    // Drop through.
                    MessageFormat messageFormat = new MessageFormat(getMessages().getString("errorlogger.formatter.initialize.exception"));
                    getErrorLogger().log(Logger.WARNING, messageFormat.format(new String[] { name }), e);
                }
            }
            if (defaultFormatter == null) {
                defaultFormatter = new SimpleFormatter();
            }
            try {
                fileHandler.setFormatter(defaultFormatter);
            } catch (IllegalArgumentException e) {
            }
            logger.addHandler(fileHandler);
        }
    // if(fileHandler != null)
    }
// if(pattern != null)
}
Also used : MessageFormat(java.text.MessageFormat) SimpleFormatter(java.util.logging.SimpleFormatter) Formatter(java.util.logging.Formatter) SimpleFormatter(java.util.logging.SimpleFormatter) LogManager(java.util.logging.LogManager) IOException(java.io.IOException) FileHandler(java.util.logging.FileHandler)

Example 20 with SimpleFormatter

use of java.util.logging.SimpleFormatter in project tomee by apache.

the class FileHandler method configure.

/**
 * Configure from <code>LogManager</code> properties.
 */
private void configure() {
    final Timestamp ts = new Timestamp(System.currentTimeMillis());
    final String tsString = ts.toString().substring(0, 19);
    date = tsString.substring(0, 10);
    // allow classes to override
    final String className = this.getClass().getName();
    final ClassLoader cl = Thread.currentThread().getContextClassLoader();
    // Retrieve configuration of logging file name
    rotatable = Boolean.parseBoolean(getProperty(className + ".rotatable", "true"));
    if (directory == null) {
        directory = getProperty(className + ".directory", "logs");
    }
    if (prefix == null) {
        prefix = getProperty(className + ".prefix", "juli.");
    }
    if (suffix == null) {
        suffix = getProperty(className + ".suffix", ".log");
    }
    final String sBufferSize = getProperty(className + ".bufferSize", String.valueOf(bufferSize));
    try {
        bufferSize = Integer.parseInt(sBufferSize);
    } catch (final NumberFormatException ignore) {
    // no op
    }
    // Get encoding for the logging file
    final String encoding = getProperty(className + ".encoding", null);
    if (encoding != null && encoding.length() > 0) {
        try {
            setEncoding(encoding);
        } catch (final UnsupportedEncodingException ex) {
        // Ignore
        }
    }
    // Get logging level for the handler
    setLevel(Level.parse(getProperty(className + ".level", String.valueOf(Level.ALL))));
    // Get filter configuration
    final String filterName = getProperty(className + ".filter", null);
    if (filterName != null) {
        try {
            setFilter((Filter) cl.loadClass(filterName).newInstance());
        } catch (final Exception e) {
        // Ignore
        }
    }
    // Set formatter
    final String formatterName = getProperty(className + ".formatter", null);
    if (formatterName != null) {
        try {
            setFormatter((Formatter) cl.loadClass(formatterName).newInstance());
        } catch (final Exception e) {
            // Ignore and fallback to defaults
            setFormatter(new SimpleFormatter());
        }
    } else {
        setFormatter(new SimpleFormatter());
    }
    // Set error manager
    setErrorManager(new ErrorManager());
}
Also used : ErrorManager(java.util.logging.ErrorManager) SimpleFormatter(java.util.logging.SimpleFormatter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Timestamp(java.sql.Timestamp) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

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