Example usage for java.util.logging Handler getFormatter

List of usage examples for java.util.logging Handler getFormatter

Introduction

In this page you can find the example usage for java.util.logging Handler getFormatter.

Prototype

public Formatter getFormatter() 

Source Link

Document

Return the Formatter for this Handler .

Usage

From source file:MailHandlerDemo.java

/**
 * Gets a formatting string describing the given handler.
 *
 * @param prefix the output prefix.//from ww w  .  j av  a  2s  .  c om
 * @param err the error stream.
 * @param h the handler.
 * @return the formatted string.
 */
private static String toString(String prefix, PrintStream err, Handler h) {
    StringBuilder buf = new StringBuilder();
    buf.append(h.getClass().getName());
    try {
        if (h instanceof MailHandler) {
            MailHandler mh = (MailHandler) h;
            buf.append(", ").append(mh.getSubject());
        }
    } catch (SecurityException error) {
        err.print(prefix + ": ");
        error.printStackTrace(err);
    }

    try {
        buf.append(", ").append(h.getFormatter());
    } catch (SecurityException error) {
        err.print(prefix + ": ");
        error.printStackTrace(err);
    }

    try {
        if (h instanceof MailHandler) {
            MailHandler mh = (MailHandler) h;
            buf.append(", ").append(Arrays.toString(mh.getAttachmentFormatters()));
        }
    } catch (SecurityException error) {
        err.print(prefix + ": ");
        error.printStackTrace(err);
    }

    try {
        buf.append(", ").append(h.getLevel());
    } catch (SecurityException error) {
        err.print(prefix + ": ");
        error.printStackTrace(err);
    }

    try {
        buf.append(", ").append(h.getFilter());
    } catch (SecurityException error) {
        err.print(prefix + ": ");
        error.printStackTrace(err);
    }

    try {
        buf.append(", ").append(h.getErrorManager());
    } catch (SecurityException error) {
        err.print(prefix + ": ");
        error.printStackTrace(err);
    }

    buf.append(", ").append(toString(h.getClass().getClassLoader()));
    return buf.toString();
}

From source file:org.ensembl.healthcheck.testcase.AbstractPerlModuleBasedTestCase.java

protected void setPerlFriendlyLogFormatters() {

    Handler[] configuredHandler = logger.getHandlers();

    // It is possible that no Handler has been configured. In that case we
    // create and add one to the logger.
    //// ww w .  jav  a  2s .  c o  m
    if (configuredHandler.length == 0) {
        logger.addHandler(new ConsoleHandler());
        configuredHandler = logger.getHandlers();
    }

    // Now there is at least one configuredHandler.
    //
    for (Handler h : configuredHandler) {

        savedFormatter.put(h, h.getFormatter());
        h.setFormatter(perlLogMessagesFormatter);

        if (h.getFormatter() != perlLogMessagesFormatter) {
            throw new RuntimeException("Unable to set Formatter!");
        }
    }
}

From source file:org.openqa.selenium.server.log.LoggingManager.java

public static void overrideSimpleFormatterWithTerseOneForConsoleHandler(Logger logger, boolean debugMode) {
    for (Handler handler : logger.getHandlers()) {
        if (handler instanceof ConsoleHandler) {
            final Formatter formatter;

            formatter = handler.getFormatter();
            if (formatter instanceof SimpleFormatter) {
                final StdOutHandler stdOutHandler;
                final Level originalLevel;

                /*//from w w w .java  2s .  com
                 * DGF - Nobody likes the SimpleFormatter; surely they wanted our terse formatter instead.
                 */
                originalLevel = handler.getLevel();
                handler.setFormatter(new TerseFormatter(false));
                handler.setLevel(Level.WARNING);

                /*
                 * Furthermore, we all want DEBUG/INFO on stdout and WARN/ERROR on stderr
                 */
                stdOutHandler = new StdOutHandler();
                stdOutHandler.setFormatter(new TerseFormatter(false));
                stdOutHandler.setFilter(new MaxLevelFilter(Level.INFO));
                stdOutHandler.setLevel(originalLevel);
                logger.addHandler(stdOutHandler);
                if (debugMode) {
                    if (originalLevel.intValue() > Level.FINE.intValue()) {
                        stdOutHandler.setLevel(Level.FINE);
                    }
                }
            }
        }
    }
}

From source file:org.openqa.selenium.server.log.LoggingManager.java

protected static void saveOriginalHandlersFormattersAndLevels(Logger logger) {
    originalHandlers = logger.getHandlers();
    originalFormatters = new HashMap<Handler, Formatter>();
    originalLogLevels = new HashMap<Handler, Level>();
    for (Handler handler : originalHandlers) {
        originalFormatters.put(handler, handler.getFormatter());
        originalLogLevels.put(handler, handler.getLevel());
    }//from  ww w  . j a v  a  2  s.c  o m
}