Search in sources :

Example 76 with Handler

use of java.util.logging.Handler in project hudson-2.x by hudson.

the class WeakLogHandler method setLevel.

@Override
public void setLevel(Level newLevel) throws SecurityException {
    super.setLevel(newLevel);
    Handler t = resolve();
    if (t != null)
        t.setLevel(newLevel);
}
Also used : Handler(java.util.logging.Handler)

Example 77 with Handler

use of java.util.logging.Handler in project cassandra-mesos-deprecated by mesosphere.

the class Main method main.

public static void main(final String[] args) {
    int status;
    try {
        final Handler[] handlers = LogManager.getLogManager().getLogger("").getHandlers();
        for (final Handler handler : handlers) {
            handler.setLevel(Level.OFF);
        }
        org.slf4j.LoggerFactory.getLogger("slf4j-logging").debug("Installing SLF4JLogging");
        SLF4JBridgeHandler.install();
        status = _main();
    } catch (final SystemExitException e) {
        LOGGER.error(e.getMessage());
        status = e.status;
    } catch (final UnknownHostException e) {
        LOGGER.error("Unable to resolve local interface for http server");
        status = 6;
    } catch (final Throwable e) {
        LOGGER.error("Unhandled fatal exception", e);
        status = 10;
    }
    System.exit(status);
}
Also used : UnknownHostException(java.net.UnknownHostException) SLF4JBridgeHandler(org.slf4j.bridge.SLF4JBridgeHandler) Handler(java.util.logging.Handler)

Example 78 with Handler

use of java.util.logging.Handler in project cogtool by cogtool.

the class CogTool method enableLogging.

public static void enableLogging(boolean value) {
    if (value) {
        logger.setLevel(Level.ALL);
        for (Handler h : logger.getHandlers()) {
            if (h instanceof FileHandler) {
                return;
            }
        }
        FileHandler fh = null;
        try {
            fh = new FileHandler((CogToolPref.LOG_DIRECTORY.getString() + LOG_FILE_PATTERN), LOG_FILE_MAX_SIZE, LOG_FILES_MAX_COUNT, true);
        } catch (IOException ex) {
            logger.warning("Couldn't create log file: " + ex);
            return;
        }
        fh.setFormatter(new Formatter() {

            @Override
            public String format(LogRecord r) {
                long ms = r.getMillis();
                return String.format("%tF %tT.%tL\t%s\n", ms, ms, ms, r.getMessage());
            }
        });
        logger.addHandler(fh);
    } else {
        logger.setLevel(DEFAULT_LOG_LEVEL);
        for (Handler h : logger.getHandlers()) {
            if (!(h instanceof ConsoleHandler)) {
                logger.removeHandler(h);
            }
        }
    }
}
Also used : LogRecord(java.util.logging.LogRecord) Formatter(java.util.logging.Formatter) FileHandler(java.util.logging.FileHandler) RcvrExceptionHandler(edu.cmu.cs.hcii.cogtool.ui.RcvrExceptionHandler) ConsoleHandler(java.util.logging.ConsoleHandler) Handler(java.util.logging.Handler) IOException(java.io.IOException) ConsoleHandler(java.util.logging.ConsoleHandler) FileHandler(java.util.logging.FileHandler)

Example 79 with Handler

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

the class ClassLoaderLogManager method addLogger.

// --------------------------------------------------------- Public Methods
/**
     * Add the specified logger to the classloader local configuration.
     *
     * @param logger The logger to be added
     */
