Search in sources :

Example 36 with Handler

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

the class LoggingJAXRSCommons method configureLoggin.

protected void configureLoggin() throws Exception {
    msgs = new LinkedList<>();
    final java.util.logging.Logger looger = getLooger();
    looger.addHandler(new Handler() {

        @Override
        public void publish(final LogRecord record) {
            synchronized (LoggingJAXRSCommons.this) {
                msgs.add(record.getMessage());
            }
        }

        @Override
        public void flush() {
        // no-op
        }

        @Override
        public void close() throws SecurityException {
        // no-op
        }

        public Collection getMsg() {
            synchronized (LoggingJAXRSCommons.this) {
                return msgs;
            }
        }
    });
}
Also used : LogRecord(java.util.logging.LogRecord) Handler(java.util.logging.Handler) Collection(java.util.Collection)

Example 37 with Handler

use of java.util.logging.Handler in project BKCommonLib by bergerhealer.

the class ModuleLogger method createDefaultLogger.

private static Logger createDefaultLogger() {
    if (Bukkit.getServer() != null && Bukkit.getLogger() != MountiplexUtil.LOGGER)
        return Bukkit.getLogger();
    Logger log = Logger.getLogger("");
    log.setUseParentHandlers(false);
    CustomRecordFormatter formatter = new CustomRecordFormatter();
    ConsoleHandler consoleHandler = new ConsoleHandler();
    consoleHandler.setFormatter(formatter);
    for (Handler iHandler : log.getHandlers()) {
        log.removeHandler(iHandler);
    }
    log.addHandler(consoleHandler);
    return log;
}
Also used : ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) Logger(java.util.logging.Logger) ConsoleHandler(java.util.logging.ConsoleHandler)

Example 38 with Handler

use of java.util.logging.Handler in project MyPet by xXKeyleXx.

the class MyPetLogger method disableDebugLogger.

public void disableDebugLogger() {
    for (Handler h : getHandlers()) {
        if (h.toString().equals("MyPet-Debug-Logger-FileHandler")) {
            removeHandler(h);
            h.close();
        }
    }
}
Also used : FileHandler(java.util.logging.FileHandler) Handler(java.util.logging.Handler)

Example 39 with Handler

use of java.util.logging.Handler in project cxf by apache.

the class Log4jLogger method getHandlers.

public synchronized Handler[] getHandlers() {
    List<Handler> ret = new ArrayList<>();
    Enumeration<?> en = log.getAllAppenders();
    while (en.hasMoreElements()) {
        Appender ap = (Appender) en.nextElement();
        if (ap instanceof HandlerWrapper) {
            ret.add(((HandlerWrapper) ap).getHandler());
        }
    }
    return ret.toArray(new Handler[ret.size()]);
}
Also used : Appender(org.apache.log4j.Appender) ArrayList(java.util.ArrayList) Handler(java.util.logging.Handler)

Example 40 with Handler

use of java.util.logging.Handler in project cxf by apache.

the class Slf4jLogger method internalLogFormatted.

@Override
protected void internalLogFormatted(String msg, LogRecord record) {
    Level level = record.getLevel();
    Throwable t = record.getThrown();
    Handler[] targets = getHandlers();
    if (targets != null) {
        for (Handler h : targets) {
            h.publish(record);
        }
    }
    if (!getUseParentHandlers()) {
        return;
    }
    /*
         * As we can not use a "switch ... case" block but only a "if ... else if ..." block, the order of the
         * comparisons is important. We first try log level FINE then INFO, WARN, FINER, etc
         */
    if (Level.FINE.equals(level)) {
        if (locationAwareLogger == null) {
            logger.debug(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
        }
    } else if (Level.INFO.equals(level)) {
        if (locationAwareLogger == null) {
            logger.info(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.INFO_INT, msg, null, t);
        }
    } else if (Level.WARNING.equals(level)) {
        if (locationAwareLogger == null) {
            logger.warn(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.WARN_INT, msg, null, t);
        }
    } else if (Level.FINER.equals(level)) {
        if (locationAwareLogger == null) {
            logger.trace(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
        }
    } else if (Level.FINEST.equals(level)) {
        if (locationAwareLogger == null) {
            logger.trace(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.TRACE_INT, msg, null, t);
        }
    } else if (Level.ALL.equals(level)) {
        // but not accessible by the API Logger.xxx() API
        if (locationAwareLogger == null) {
            logger.error(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, null, t);
        }
    } else if (Level.SEVERE.equals(level)) {
        if (locationAwareLogger == null) {
            logger.error(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.ERROR_INT, msg, null, t);
        }
    } else if (Level.CONFIG.equals(level)) {
        if (locationAwareLogger == null) {
            logger.debug(msg, t);
        } else {
            locationAwareLogger.log(null, FQCN, LocationAwareLogger.DEBUG_INT, msg, null, t);
        }
    } else if (Level.OFF.equals(level)) {
    // don't log
    }
}
Also used : Handler(java.util.logging.Handler) Level(java.util.logging.Level)

Aggregations

Handler (java.util.logging.Handler)135 Logger (java.util.logging.Logger)52 ConsoleHandler (java.util.logging.ConsoleHandler)30 LogRecord (java.util.logging.LogRecord)24 Test (org.junit.Test)22 FileHandler (java.util.logging.FileHandler)17 File (java.io.File)14 IOException (java.io.IOException)13 Level (java.util.logging.Level)11 SimpleFormatter (java.util.logging.SimpleFormatter)8 Formatter (java.util.logging.Formatter)7 LogManager (java.util.logging.LogManager)6 PrintStream (java.io.PrintStream)5 ArrayList (java.util.ArrayList)5 SLF4JBridgeHandler (org.slf4j.bridge.SLF4JBridgeHandler)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 StringTokenizer (java.util.StringTokenizer)3 StdOutConsoleHandler (alma.acs.logging.StdOutConsoleHandler)2