@Override
public synchronized boolean addLogger(final Logger logger) {
    final String loggerName = logger.getName();
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    ClassLoaderLogInfo info = getClassLoaderInfo(classLoader);
    if (info.loggers.containsKey(loggerName)) {
        return false;
    }
    info.loggers.put(loggerName, logger);
    // Apply initial level for new logger
    final String levelString = getProperty(loggerName + ".level");
    if (levelString != null) {
        try {
            AccessController.doPrivileged(new PrivilegedAction<Void>() {

                @Override
                public Void run() {
                    logger.setLevel(Level.parse(levelString.trim()));
                    return null;
                }
            });
        } catch (IllegalArgumentException e) {
        // Leave level set to null
        }
    }
    // Always instantiate parent loggers so that
    // we can control log categories even during runtime
    int dotIndex = loggerName.lastIndexOf('.');
    if (dotIndex >= 0) {
        final String parentName = loggerName.substring(0, dotIndex);
        Logger.getLogger(parentName);
    }
    // Find associated node
    LogNode node = info.rootNode.findNode(loggerName);
    node.logger = logger;
    // Set parent logger
    Logger parentLogger = node.findParentLogger();
    if (parentLogger != null) {
        doSetParentLogger(logger, parentLogger);
    }
    // Tell children we are their new parent
    node.setParentLogger(logger);
    // Add associated handlers, if any are defined using the .handlers property.
    // In this case, handlers of the parent logger(s) will not be used
    String handlers = getProperty(loggerName + ".handlers");
    if (handlers != null) {
        logger.setUseParentHandlers(false);
        StringTokenizer tok = new StringTokenizer(handlers, ",");
        while (tok.hasMoreTokens()) {
            String handlerName = (tok.nextToken().trim());
            Handler handler = null;
            ClassLoader current = classLoader;
            while (current != null) {
                info = classLoaderLoggers.get(current);
                if (info != null) {
                    handler = info.handlers.get(handlerName);
                    if (handler != null) {
                        break;
                    }
                }
                current = current.getParent();
            }
            if (handler != null) {
                logger.addHandler(handler);
            }
        }
    }
    // Parse useParentHandlers to set if the logger should delegate to its parent.
    // Unlike java.util.logging, the default is to not delegate if a list of handlers
    // has been specified for the logger.
    String useParentHandlersString = getProperty(loggerName + ".useParentHandlers");
    if (Boolean.parseBoolean(useParentHandlersString)) {
        logger.setUseParentHandlers(true);
    }
    return true;
}
Also used : StringTokenizer(java.util.StringTokenizer) URLClassLoader(java.net.URLClassLoader) Handler(java.util.logging.Handler) Logger(java.util.logging.Logger)

Example 80 with Handler

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

the class ClassLoaderLogManager method readConfiguration.

/**
     * Load specified configuration.
     *
     * @param is InputStream to the properties file
     * @param classLoader for which the configuration will be loaded
     * @throws IOException If something wrong happens during loading
     */
protected synchronized void readConfiguration(InputStream is, ClassLoader classLoader) throws IOException {
    ClassLoaderLogInfo info = classLoaderLoggers.get(classLoader);
    try {
        info.props.load(is);
    } catch (IOException e) {
        // Report error
        System.err.println("Configuration error");
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException ioe) {
        // Ignore
        }
    }
    // Create handlers for the root logger of this classloader
    String rootHandlers = info.props.getProperty(".handlers");
    String handlers = info.props.getProperty("handlers");
    Logger localRootLogger = info.rootNode.logger;
    if (handlers != null) {
        StringTokenizer tok = new StringTokenizer(handlers, ",");
        while (tok.hasMoreTokens()) {
            String handlerName = (tok.nextToken().trim());
            String handlerClassName = handlerName;
            String prefix = "";
            if (handlerClassName.length() <= 0) {
                continue;
            }
            // "10WebappFooHandler.")
            if (Character.isDigit(handlerClassName.charAt(0))) {
                int pos = handlerClassName.indexOf('.');
                if (pos >= 0) {
                    prefix = handlerClassName.substring(0, pos + 1);
                    handlerClassName = handlerClassName.substring(pos + 1);
                }
            }
            try {
                this.prefix.set(prefix);
                Handler handler = (Handler) classLoader.loadClass(handlerClassName).newInstance();
                // The specification strongly implies all configuration should be done
                // during the creation of the handler object.
                // This includes setting level, filter, formatter and encoding.
                this.prefix.set(null);
                info.handlers.put(handlerName, handler);
                if (rootHandlers == null) {
                    localRootLogger.addHandler(handler);
                }
            } catch (Exception e) {
                // Report error
                System.err.println("Handler error");
                e.printStackTrace();
            }
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) Handler(java.util.logging.Handler) IOException(java.io.IOException) Logger(java.util.logging.Logger) IOException(java.io.IOException) AccessControlException(java.security.AccessControlException)

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